Double compare

Run Settings
LanguageC
Language Version
Run Command
#include <stdio.h> #include <stdint.h> #include <stdbool.h> #include <stdlib.h> typedef int64_t s64; typedef uint64_t u64; typedef int64_t s8; typedef double f64; typedef bool b8; u64 f64Tos64(f64 a) { s64 Result = *((s64 *)&a); return Result; } f64 s64Tof64(s64 a) { f64 Result = *((f64 *)&a); return Result; } b8 Compare(f64 in_a, f64 in_b, s8 Shift) { s64 a = f64Tos64(in_a); s64 b = f64Tos64(in_b); // Verify: // How exponet reacts. // Compare on exponet first? // Assert shift is no higher than mantissa. return labs(a-b) <= (2 << Shift); } int main() { f64 a = 0.30000000000001; // Equal // f64 a = 0.3000000000001; // Not Equal f64 b = 0.3; printf("%f\n",a); printf("%lx\n",f64Tos64(a)); printf("%f\n",b); printf("%lx\n",f64Tos64(b)); printf("%li\n",f64Tos64(3.0) - f64Tos64(0.3)); printf("-0.0 - 0.0: %li\n",f64Tos64(-0.0) - f64Tos64(0.0)); // Problems :( printf("%li\n",Compare(f64Tos64(-0.0), f64Tos64(0.0), 8)); if(Compare(b,a,8)) { printf("yay?\n"); } else { printf("nay\n"); } return 0; }
Editor Settings
Theme
Key bindings
Full width
Lines