Skip to content

PreciseNumber.Radix returns 2, but the type is base 10 (significand × 10^exponent) #116

Description

@matt-edmondson

What's wrong

PreciseNumber/PreciseNumber.cs:437-440:

private const int BinaryRadix = 2;
/// <inheritdoc/>
public static int Radix => BinaryRadix;

INumberBase<T>.Radix is defined as "the radix, or base, for the type". PreciseNumber stores Significand × 10^Exponent and does all its digit work (significant digits, rounding, Round(decimalDigits), the rendered form) in base 10. The BCL's other decimal type, decimal, reports Radix == 10.

PreciseNumber.Test/PreciseNumberTests.cs:906-908 (TestRadix) asserts 2, so the test pins the wrong value. It does not document a design decision.

Why it matters

Generic math code uses T.Radix to reason about representation. Examples include computing ulps, deciding how many digits a value can hold exactly, choosing a digit-by-digit algorithm, or checking whether scaling by the radix is exact. With Radix == 2, such code treats PreciseNumber as binary. For example, it would conclude that x * 10 is not an exact scale, while x * 2 is.

Suggested fix

  • Return 10, and rename the constant (e.g. DecimalRadix).
  • Update TestRadix to expect 10.
  • This is a behaviour change to a public static, so note it in the changelog.

Acceptance criteria

  • PreciseNumber.Radix == 10 == decimal.Radix.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

bugSomething isn't working

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions