Skip to content

Instantly share code, notes, and snippets.

@KevOrr
Created June 4, 2018 03:40
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 KevOrr/1101b358cf0bd10fff98bb62901fd46f to your computer and use it in GitHub Desktop.
Save KevOrr/1101b358cf0bd10fff98bb62901fd46f to your computer and use it in GitHub Desktop.
Floating Point Weirdness
f1: 0.707106769084930419921875 f3 04 35 3f
f2: 0.707106769084930419921875 f3 04 35 3f
f3: 0.707106769084930419921875 f3 04 35 3f
d1: 0.707106781186547572737311 cd 3b 7f 66 9e a0 e6 3f
d2: 0.707106781186547461715008 cc 3b 7f 66 9e a0 e6 3f
l1: 0.707106781186547572737311 00 68 de f9 33 f3 04 b5 fe 3f 00 00 00 00 00 00
l2: 0.707106781186547524436104 85 64 de f9 33 f3 04 b5 fe 3f 40 00 00 00 00 00
#include <stdio.h>
#include <stdint.h>
#include <math.h>
void print_hex(void *arr, size_t n) {
for (size_t i=0; i<n; i++)
printf("%02x ", *(uint8_t*)(arr + i));
puts("");
}
int main() {
float f1 = sqrt(0.5f);
float f2 = 1.0f / (2.0f * (float)sqrt(0.5f));
float f3 = 1.0f / (2.0f * sqrtf(0.5f));
double d1 = sqrt(0.5);
double d2 = 1.0 / (2.0 * sqrt(0.5));
long double l1 = sqrt(0.5l);
long double l2 = 1.0l / (2.0l * sqrtl(0.5l));
printf("f1: %.24f ", f1);
print_hex(&f1, 4);
printf("f2: %.24f ", f2);
print_hex(&f2, 4);
printf("f3: %.24f ", f3);
print_hex(&f3, 4);
printf("d1: %.24f ", d1);
print_hex(&d1, 8);
printf("d2: %.24f ", d2);
print_hex(&d2, 8);
printf("l1: %.24Lf ", l1);
print_hex(&l1, 16);
printf("l2: %.24Lf ", l2);
print_hex(&l2, 16);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment