Reputation: 6852
>> a = 12.5 * 9.45
a =
1.181250000000000e+02
>> round(a * 100) /100
ans =
1.181200000000000e+02
The rounded value should be 118.13, not 118.12.
If you type 9.45 in MatLab command line, it can't be represented :
>> 9.45
ans =
9.449999999999999
If I set the numeric format to short, the final result is the same.
>> a = 12.5 * 9.45
a =
118.1250
>> round(a * 100) / 100
ans =
118.1200
Can someone explain that? Any workaround?
Upvotes: 0
Views: 144
Reputation: 7423
You could try something like John D'Errico's hpf class.
This will give the result you are expecting
round(hpf('12.5') * hpf('9.45') * 100)/100
ans =
118.13
F = hpf('9.45')
F =
9.45
Upvotes: 3
Reputation: 8511
Sounds like you've got some settings that are messing with your output precision. It looks like your current settings want 15 decimal places(possibly longG
)?, and with MATLABs floating point numbers you won't get 100% precision that far.
You can change the format of your display output, but the precision is going to be an issue to that level regardless. The relevant documentation.
Upvotes: 0