C dynamically printf double, no loss of precision and no trailing zeroes

There’s probably no easier way. It’s a quite involved problem. Your code isn’t solving it right for several reasons: Most practical implementations of floating-point arithmetic aren’t decimal, they are binary. So, when you multiply a floating-point number by 10 or divide it by 10, you may lose precision (this depends on the number). Even though … 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

C fundamentals: double variable not equal to double expression?

I suspect you’re using 32-bit x86, the only common architecture subject to excess precision. In C, expressions of type float and double are actually evaluated as float_t or double_t, whose relationships to float and double are reflected in the FLT_EVAL_METHOD macro. In the case of x86, both are defined as long double because the fpu … Read more