mathjs icon indicating copy to clipboard operation
mathjs copied to clipboard

Bug on simple rounding

Open adshrc opened this issue 2 years ago • 6 comments

The issues section is used only for bug reports. Please use the Discussions section to ask questions and share ideas and suggestions.

Describe the bug I use this library for simple rounding of currency values. Below, I want to convert 0.145 € to cents.

To Reproduce round(0.145*100) returns 14, which should be actually 15 round(evaluate('0.145 * 100')) returns also 14, which is wrong round(14.5) returns 15, which is correct

adshrc avatar Dec 07 '23 08:12 adshrc

Thanks for reporting, we should look into that! Most of the functions like equal, larger, ceil, fix do use a nearlyEqual check internally to handle round off errors like 0.145*100 = 14.499999999999998, but it looks like the function round doesn't.

josdejong avatar Dec 08 '23 07:12 josdejong

@josdejong I wanted to take this this issue.

rakheesingh avatar Dec 09 '23 18:12 rakheesingh

Thanks @rakheesingh! Please let me know when you need help (I haven't looked into this and I'm not sure what is needed to fix it).

josdejong avatar Dec 09 '23 18:12 josdejong

Hello @josdejong! Wanted to know if the issue is still open? Would love to give it a try!

developer-diganta avatar Jan 03 '24 03:01 developer-diganta

adding an epsilon fixes it:

...
// to prevent this error, add an epsilon
const epsilon = 1e-12;
return round(number + epsilon, decimals);

adshrc avatar Jan 05 '24 13:01 adshrc

The issue is still open, thanks @developer-diganta for wanting to look into it. Help would be welcome.

josdejong avatar Jan 10 '24 13:01 josdejong

Fixed now in v12.4.0 via #3136.

josdejong avatar Feb 22 '24 16:02 josdejong