def Decimal.frac.go

bend-kit-bignum@0.1.0.0 · decimal.bend:102

import bend-kit-bignum@0.1.0.0/decimal.bend as Decimal
import Base
import bend-kit-bignum@0.1.0.0/bigint.bend as Big

102# Each step divides d by 2 or 5 and scales n to match, so a d of D digits
103# needs fewer than 4D steps; f is that bound.
104def Decimal.frac.go(f: Nat, n: Big.BigInt, k: Nat, one: Bool,
105  t2: Bool & Big.BigInt, t5: Bool & Big.BigInt) -> Maybe<&2, Decimal>:
106  match f:
107    case 0n:
108      None{}
109    case 1n+g:
110      match one:
111        case True{}:
112          Some{Decimal{n, k}}
113        case False{}:
114          (z2, q2) = t2
115          match z2:
116            case True{}:
117              +d = {q2 : Big.BigInt}
118              Decimal.frac.go(g, Big.BigInt.mul(n, Big.BigInt.from_u32(5)),
119                1n+k, Decimal.is_one(d), Decimal.divk(d, 2),
120                Decimal.divk(d, 5))
121            case False{}:
122              (z5, q5) = t5
123              match z5:
124                case True{}:
125                  +d = {q5 : Big.BigInt}
126                  Decimal.frac.go(g, Big.BigInt.mul(n, Big.BigInt.from_u32(2)),
127                    1n+k, Decimal.is_one(d), Decimal.divk(d, 2),
128                    Decimal.divk(d, 5))
129                case False{}:
130                  None{}

Uses (11)

Used by (1)