How to get bc to handle numbers in scientific (aka exponential) notation?

Unfortunately, bc doesn’t support scientific notation. However, it can be translated into a format that bc can handle, using extended regex as per POSIX in sed: sed -E ‘s/([+-]?[0-9.]+)[eE]\+?(-?)([0-9]+)/(\1*10^\2\3)/g’ <<<“$value” you can replace the “e” (or “e+”, if the exponent is positive) with “*10^”, which bc will promptly understand. This works even if the exponent … 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

Why does floating-point arithmetic not give exact results when adding decimal fractions?

Binary floating point math is like this. In most programming languages, it is based on the IEEE 754 standard. The crux of the problem is that numbers are represented in this format as a whole number times a power of two; rational numbers (such as 0.1, which is 1/10) whose denominator is not a power … Read more

Rounding Errors?

It is true. It is an inherent limitation of how floating point values are represented in memory in a finite number of bits. This program, for instance, prints “false”: public class Main { public static void main(String[] args) { double a = 0.7; double b = 0.9; double x = a + 0.1; double y … Read more

Why does the floating-point value of 4*0.1 look nice in Python 3 but 3*0.1 doesn’t?

The simple answer is because 3*0.1 != 0.3 due to quantization (roundoff) error (whereas 4*0.1 == 0.4 because multiplying by a power of two is usually an “exact” operation). Python tries to find the shortest string that would round to the desired value, so it can display 4*0.1 as 0.4 as these are equal, but … Read more