Unexpected floor() function result?
15 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
>> (1.2-floor(1.2))*10
ans =
2.0000
>> floor((1.2-floor(1.2))*10)
ans =
1
Have I misunderstood how the floor function interacts with other math operations?
2 commentaires
Réponse acceptée
Adam Danz
le 17 Mai 2021
Modifié(e) : Adam Danz
le 17 Mai 2021
This is round-off error and is caused by limitation to representing floating point decimals.
1.2-floor(1.2)
1.2-floor(1.2) - 0.2000 % Reveal round-off error
Likewise,
(1.2-floor(1.2))*10
(1.2-floor(1.2))*10 - 2.000 % Reveal round-off error
Here's another example,
4/3
4/3-1
(4/3 - 1)*3
(4/3 - 1)*3 - 1 % Reveal round-off error
See this answer for a list of references to learn more about round-off error and floating point representation. If there's a specific issue you need to solve to avoid the problem, add some details and I may be able to point you in the right direction.
0 commentaires
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Logical dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!