Numbers icon indicating copy to clipboard operation
Numbers copied to clipboard

ERounding.None

Open cvdevol opened this issue 3 years ago • 5 comments

Now that ERounding.None is gone, how to avoid having the last digit of a EDecimal rounded? I'm investigating repeating decimals in fractions, and it would be nice to have the EDecimal.Divide..... 'ToString' representation end in the exact last digit of the repeating sequence.

cvdevol avatar Mar 27 '22 16:03 cvdevol

ERounding.None hasn't been deprecated and is still a supported rounding mode.

peteroupc avatar Mar 27 '22 16:03 peteroupc

I get this:(in VB.NET)

Dim EC As EContext = EContext.ForPrecisionAndRounding(100, ERounding.None)

    Dim dv As EDecimal = EDecimal.FromString("1").Divide(EDecimal.FromString("23"), EC)

The result, dv, is an NaN. Shouldn't it be a 100 digit decimal with no rounding?

If I use ERounding.OddOrZeroFiveUp I get: dv = 0.04347826086956521739130434782608695652173913043478260869565217391304347826086956521739130434782608696

cvdevol avatar Mar 27 '22 17:03 cvdevol

It appears that yours is a common issue: EDecimal's Divide method with EContext.None behaves like EDecimal's Divide method with no context; see https://peteroupc.github.io/Numbers/docs/PeterO.Numbers.EDecimal.html. It returns NaN "if the result can't be exact because it would have a nonterminating decimal expansion" (as is the case for 1 divided by 3). See also #3, #8. (I'm just aware of a mixup between "decimal" and "binary" here in the documentation at the moment and will fix it.)

peteroupc avatar Mar 27 '22 17:03 peteroupc

IDK. Seems like a bug to me. The other rounding types work fine with non-terminating fractions, and know what to do with the last digit at the specified precision. I've overcome the problem by using a precision that's one greater than the length of the repeating cycle and I simply remove the last (rounded) digit. But it's clunky and requires determining the length of the repeating cycle beforehand.

cvdevol avatar Mar 27 '22 17:03 cvdevol