Skip to content

Instantly share code, notes, and snippets.

@bilbothebaggins
Last active February 9, 2022 14:52
Show Gist options
  • Select an option

  • Save bilbothebaggins/d8a44d38b4b54bfefbb67feb5baad0f5 to your computer and use it in GitHub Desktop.

Select an option

Save bilbothebaggins/d8a44d38b4b54bfefbb67feb5baad0f5 to your computer and use it in GitHub Desktop.
C++ Floating Point Double 655
#include <cstdio>
#include <string>
__declspec(noinline) double get_delta(double lhs, double rhs) {
return lhs - rhs;
}
void print_dbl(std::string const& what, double val) {
const int dig14 = DBL_DIG - 1;
printf("'%s'@%d: %.*e\n", what.c_str(), dig14, dig14, val);
std::string s14(15 + 1, '.');
printf("' '@__: %s\n", s14.c_str());
printf("'%s'@%d: %.*e\n", what.c_str(), 18, 18, val);
uint64_t bits;
memcpy(&bits, &val, sizeof(uint64_t));
printf("'%s'@XX: 0x%016llx\n", what.c_str(), bits);
printf("\n");
}
int main()
{
double a = 655.36;
double b = 655.34;
double d = get_delta(a, b);
double zz2 = 0.02;
print_dbl("a", a);
print_dbl("b", b);
print_dbl("d", d);
print_dbl("2", zz2);
}
@bilbothebaggins

bilbothebaggins commented Feb 9, 2022

Copy link
Copy Markdown
Author

Output:

'a'@14: 6.55360000000000e+02
' '@__: ................
'a'@18: 6.553600000000000136e+02
'a'@XX: 0x40847ae147ae147b

'b'@14: 6.55340000000000e+02
' '@__: ................
'b'@18: 6.553400000000000318e+02
'b'@XX: 0x40847ab851eb851f

'd'@14: 1.99999999999818e-02
' '@__: ................
'd'@18: 1.999999999998181011e-02
'd'@XX: 0x3f947ae147ae0000

'2'@14: 2.00000000000000e-02
' '@__: ................
'2'@18: 2.000000000000000042e-02
'2'@XX: 0x3f947ae147ae147b

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment