How to subtract two close numbers?
2 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
kamiR fardi
il 16 Ott 2019
Commentato: kamiR fardi
il 16 Ott 2019
Hi!
I've compiled the code below and i don't know why it doesn't give exact zero.
I'll appreciate if someone helps me.
format long
x=5.4;
y=x-5;
z=y-0.4;
disp(z)
0 Commenti
Risposta accettata
Stephen23
il 16 Ott 2019
Modificato: Stephen23
il 16 Ott 2019
"..i don't know why it doesn't give exact zero."
The decimal values 5.4 and 0.4 cannot be represented exactly using binary floating point numbers, in just the same way that you cannot write 1/3 using a finite decimal fraction.
Use num2strexact see the precise decimal values of the closest floating point binary numbers:
The behaviors of floating point numbers are quite well documented, you need to learn how to write your code taking them into account. Start by reading these:
etc.etc.
This is worth reading as well:
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Creating and Concatenating Matrices in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!