cmpute / dashu

A library set of arbitrary precision numbers implemented in Rust.
Apache License 2.0
74 stars 9 forks source link

round mode HalfUp #36

Closed faassen closed 6 months ago

faassen commented 10 months ago

I'm trying to implement this behavior with Dashu:

https://www.w3.org/TR/xpath-functions-31/#func-round

If two such values are equally near (for example, if the fractional part in $arg is exactly .5), the function returns the one that is closest to positive infinity."

While dashu implements a rounding mode HalfAway, I think this requires HalfUp. Alternatively I take the absolute value, use HalfAway, and then reapply the sign, but that seems cumbersome.

cmpute commented 10 months ago

I don't plan to add that rounding mode to Dashu now, because this rounding mode is rather rarely used. You can implement the rounding mode in your application, because the requirement for the rounding mode is just to implement the Round trait.

Please refer to https://github.com/cmpute/dashu/blob/4329c5cc67fa9798924888bb5cdd495dba96e566/float/src/round.rs#L288-L321 for an implementation example.

faassen commented 10 months ago

Thanks for the tip! Given that the rounding mode is rarely used, I now wonder how this spec ended up with it - seems a strange one to pick.

cmpute commented 10 months ago

I have the same question... It's so counter-intuitive