Created
June 6, 2018 02:37
-
-
Save naohaq/f2b910e2f8bdd0f751999060606c0732 to your computer and use it in GitHub Desktop.
Output bit string of double value.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdint.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <math.h> | |
typedef union U_DBL_INT { | |
double d; | |
uint64_t i; | |
} dbl_int_t; | |
static const char * | |
u64_to_bitstr(uint64_t x) | |
{ | |
static char buf[65]; | |
int32_t k; | |
buf[64] = 0; | |
for (k=0; k<64; k+=1) { | |
char c = '0'; | |
if ((x & 1) == 1) { | |
c = '1'; | |
} | |
else { | |
c = '0'; | |
} | |
buf[63-k] = c; | |
x = x >> 1; | |
} | |
return buf; | |
} | |
int | |
main(int argc, char * argv[]) | |
{ | |
double x; | |
double y; | |
dbl_int_t v; | |
x = 1.0/1024.0; | |
y = 1.0 - x*x*x*x*x*0.25; | |
v.d = cbrt(y); | |
printf("%s\n", u64_to_bitstr(v.i)); | |
printf("%.16e\n", v.d); | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment