function rem issue with exact result

9 vues (au cours des 30 derniers jours)
shahriar memaran
shahriar memaran le 27 Fév 2017
Modifié(e) : Stephen23 le 27 Fév 2017
sometimes the function rem does not return the exact solution. For example rem(135.01,100) does not return 35.01 but it returns 35.009999999999991 (visible when format is changed to long). Can anyone tel me why !? and how to fix it !?
  1 commentaire
Stephen23
Stephen23 le 27 Fév 2017
Modifié(e) : Stephen23 le 27 Fév 2017
The decimal value 135.01 cannot be stored exactly in binary, just in the same way that 1/3 cannot be written exactly using decimal digits. Have a look at it in more detail:
>> fprintf('%.30f\n',135.01)
135.009999999999990000000000000000
Read Steven Lord's answer, it tells you what is going on.

Connectez-vous pour commenter.

Réponse acceptée

Steven Lord
Steven Lord le 27 Fév 2017

Plus de réponses (0)

Catégories

En savoir plus sur Mathematics dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by