Skip to content

Instantly share code, notes, and snippets.

@jepler
Created August 31, 2017 23:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jepler/0ebbd97520ef0c277577226f5180ba75 to your computer and use it in GitHub Desktop.
Save jepler/0ebbd97520ef0c277577226f5180ba75 to your computer and use it in GitHub Desktop.
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <stdint.h>
#include <inttypes.h>
int main() {
for(int i=0; i<1000000; i++) {
char s[16];
snprintf(s, sizeof(s), "1.%06d", i);
double d = strtod(s, NULL);
uint64_t dc = (uint64_t)(d * 1e6);
if(dc != i + 1000000)
printf("%s %7" PRId64 " %.14A %.14A\n", s, dc, d, d*1e6);
}
}
@jepler
Copy link
Author

jepler commented Aug 31, 2017

This program shows all values of the form "1.xxxxxx" which, when multiplied by 1e6 and truncated to an integer don't equal the value you expect in decimal arithmetic.

On an x86_64 platform, counting the number of "unexpected" values:

$ for ARCH in -m32 -m64; do for STD in -std=gnu11 -std=c11; do gcc $ARCH $STD -O into.c; printf "%3s %12s " $ARCH $STD; ./a.out | wc -l; done; done 
-m32   -std=gnu11 499718
-m32     -std=c11 499718
-m64   -std=gnu11 11554
-m64     -std=c11 11554

On an armhf platform, the count is:

$ for STD in -std=gnu11 -std=c11 ; do printf "%12s " $STD; gcc $STD into.c && ./a.out | wc -l; done
  -std=gnu11 11554
    -std=c11 11554

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment