Skip to content

Commit

Permalink
add float precision demonstration
Browse files Browse the repository at this point in the history
  • Loading branch information
vladak committed Oct 27, 2023
1 parent 8748415 commit d14eb80
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/float-precision.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Compile and run the program, observe what happens.
* Then recompile it with the -DADDTWO and observe once again.
* Why is that ?
*/

#include <stdio.h>

int
main(void)
{
float f = 1 << 23;
size_t cnt = 0;

printf("%zu\n", sizeof (f));

while (1) {
printf("%16.2f %zu\n", f, cnt++);
#if ADDTWO
if (f >= 16777216.0)
f += 2;
else
#endif
f += 1;
}
}

0 comments on commit d14eb80

Please sign in to comment.