Skip to content

Instantly share code, notes, and snippets.

@bast
Last active January 11, 2017 09:19
Show Gist options
  • Save bast/e9cc08bcfdebcf17957765b91e775fdf to your computer and use it in GitHub Desktop.
Save bast/e9cc08bcfdebcf17957765b91e775fdf to your computer and use it in GitHub Desktop.
Example demonstrating increment problem with 32-bit float.
#include "stdio.h"
/* example obtained from Ole Martin Bjoerndalen */
int main()
{
float a = 16777216;
printf("before increment %f\n", a);
float b = a + 1;
printf("after increment by one %f\n", b);
/* the integer part of a 32-bit float is 24 bits, if you add 2 you get the correct result */
b = a + 2;
printf("after increment by two %f\n", b);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment