FAQ
https://codereview.appspot.com/6930059/diff/11001/src/pkg/math/big/nat.go
File src/pkg/math/big/nat.go (right):

https://codereview.appspot.com/6930059/diff/11001/src/pkg/math/big/nat.go#newcode116
src/pkg/math/big/nat.go:116: return uint64(z[1]<<32 | z[0])
on 32 bits, z[1]<<32 is identically zero.

Can you test your code with GOARCH=386 ?

Also, from a very low-level point of view, adding a private method grows
the method table of nat for no particular reason.

https://codereview.appspot.com/6930059/diff/11001/src/pkg/math/big/rat.go
File src/pkg/math/big/rat.go (right):

https://codereview.appspot.com/6930059/diff/11001/src/pkg/math/big/rat.go#newcode94
src/pkg/math/big/rat.go:94: switch {
It's really not clear why you shift twice.

// Determine A' and B', such A'/B' = A/B * (1<<shift)
// is in the interval [1<<52, 1<<53).

https://codereview.appspot.com/6930059/diff/11001/src/pkg/math/big/rat.go#newcode105
src/pkg/math/big/rat.go:105: default:
// First we determine α/β where α and β are shifted values
// of A and B such that 1 <= α/β < 2.

// Here we only get 1/2 <= A/B*2^-exp < 2.
// If this is less than 1, decrement exp by 1 to get
// back in interval [1, 2)

https://codereview.appspot.com/6930059/diff/11001/src/pkg/math/big/rat.go#newcode112
src/pkg/math/big/rat.go:112: // Shift and divide.
// Now we use A'/B' = α/β * 2^52 = A/B * 2^(52-exp).
// A'/B' is in the interval [2^52, 2^53).

https://codereview.appspot.com/6930059/diff/11001/src/pkg/math/big/rat.go#newcode114
src/pkg/math/big/rat.go:114: var a2, b2 nat
please reuse a2, b2 from above.

https://codereview.appspot.com/6930059/diff/11001/src/pkg/math/big/rat.go#newcode127
src/pkg/math/big/rat.go:127: if mantissa&^(1<<52-1) != 1<<52 {
You want to check that mantissa>>52 == 1.

https://codereview.appspot.com/6930059/diff/11001/src/pkg/math/big/rat.go#newcode139
src/pkg/math/big/rat.go:139: if mantissa == 0x20000000000000 {
1<<53. I really don't want to count the zeroes.

https://codereview.appspot.com/6930059/diff/11001/src/pkg/math/big/rat.go#newcode144
src/pkg/math/big/rat.go:144:
// After rounding, q*2^-52 * 2^exp is a good approximation of A/B

https://codereview.appspot.com/6930059/diff/11001/src/pkg/math/big/rat.go#newcode151
src/pkg/math/big/rat.go:151:
I would write:

switch {
case exp > 1023:
// infinity
case exp >= -1022:
// finite
case exp < -1022-53:
// zero
default:
// denormal or zero
// q*2^(exp-52) = q*2^(exp+1022)*2^(-1022-52)
shift := uint(-1022-exp)
lostbits := mantissa & (1<<shift - 1)
mantissa >>= shift
switch {
case lostbits == 1<<(shift-1):
// halfway
mantissa &^= 1
case lostbits>>(shift-1) != 0:
// round up
mantissa++
default:
// nothing to do
}
// Now mantissa is in [0, 1<<52]
f = math.Float64frombits(mantissa) // valid even if mantissa == 1<<52
if neg {
f = -f
}
return f
}

https://codereview.appspot.com/6930059/diff/11001/src/pkg/math/big/rat.go#newcode176
src/pkg/math/big/rat.go:176: // can we tell which is closer?
I'm not sure why it would oscillate.

Denormalized/zero numbers are n * 2^(-1022-52) where n is an integer in
[0, 1<<52). The case n==1<<52 is exactly the next float64 so you just
have to multiply by 1<<1074 and round to the nearest integer (even if
the nearest is 1<<52).

And the closest float64 is well defined.

https://codereview.appspot.com/6930059/diff/11001/src/pkg/math/big/rat_test.go
File src/pkg/math/big/rat_test.go (right):

https://codereview.appspot.com/6930059/diff/11001/src/pkg/math/big/rat_test.go#newcode645
src/pkg/math/big/rat_test.go:645: // smallest denormal
Please add:
* the exact half of smallest denormal (1p-1075) -> round to zero
* a little more than the exact half of smallest denormal (let's say
1p-1075 + 1p-1100), rounding to 1p-1074
* the exact halfway between smallest normal and largest denormal:
1p-1022 - 1p-1075, rounding to 1p-1022

The decimal representation might be a bit long to write.

https://codereview.appspot.com/6930059/

Search Discussions

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-dev @
categoriesgo
postedDec 15, '12 at 12:17a
activeDec 15, '12 at 8:43p
posts3
users3
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase