Last active
February 9, 2022 14:52
-
-
Save bilbothebaggins/d8a44d38b4b54bfefbb67feb5baad0f5 to your computer and use it in GitHub Desktop.
C++ Floating Point Double 655
This file contains hidden or 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 <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); | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output: