MATLAB double addition format long

21 visualizaciones (últimos 30 días)
vivek patel
vivek patel el 3 de Nov. de 2022
Editada: Stephen23 el 3 de Nov. de 2022
Why does the below addition show wrong output when i use the format long option ?
  1 comentario
Stephen23
Stephen23 el 3 de Nov. de 2022
Editada: Stephen23 el 3 de Nov. de 2022
"Why does the below addition show wrong output when i use the format long option ?"
Why do you think that it is a "wrong output" ?
Can you show us any double floating point number that is closer to the value 14.2?
format hex
14.2
ans =
402c666666666666
format long
14.2
ans =
14.199999999999999
N = hex2num({'402c666666666665','402c666666666666','402c666666666667'})
N = 3×1
14.199999999999998 14.199999999999999 14.200000000000001
fprintf('%.50f\n',N)
14.19999999999999751310042483964934945106506347656250 14.19999999999999928945726423989981412887573242187500 14.20000000000000106581410364015027880668640136718750

Iniciar sesión para comentar.

Respuesta aceptada

Davide Masiello
Davide Masiello el 3 de Nov. de 2022

Más respuestas (1)

VBBV
VBBV el 3 de Nov. de 2022
format long
x = 14.1 %
x =
14.100000000000000
x = 0.1
x =
0.100000000000000
x = 14.1 + 0.1 % when you include + operator
x =
14.199999999999999
When a mathematical operator /computation is used between any two numbers, Matlab uses its floating point arithmetic precision to compute precisely, Notice the difference in outputs when individual numbers are present without operators !

Categorías

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

Community Treasure Hunt

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

Start Hunting!

Translated by