Floating point format can't store every number.
The numbers that it can store goes in steps. Like
1.00000000000000000000000
1.00000011920928955078125
1.00000023841857910156250
1.00000035762786865234375
...
So the number you are trying to store, i.e.
1.0000000000000000000000000000000000000117549435..
can't be represented in float format. So in your case it's rounded to 1.0
Try this program:
#include <stdio.h>
#include <assert.h>
union
{
float f;
unsigned u;
} val;
int main() {
assert(sizeof(float) == 4);
assert(sizeof(unsigned) == 4);
val.u = 0x3f800000;
printf("0x%08x -> %.50f\n", val.u, val.f);
++val.u;
printf("0x%08x -> %.50f\n", val.u, val.f);
++val.u;
printf("0x%08x -> %.50f\n", val.u, val.f);
++val.u;
printf("0x%08x -> %.50f\n", val.u, val.f);
return 0;
}
Output:
0x3f800000 -> 1.00000000000000000000000000000000000000000000000000
0x3f800001 -> 1.00000011920928955078125000000000000000000000000000
0x3f800002 -> 1.00000023841857910156250000000000000000000000000000
0x3f800003 -> 1.00000035762786865234375000000000000000000000000000
The above code uses a union
of float
and unsigned
and an increment of the unsigned
to get the next float.
Another way is to use nextafterf
function to get the next representable float. Like:
#include <stdio.h>
#include <assert.h>
#include <math.h>
union
{
float f;
unsigned u;
} val;
int main() {
assert(sizeof(float) == 4);
assert(sizeof(unsigned) == 4);
val.f = 1*pow(2, -126);
printf("0x%08x --> %.200f\n", val.u, val.f);
val.f = nextafterf(val.f, 1.0);
printf("0x%08x --> %.200f\n", val.u, val.f);
val.f = nextafterf(val.f, 1.0);
printf("0x%08x --> %.200f\n", val.u, val.f);
val.f = nextafterf(val.f, 1.0);
printf("0x%08x --> %.200f\n", val.u, val.f);
return 0;
}
Output:
0x00800000 --> 0.00000000000000000000000000000000000001175494350822287507968736537222245677818665556772087521508751706278417259454727172851562500000000000000000000000000000000000000000000000000000000000000000000000000
0x00800001 --> 0.00000000000000000000000000000000000001175494490952133940450443629595204006810278684798281709160328881985245648433835441437622648663818836212158203125000000000000000000000000000000000000000000000000000
0x00800002 --> 0.00000000000000000000000000000000000001175494631081980372932150721968162335801891812824475896811906057692074037412943710023682797327637672424316406250000000000000000000000000000000000000000000000000000
0x00800003 --> 0.00000000000000000000000000000000000001175494771211826805413857814341120664793504940850670084463483233398902426392051978609742945991456508636474609375000000000000000000000000000000000000000000000000000