Skip to content

Commit

Permalink
math/big: fix comment in divRecursiveStep
Browse files Browse the repository at this point in the history
There appears to be a typo in the description of
the recursive division algorithm.

Two things seem suspicious with the original comment:
  1. It is talking about choosing s, but s doesn't
     appear anywhere in the equation.
  2. The math in the equation is incorrect.

Where
  B = len(v)/2
  s = B - 1

Proof that it is incorrect:
    len(v) - B >= B + 1
    len(v) - len(v)/2 >= len(v)/2 + 1

    This doesn't hold if len(v) is even, e.g. 10:
    10 - 10/2 >= 10/2 + 1
    10 - 5 >= 5 + 1
    5 >= 6  // this is false

The new equation will be the following,
which will be mathematically correct:
    len(v) - s >= B + 1
    len(v) - (len(v)/2 - 1) >= len(v)/2 + 1
    len(v) - len(v)/2 + 1 >= len(v)/2 + 1
    len(v) - len(v)/2 >= len(v)/2

    This holds if len(v) is even or odd.

    e.g. 10
    10 - 10/2 >= 10/2
    10 - 5 >= 5
    5 >= 5

    e.g. 11
    11 - 11/2 >= 11/2
    11 - 5 >= 5
    6 >= 5

Change-Id: If77ce09286cf7038637b5dfd0fb7d4f828023f56
Reviewed-on: https://go-review.googlesource.com/c/go/+/287372
Run-TryBot: Katie Hockman <katie@golang.org>
Reviewed-by: Filippo Valsorda <filippo@golang.org>
Trust: Katie Hockman <katie@golang.org>
  • Loading branch information
katiehockman committed Feb 3, 2021
1 parent fca94ab commit e491c6e
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion src/math/big/nat.go
Original file line number Diff line number Diff line change
Expand Up @@ -881,7 +881,7 @@ func (z nat) divRecursiveStep(u, v nat, depth int, tmp *nat, temps []*nat) {
// then floor(u1/v1) >= floor(u/v)
//
// Moreover, the difference is at most 2 if len(v1) >= len(u/v)
// We choose s = B-1 since len(v)-B >= B+1 >= len(u/v)
// We choose s = B-1 since len(v)-s >= B+1 >= len(u/v)
s := (B - 1)
// Except for the first step, the top bits are always
// a division remainder, so the quotient length is <= n.
Expand Down

0 comments on commit e491c6e

Please sign in to comment.