Message Boards Message Boards

0
|
3345 Views
|
9 Replies
|
4 Total Likes
View groups...
Share
Share this post:

Rounding the output of simple division

Posted 2 years ago

Hello!

Why

FullForm[Apply[Plus, {4.2, 5, 3, 4.1, 4.4, 4.2}]/6]

returns 4.1499999999999995` and not 4.15?

This generates a problem when we want to round the result :

Round[Apply[Plus, {4.2, 5, 3, 4.1, 4.4, 4.2}]/6, 0.1]

returns 4.1 instead of 4.2.

I met this problem when I've done this calculation :

Round[Mean[{4.2, 5, 3, 4.1, 4.4, 4.2}], 0.1]

Thank you for your help.

Jérôme

9 Replies
Posted 2 years ago

There seems to be an inconsistency in Mathematica when rounding a number of type x.5, or x.y5, x.yz5 ... When the last digit before 5 is even:

enter image description here

NumberForm rounds upwards. The usual way.

Round uses "bankers rounding", to the nearest even number.

POSTED BY: Hans Milton

Further to your answers, I 've yeasterday done some manipulations with Mathematica and I've realized something very strange (for me):

When you eval Round[21.5], Mathematica returns 22 what you are waiting for. But when you eval Round[22.5], Mathematica returns 22 too. For an average user, this incoherence is not obvious to understand.

Obviously, my interest was to understand why Round[5.25,0.1] gives 5.2 and Round[5.15,0.1] too. Indeed, this type of rounding often occurs in my job (teacher of mathematics) when I calculate some mean of notes rounded to the tenth.

Have you an explanation for this apparent incoherence ?

Thank you for your help.

Best regards

Jérôme

Round-to-even is documented behavior (Details, second bullet item). For rationale, could check this MSE thread and references provided therein.

POSTED BY: Daniel Lichtblau

Thank you for your answers. They help me !

Jérôme,

However, I find strange that Mathematica is not able to compute properly a simple division like 24.9 / 6 without using the Rationalize function. My calculator do it without any probleme...

Mathematica does do 24.9/ 6 exactly like your calculator. It even displays 4.15 . The calculator rounds to 10 or 12 or some number of digits that is has in its display. If you round to 12 digits in Mathematica you also get 4.15 exactly. The problem is you are rounding to a very low precision and bypassing that internal round that the calculator does to display the result. Using a version of Hans' suggestion would be more like the calculator and could fix any floating point errors. First a round to 14 or 15 decimal places and then round to what you want at lower precision would mimic the calculator's internal operation.

This is a classical problem in computing with floating point numbers. If you use C or any other language you would get the exact same result. Besides, as a theoretical issue, you should NEVER round to a digit that you care about (where +-1 makes a difference). If a difference in (for example) the 3rd decimal place matters to you, you should round below that (say the 4th decimal place) or you will get exactly the "error" that you are seeing.

I hope this helps.

Regards,

Neil

POSTED BY: Neil Singer

Neil, I think some calculators actual use decimal rather than binary arithmetic, so that can also explain differences in behavior.

POSTED BY: Daniel Lichtblau
Posted 2 years ago

As an alternative to using Rationalize one could also round in two steps

Round[24.9/6, 0.01] // Round[#, 0.1] &
(* 4.2 *)
POSTED BY: Hans Milton

What you are seeing is the machine precision. Machine Precision for floating number is roughly 16 digits. You can do arbitrary precision in Mathematica if you need it. For example,

In[20]:= Round[Mean[Rationalize[{4.2, 5, 3, 4.1, 4.4, 4.2}]], 0.1]

Out[20]= 4.2

There are ways to deal with numerical precision depending on what you want/need. For example you can use 20 digits of precision if it makes sense. Look at SetPrecision] and Precision[] and [related functions. Also, look at this tutorial and scroll to Arbitrary Precision numbers.

Regards,

Neil

POSTED BY: Neil Singer

Thank you for your answer. I understand it.

However, I find strange that Mathematica is not able to compute properly a simple division like 24.9 / 6 without using the Rationalize function. My calculator do it without any probleme...

Regards,

Jérôme

Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract