Skip to content

Instantly share code, notes, and snippets.

@qddddr
Created November 10, 2013 09:34
Show Gist options
  • Save qddddr/7396009 to your computer and use it in GitHub Desktop.
Save qddddr/7396009 to your computer and use it in GitHub Desktop.
test uint52_t
#include <iostream>
#include <cinttypes>
using namespace std;
void display(double d)
{
const uint64_t u = *reinterpret_cast<uint64_t*>(&d);
const auto sign = u >> 63;
const auto exponent = (u >> 52) & ((1 << 11) - 1);
const auto fraction = u & ((static_cast<uint64_t>(1) << 52) - 1);
cout << d << endl;
cout << "\tsign: " << sign << endl;
cout << "\texponent: " << exponent << endl;
cout << "\tfraction: " << fraction << endl;
}
double convert(uint64_t sign, uint64_t exponent, uint64_t fraction)
{
uint64_t u = (sign << 63) + (exponent << 52) + fraction;
return *reinterpret_cast<double*>(&u);
}
double count_niters(uint64_t n)
{
const auto x = convert(0,0,n);
const auto one = convert(0,0,1);
auto counter = convert(0,0,0);
for (double x1 = 0, x2 = convert(0,0,70); x1 != x2; ) {
display(x2);
x1 = x2;
const auto u1 = x1/one;
x2 = (x1 + (x + x1 - one) / u1) / 2;
counter += one;
}
return counter;
}
int main(void)
{
display(count_niters(4321));
}
/*
* $ ./a.out
* 3.45846e-322
* sign: 0
* exponent: 0
* fraction: 70
* 3.26083e-322
* sign: 0
* exponent: 0
* fraction: 66
* 9.88131e-324
* sign: 0
* exponent: 0
* fraction: 2
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment