Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Floating Point operations and mistakes

Subject: Floating Point operations and mistakes

From: Roberto Gennari

Date: 25 Jun, 2010 21:49:05

Message: 1 of 3

Hi everyone.
I'm using Matlab since 4 years at my University and this is the first time I post here.
I was programming with Matlab just for solving a puzzle (in particular, looking for three numbers that have tha same product and sum in the interval 0.99-9.99) when I noticed this fact:
the sum of the three floating points number 1.25+1.62+2.8 is correctly 5.670000000000000 (using format long), and the product of the same numbers (in the same order (1.25*1.62*2.8) is 5.670000000000001. The problem is that if you write the product in this order 2.8*1.25*1.62 now you get 5.670000000000000 !!
This happens even with other permutations of the three numbers and it is has been a pain for me because it fails the if control ( actually matlab returns 0 if you ask 1.25+1.62+2.8==1.25*1.62*2.8 but it returns 1 with 1.25+1.62+2.8==2.8*1.25*1.62 !)
Does anybody know why this happens? I have already "solved" the problem rounding the numbers, but I really want to understand why!!

Thank you.

Roberto Gennari
Ferrara, Italy

Subject: Floating Point operations and mistakes

From: Matt Fig

Date: 25 Jun, 2010 22:04:22

Message: 2 of 3

http://matlabwiki.mathworks.com/MATLAB_FAQ#Why_is_0.3-0.2-0.1_not_equal_to_zero_.28or_similar.29.3F

Subject: Floating Point operations and mistakes

From: James Tursa

Date: 25 Jun, 2010 22:34:05

Message: 3 of 3

"Roberto Gennari" <gennari.roberto@gmail.com> wrote in message <i0388h$ikh$1@fred.mathworks.com>...
> Hi everyone.
> I'm using Matlab since 4 years at my University and this is the first time I post here.
> I was programming with Matlab just for solving a puzzle (in particular, looking for three numbers that have tha same product and sum in the interval 0.99-9.99) when I noticed this fact:
> the sum of the three floating points number 1.25+1.62+2.8 is correctly 5.670000000000000 (using format long), and the product of the same numbers (in the same order (1.25*1.62*2.8) is 5.670000000000001. The problem is that if you write the product in this order 2.8*1.25*1.62 now you get 5.670000000000000 !!
> This happens even with other permutations of the three numbers and it is has been a pain for me because it fails the if control ( actually matlab returns 0 if you ask 1.25+1.62+2.8==1.25*1.62*2.8 but it returns 1 with 1.25+1.62+2.8==2.8*1.25*1.62 !)
> Does anybody know why this happens? I have already "solved" the problem rounding the numbers, but I really want to understand why!!

To see the exact numbers for your particular example:

>> num2strexact(1.25)
ans =
1.25
>> num2strexact(1.62)
ans =
1.62000000000000010658141036401502788066864013671875
>> num2strexact(2.8)
ans =
2.79999999999999982236431605997495353221893310546875
>> num2strexact(1.25+1.62+2.8)
ans =
5.6699999999999999289457264239899814128875732421875
>> num2strexact(1.25*1.62*2.8)
ans =
5.67000000000000081712414612411521375179290771484375
>> num2strexact(2.8*1.25*1.62)
ans =
5.6699999999999999289457264239899814128875732421875


You can get num2strexact here:

http://www.mathworks.com/matlabcentral/fileexchange/22239-num2strexact-exact-version-of-num2str

James Tursa

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us