# Accuracy and precision control for a simple calculation [closed]

Consider a very simple calculation: $$231 \times (2.54) ^ 3$$, which is the number of cubic cm in a US gallon.

On my phone app calculator I got the exact answer, which is $$3785.411784$$.

On Mathematica, doing the same calculation 231 * (2.54)^3 I got 3785.41.

Even with N[231 * 2.54^3, 100] etc. I got the same (imprecise) answer.

By converting the whole calculation to involve integers (254^3 * 231 /1000000) the answer was exact.

The Precision function kept indicating MachinePrecision, even when using N[] with a large number of significant figures.

How can I increase the precision beyond MachinePrecision for trivial calculations like this?

What if I have more complicated calculations involving exponents or other?

I would want to avoid accumulating loss of precision in each calculation.

• N[] won't convert machine precision to arbitrary precision. You could try showing the full result: Style[231*2.54^3, PrintPrecision -> 17] or FullForm[231*2.54^3]. Or you could feed arbitrary-precision numbers as input: 231*2.54100^3 (tho 100 digits is overkill). Commented Jan 3, 2023 at 18:32
• Read the Tech Note "Numbers" for more detail. Commented Jan 3, 2023 at 18:33
• Thanks a lot! In the event that there was a lengthy calculation with many similar constructs in it similar to 2.54^3, would Mathematica treat each intermediate with a full 15 or so digits of precision, or would each intermediate or sub-section of the calculations have only 7 or so digits, with accumulating error? If so, if extreme precision was needed, it may be necessary to go through each section of the calculation to expand precision? Commented Jan 3, 2023 at 21:56
• I am seeing from this discussion that a good practice would be to define and store constants with the desired level of precision, prior to a calculation; this would probably improve readability of the code as well. So for example to begin with cminch=2.5420. Then cminch^3 instead of 2.54^3. Commented Jan 4, 2023 at 1:40
• Numbers entered with a decimal point and no backtick specifying a precision are treated as machine precision (typically 64-bit floats, aka binary64 or double-precision). They have a 53-bit mantissa, which gives you about 15.95 digits of precision. (Despite having less than 16 digits of precision, it takes 17 in some edge cases to distinguish distinct binary floating-point numbers; e.g. Style[0.4 {1 - $MachineEpsilon/2, 1}, PrintPrecision -> 16] and retry with 17.) The default value for PrintPrecision is 6. Commented Jan 4, 2023 at 3:50 ## 3 Answers First off, the default display for finite precision numbers includes just a few digits. But the 15 or so digits of precision are all there: FullForm[231*2.54^3]  3785.411784' Secondly, when you tried this: N[231*2.54^3, 100]  The 2.54 had the standard precision, and you can't perform calculations at a higher precision that the elements within it. But there is a way to set precision for a number higher. 2.54100  tells Mathematica to use 100 digits of precision. So, you could do this if you really wanted to: 231*2.54100^3  3785.41178400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 UnitConvert[Quantity[1, "Gallons"], "Cubic Centimeters"] // N[#, 10] &  Quantity[3785.411784, ("Centimeters")^3] UnitConvert[Quantity[1, "Gallons"], "Liters"] // N[#, 10] &  Quantity[3.785411784, "Liters"] After Mark McClure's post here — handy up to 16 digits form[x_Real] := DecimalForm[x, Length@First@RealDigits@x]; form[x_] := x;$Post = form;

v = 231*2.54^3


3785.411784

Head[v]


Real

Unset with \$Post=.

Without further measures, limited to default precision

v = -12345678901234.567


-12345678901234.57