Filter löschen
Filter löschen

question on addition of numbers.

1 Ansicht (letzte 30 Tage)
Ashwini
Ashwini am 15 Aug. 2016
Kommentiert: John D'Errico am 15 Aug. 2016
I have 2 numbers. a=11.699124064044344*2^192 and b=-17.935606820123120*2^252. adding a and b in MATLAB gives me the c=-1.2980e+77 (c=a+b). c-b gives me zero instead of 'a' value.
How to get back 'a' from 'c'

Akzeptierte Antwort

FirefoxMetzger
FirefoxMetzger am 15 Aug. 2016
The simple answer is that a double is not precise enough to represent a + b .
A double is represented using IEEE® Standard 754 . Which (very roughly) stores variables as (sign) * 1.abcdef...*10^(XYZ) where all the numbers are binary.
Representing variable a as X * 10^252 means that X has a lot of leading zeros (0.000...00001169912...). At some point this takes more then the 52 bit to represent the number, so the standard tells us to round the 53nd bit, which is 0. All leading digits are also 0. Thus a = 0 * 10^252 (due to lack of precision). Logically a + b = b if we account for this lack of precision.
To still calculate correctly concider digits which are variable precision numbers. However be advised that they might be a lot slower when computing

Weitere Antworten (1)

Azzi Abdelmalek
Azzi Abdelmalek am 15 Aug. 2016
when I run
a=11.699124064044344*2^192
b=-17.935606820123120*2^254
c=a+b
isequal(c,b)
c and b are equal, that's why c-b is 0. To understand the reason, read this http://matlab.wikia.com/wiki/FAQ#Why_is_0.3_-_0.2_-_0.1_.28or_similar.29_not_equal_to_zero.3F
  2 Kommentare
Ashwini
Ashwini am 15 Aug. 2016
Iam finding difficult to understand the concept. The content in the link says "some floating point numbers can not be represented exactly in binary form". Does that mean i cannot recover 'a' from 'c' in the above example. Please let me know if there is any method to recover 'a'. Please share the code for it if possible.
Thanks ....
John D'Errico
John D'Errico am 15 Aug. 2016
You CANNOT recover a from c, when using double precision. NEVER. A double lacks sufficient precision to do so.

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

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

Start Hunting!

Translated by