# Thread: The smallest floating point x such that x+2=x

1. Hi! I want to calculate the smallest floating point x such that x+2=x. I've written a programme in C that does not actually determines the smallest floating point x such that x+2=x, but rather determines a power of two within an interval that contains the smallest floating point x. Does anyone have any ideas how can I improve this solution or find out a new one? Is there any built-in function in MATLAB and/or C to get accurate approximations of this x?

Code:
```#include <stdio.h>

int main( int argc, char **argv )
{
float x = 1.0f;

printf( "current x \t 2 + current x\n" );
do {
printf( "%G\t\t %.20f\n", x, (2.0f + x) );
x *= 2.0f;
}
while ((float)(2.0 + x) != x);

printf( "\nCalculated x: %G\n", x );
return 0;
}```
Code:
`Calculated x: 3.68935E+019`  2.

3. What about binary search? Run it just after your do - while loop. You'll have
Code:
```  double hi = 2.0 * x;
double lo = x;

while ( lo != hi ) // maybe lo < hi
{
float mi = (lo + hi) * 0.5;

if ( (float)(mi + 2.0) == 2.0 )
{
lo = mi;
}
else
{
hi = mi;
}
}```
Or something like that.  4. @BlasW
Calculated hi, lo and mi: 3.68935E+019
Any ideas?  5. This is (obviously?) dependent on the implementation of floating point numbers. Wouldn't it be easier to just look at the encoding (presumably for 64-bit IEEE)? You should be able to work it out from this: Double precision floating-point format - Wikipedia, the free encyclopedia

It looks like the answer should be 3.6028797 × 1016 (which is pretty close to your result).

And in MATLAB, isn't this 2 x bitmax (http://www.mathworks.de/help/techdoc/ref/bitmax.html)

EDIT: not sure that is correc ... but I don't have time to work out why!  6. Originally Posted by Strange This is (obviously?) dependent on the implementation of floating point numbers. Wouldn't it be easier to just look at the encoding (presumably for 64-bit IEEE)? You should be able to work it out from this: Double precision floating-point format - Wikipedia, the free encyclopedia

It looks like the answer should be 3.6028797 × 1016 (which is pretty close to your result).

And in MATLAB, isn't this 2 x bitmax (Maximum double-precision floating-point integer - MATLAB)

EDIT: not sure that is correc ... but I don't have time to work out why!
Sorry! I read the articles but I don't understand at all. How did you calculate the x? Could you be more specific?  7. My answer was wrong because I thought you were dealing with doubles not floats. But, from that wikipedia page: "from 253 to 254 ... the representable numbers are the even ones". So you could add 2 to any number in this range and you would get the next number. So (I think) the smallest number where adding 2 would have no effect is 2^54 = 3.6028797E16.

But as you are looking at floats, I would have expected the number to be 2^26 = about 6.7E7. (But I am not convinced!)

Possible problems with your code: in your loop test, you add 2.0 (a double) instead of 2.0f, this will promote both values before doing the additions so you will get all sorts of complications due to rounding; you do an equality test which is never a good idea with floating point numbers.  8. The equation has no solution. Each term on the both sides of the equal signus represent two parallel straight lines separated by 2 units which never intersect.
In other words, x+2=x is the same as x+2-x=0 what is the same as 2=0 which is an absurd meaning no solution.  Bookmarks
 Posting Permissions
 You may not post new threads You may not post replies You may not post attachments You may not edit your posts   BB code is On Smilies are On [IMG] code is On [VIDEO] code is On HTML code is Off Trackbacks are Off Pingbacks are Off Refbacks are On Terms of Use Agreement