Skip to content

Instantly share code, notes, and snippets.

@naohaq
Created June 6, 2018 02:37
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 naohaq/f2b910e2f8bdd0f751999060606c0732 to your computer and use it in GitHub Desktop.
Save naohaq/f2b910e2f8bdd0f751999060606c0732 to your computer and use it in GitHub Desktop.
Output bit string of double value.
#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