Why 0.6+0.3+0.1-1 is -1.11022302462516e-16, not 0 ? why?? A serials bug??

8 visualizaciones (últimos 30 días)
denny
denny el 20 de Jun. de 2018
Respondida: cdarling el 4 de Jun. de 2025
0.6+0.3+0.1-1
  1 comentario
denny
denny el 9 de Sept. de 2019
Editada: denny el 26 de Feb. de 2025
This is a bug? this is bucase the numeric calculation in a computer.
You can use a fraction to calculate it.

Iniciar sesión para comentar.

Respuestas (1)

cdarling
cdarling el 4 de Jun. de 2025
As a numerical calculation software, MATLAB stores data as double data type by default.
IEEE-754 is the standard for double data type.
This may better explained by the following code:
>> num2hex(0.1)
ans =
'3fb999999999999a'
>> num2hex(0.1+0.1+0.1)
ans =
'3fd3333333333334'
>> num2hex(0.3)
ans =
'3fd3333333333333'
If you need to calculate the theoretical value, you may try Symbolic Math Toolbox:
sym('0.6')+sym('0.3')+sym('0.1')-sym('1')

Categorías

Más información sobre 建模 en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!