Python “decimal” package gives wrong results

When you construct a Decimal from a floating-point number, you get the exact value of the floating-point number, which may not precisely match the decimal value because that’s how floating-point numbers work. If you want to do precise decimal arithmetic, construct your Decimal objects from strings instead of floating-point numbers: >>> Decimal(‘22.0’) / Decimal(‘10.0’) – … Read more

Golang converting float64 to int error

You need to understand something: 100.55 is a decimal number (presented in decimal radix). 100.55 in decimal is a finite number and is exactly this: 100.55. Computers in general store numbers in binary representation. The number 100.55 cannot be represented with a finite binary number: 100.55 is an infinite number in binary representation (same reason … Read more

Exact decimal datatype for C++?

The Boost.Multiprecision library has a decimal based floating point template class called cpp_dec_float, for which you can specify any precision you want. #include <iostream> #include <iomanip> #include <boost/multiprecision/cpp_dec_float.hpp> int main() { namespace mp = boost::multiprecision; // here I’m using a predefined type that stores 100 digits, // but you can create custom types very easily … Read more

C# Converting 20 digit precision double to string and back again

Use the “R” numeric format string: double d = 0.00034101243963859839; string s = d.ToString(“R”); //… double d2 = double.Parse(s); if(d == d2) { //– Success } The R stands for “round-trip”. From the linked document: This format is supported only for the Single and Double types. The round-trip specifier guarantees that a numeric value converted … Read more