c++ - What gets converted to what when comparing a double to an integer for identity? -


OK, so I know that you are not usually comparing two floating-point numbers for equality . However, in William Cowans he shows the following code (pseudo-code, I believe):

  actual function T (Real Z): T: = XP (Z); ... round, of course. If (T = 1) returns (T); ... when Z | If very small (T = 0) returns (T: = -1 / Z); ... when the XP is underflow return (T: = (T-1) / log (T)); ... In all other cases, End T  

Now, I'm interested in implementing it in C or C ++, and I have two related questions:

< P> a) If I take t to be doubled, then in comparison to (t == 1) or (t == 0) 0 and 1, to maintain the correctness of the values ​​involved in multi-type expression Will be converted into two?

b) Does it still calculate the comparison of two floating-point numbers for equality?

Yes and yes.

For 32-bit ints, double can display each value properly when you compare a double from 64-bit int, however, the potential goaloff error You can use the long double if int is greater than 2 ^ 52, however, in which at least 64 bits is Montisa.

Of course, the best way is to just use a floating-point word: 1.0 or simply 1. type double , 1.0f is a float , and My_float_type (1) to whatever It is supposed to be: v).


Comments