Filter löschen
Filter löschen

Error during multiplication (r2009b)

2 Ansichten (letzte 30 Tage)
Praneeth Namburi
Praneeth Namburi am 20 Aug. 2011
Hi all, I am using MATLAB r2009b and when I type the following at the command prompt, I get something very unexpected...
19*2.2 == 41.8
I get an answer of 0, when the answer should be 1! Upon typing 19*2.2 and inspecting the answer, I get 41.800000000000004.
Moreover, mod(41.8, 2.2) gives the correct answer, 0, but mod(41.8/2.2, 1) gives the wrong answer, 0.999999999999996.
Kindly let me know if this is a known issue that has already been discussed and rectified and if my friend discovered just a specific case of this issue.
Thanks in advance for your time!
- Praneeth

Akzeptierte Antwort

Jan
Jan am 20 Aug. 2011
Welcome to the world of floating point arithmetics with limited precision. Numbers are represented as DOUBLE using 64 bits. In consequence not all numbers can be represented exactly, and you found out (like thousands of others users before), that 19*2.2 has a slightly different representation as 41.8. See also: FAQ: Why is 0.3-0.2~=0.1.
BTW. it is worth to read the rest of the FAQ also, because it contains the frequent standard problems of other MATLAB beginners.
  1 Kommentar
Praneeth Namburi
Praneeth Namburi am 20 Aug. 2011
Hi Jan, Thank you for your explanation and the link!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Introduction to Installation and Licensing finden Sie in Help Center und File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by