Created
October 21, 2016 15:56
-
-
Save blippy/ccbdd49c86326380b2dba65b63632eb1 to your computer and use it in GitHub Desktop.
C++ decimal arithmetic
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 <iostream> | |
#include <decimal/decimal> | |
using std::cout; | |
using std::endl; | |
template<typename T> | |
void print(T d) | |
{ | |
cout << std::decimal::decimal_to_double(d) << endl; | |
} | |
int main() | |
{ | |
std::decimal::decimal64 d1; | |
d1 = 1234; | |
print(d1); // 1234 | |
std::decimal::decimal64 d2 = std::decimal::make_decimal64(987633LL, -2); | |
print(d2); // 9876.33 | |
std::decimal::decimal64 d3 = std::decimal::make_decimal64(3LL, -3); | |
print(d2*d3); // 29.629 [29.627899] | |
print(d2+d3); // 9876.33 | |
print(d3+d2); // 9876.33 | |
print(d3*d3); // 9e-06 | |
std::decimal::decimal64 d4 = std::decimal::make_decimal64(25LL, -1); | |
cout << std::decimal::decimal_to_long_double(d4) << endl; | |
cout << "testing accuracy" << endl; | |
cout << ((1.2 - 1.0) == 0.2) << endl; // 0 meaning false | |
std::decimal::decimal64 d12 = std::decimal::make_decimal64(12LL, -1); | |
std::decimal::decimal64 d10 = std::decimal::make_decimal64(10LL, -1); | |
std::decimal::decimal64 d02 = std::decimal::make_decimal64( 2LL, -1); | |
cout << ((d12-d10) == d02) << endl; // 1 true | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment