| New Reply |
The smallest floating point x such that x+2=x |
Share Thread | Thread Tools |
| Nov10-11, 09:00 AM | #1 |
|
|
The smallest floating point x such that x+2=x
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 that 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 |
| Nov10-11, 09:29 AM | #2 |
|
|
Might I suggest you approach this from the bit level? You may however not be a bit-head and won't like this idea. But if you're interested, for example, on a 32-bit machine, say floating point numbers are double-wide so 64 bits, what is the smallest and next largest number centered at 2 in 64-bit floating-point format that can be coded? What are the bit-values for the exponent and mantissa for these numbers? There has to be a definite bit-pattern for this.
|
| Nov10-11, 09:41 AM | #3 |
|
Mentor
|
In your code, the while loop repeatedly multiplies your starting value by 2. You should be dividing it by 2 in each loop iteration. |
| Nov10-11, 11:08 AM | #4 |
|
|
The smallest floating point x such that x+2=xSo, I suppose that: [tex](2)_{10} =(0.1 \underbrace{000...0}_\text{51})_2 \times 2^2[/tex] What's next? |
| Nov10-11, 11:14 AM | #5 |
|
Recognitions:
|
Single precision floating point numbers have a sign bit, an 8 bit exponent, and a 24 bit signifcand where the upper bit is assumed to be 1 so only the lower 23 bits are stored in the number.
http://en.wikipedia.org/wiki/Single_...g-point_format For single precicion, (x+2) (truncated) = x when x = 2^25 = 33554432, which in single precision format will be encoded as ((25+127) << 23) = hex 4c000000. Trying to add 2 to this number will result in the 2 shifted off to the right of the signifcand when adding, assuming the compiler doesn't include some rounding up function internally. For double precision, the signficand has 53 bits (52 stored), so you'd want 2^54 = 18014398509481984 which will be stored as ((54+1023)<< 52) = hex 4350000000000000 http://en.wikipedia.org/wiki/Double_...g-point_format |
| Nov10-11, 11:23 AM | #6 |
|
Recognitions:
|
Each time you enter the test loop, instead of multiplying the old x value by 2, why not divide by 2?
|
| Nov10-11, 11:25 AM | #7 |
|
Mentor
|
phil.st: That number is a bit big; it looks like a double rather than a float. I get 33554432 rather than 3.68935E+019. |
| Nov10-11, 11:55 AM | #8 |
|
|
@rcgldr: MATLAB gives me the following results:
Code:
>> format long e
>> x = 2^54
x =
1.801439850948198e+016
>> tf = isequal(x,x+2)
tf =
1
>>
@D H: To be honest, I'm totally confused at this point. |
| Nov10-11, 12:00 PM | #9 |
|
Mentor
|
What machine, what compiler are you using?
|
| Nov10-11, 12:07 PM | #10 |
|
|
2) Dev-C++ 5.0 beta 9.2 (4.9.9.2) with Mingw/GCC, MATLAB |
| Nov10-11, 12:27 PM | #11 |
|
Mentor
|
You haven't told MATLAB to use single precision, so it is using the default double precision. You are probably compiling optimized, and your compiler is (erroneously) eliminating the cast to float, instead using the internal 80 bit doubles for the comparison. Try compiling unoptimized, and also tell the compiler to stop using floating point registers so much: Code:
#include <stdio.h>
int main( int argc, char **argv )
{
float x = 1.0f;
volatile float xp2 = x + 2.0f;
printf( "current x \t 2 + current x\n" );
do {
printf( "%.1f\t\t %.1f\n", x, xp2 );
x *= 2.0f;
xp2 = x + 2.0f;
}
while (xp2 != x);
printf( "\nCalculated x: %.1f\n", x );
return 0;
}
|
| Nov10-11, 12:29 PM | #12 |
|
|
|
| Nov10-11, 01:37 PM | #13 |
|
Mentor
|
|
| Nov13-11, 04:52 AM | #14 |
|
|
|
| Nov13-11, 05:56 AM | #15 |
|
Recognitions:
|
http://en.wikipedia.org/wiki/Extended_precision |
| Nov13-11, 06:48 AM | #16 |
|
Mentor
|
2^65 is 3.6893488147419103232×1019, which is the stated value. The Intel 80 bit extended precision format has a 64 bit significand and does not use normalized numbers as does the IEEE floating point standard. |
| Nov13-11, 10:07 AM | #17 |
|
Recognitions:
|
http://en.wikipedia.org/wiki/Extended_precision Code:
#include <stdio.h>
int main(int argc, char **argv )
{
float x = 1.0f;
float y;
do {
x *= 2.0f;
y = x + 2.0f;
}
while (*(long *)(&y) != *(long *)(&x));
printf( "\nCalculated x: %21.0f\n", x );
return 0;
}
Code:
while (y != x); |
| New Reply |
| Thread Tools | |
Similar Threads for: The smallest floating point x such that x+2=x
|
||||
| Thread | Forum | Replies | ||
| matlab floating point | Programming & Comp Sci | 6 | ||
| decimal to floating point | Engineering, Comp Sci, & Technology Homework | 10 | ||
| convert 1/3 to Floating Point | Programming & Comp Sci | 2 | ||
| floating point | Precalculus Mathematics Homework | 1 | ||
| floating point format | Engineering, Comp Sci, & Technology Homework | 1 | ||