dentaku
dentaku copied to clipboard
ROUND does not work properly for inline floats
Given rule like that: ROUND(1.23456789, 3)
executing Dentaku like so:
Dentaku::Calculator.new.evaluate!('ROUND(1.23456789, 3)') #=> 0.1235e1
what is surprising is the argument is not an inline:
Dentaku::Calculator.new.evaluate!('ROUND(nr, 3)', nr: 1.2345678) #=> 1.235
ROUNDUP and ROUNDDOWN work as expected.
When parsing numeric literals, the value will either become an integer or a BigDecimal (see token_scanner.rb:94), and rounding a BigDecimal still returns a BigDecimal. On the other hand, context values are used as-is without any conversion, so the second result is still a float. ROUNDUP/DOWN use floor or ceil on the scaled value and then divide by a float, so they will always coerce to float.
Maybe it would be more consistent to always force numeric functions to return integer or BigDecimal?
Maybe it would be more consistent to always force numeric functions to return integer or BigDecimal?
BigDecimal would be much better and much more natural imho.
Not rushing but any changes here? :)
Not so far, but I'll try to take a look at this soon.
Thank you. Don't hesitate if you need any help!