Skip to content

math/big: Rat: add FloatPrec() (int, bool) #50489

@mitar

Description

@mitar

Update, Nov 13, 2023: Current API is at #50489 (comment). -gri


Currently if I parse string like 123.34 with SetString() and want to print it back to full precision, one has to manually pick the right precision for FloatString() function to get that. I propose that instead FloatString() should accept negative prec parameter. The full semantics of prec parameter would then be:

  • For positive prec, it formats with prec digits after the radix point, padding with 0 on the right if necessary. The last digit is rounded to nearest, with halves rounded away from zero, if rounding is necessary.
  • When prec is 0, there are no digits after the radix point.
  • If prec is less than 0, then:
    • If there is a finite number of digits after the radix point to precisely represent the number, all these digits are appended after the radix point.
    • If the number of digits is infinite, then abs(prec) number of digits is used after the radix point, rounded the last digit to nearest, with halves rounded away from zero.

So FloatString(3) for the number above returns 123.340, but FloatString(-3) return 123.34.

big.NewRat(1, 3).FloatString(3) returns 0.333 and big.NewRat(1, 3).FloatString(-3) would return 0.333.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions