Skip to content

Commit 3c33606

Browse files
committed
Revise the total precision bits of the big.Floats to be closer to the amounts used in C++ exact_float (from billions down to millions)
1 parent 438f30b commit 3c33606

File tree

2 files changed

+19
-11
lines changed

2 files changed

+19
-11
lines changed

r3/precisevector.go

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,18 @@ import (
2020
)
2121

2222
const (
23-
// prec is the number of bits of precision to use for the Float values.
24-
// To keep things simple, we use the maximum allowable precision on big
25-
// values. This allows us to handle all values we expect in the s2 library.
26-
prec = big.MaxPrec
23+
// MaxPrec is the number of bits of precision to use for the Float values.
24+
// To keep things simple, we match the limit used in the C++ library.
25+
// This allows us to handle all values we expect in the s2 library.
26+
MaxPrec = 64 << 20 // see C++'s util/math/exactfloat/exactfloat.h
27+
28+
// MaxExp is the maximum exponent supported. If a value has an exponent larger than
29+
// this, it is replaced by infinity (with the appropriate sign).
30+
MaxExp = 200 * 1000 * 1000 // About 10**(60 million)
31+
32+
// MinExp is the minimum exponent supported. If a value has an exponent less than
33+
// this, it is replaced by zero (with the appropriate sign).
34+
MinExp = -MaxExp // About 10**(-60 million)
2735
)
2836

2937
// define some commonly referenced values.
@@ -37,28 +45,28 @@ var (
3745
// are integer multiples of integer powers of 2.
3846
func precStr(s string) *big.Float {
3947
// Explicitly ignoring the bool return for this usage.
40-
f, _ := new(big.Float).SetPrec(prec).SetString(s)
48+
f, _ := new(big.Float).SetPrec(MaxPrec).SetString(s)
4149
return f
4250
}
4351

4452
func precInt(i int64) *big.Float {
45-
return new(big.Float).SetPrec(prec).SetInt64(i)
53+
return new(big.Float).SetPrec(MaxPrec).SetInt64(i)
4654
}
4755

4856
func precFloat(f float64) *big.Float {
49-
return new(big.Float).SetPrec(prec).SetFloat64(f)
57+
return new(big.Float).SetPrec(MaxPrec).SetFloat64(f)
5058
}
5159

5260
func precAdd(a, b *big.Float) *big.Float {
53-
return new(big.Float).SetPrec(prec).Add(a, b)
61+
return new(big.Float).SetPrec(MaxPrec).Add(a, b)
5462
}
5563

5664
func precSub(a, b *big.Float) *big.Float {
57-
return new(big.Float).SetPrec(prec).Sub(a, b)
65+
return new(big.Float).SetPrec(MaxPrec).Sub(a, b)
5866
}
5967

6068
func precMul(a, b *big.Float) *big.Float {
61-
return new(big.Float).SetPrec(prec).Mul(a, b)
69+
return new(big.Float).SetPrec(MaxPrec).Mul(a, b)
6270
}
6371

6472
// PreciseVector represents a point in ℝ³ using high-precision values.

r3/precisevector_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import (
2121

2222
// preciseEq compares two big.Floats and checks if the are the same.
2323
func preciseEq(a, b *big.Float) bool {
24-
return a.SetPrec(prec).Cmp(b.SetPrec(prec)) == 0
24+
return a.SetPrec(MaxPrec).Cmp(b.SetPrec(MaxPrec)) == 0
2525
}
2626

2727
func TestPreciseRoundtrip(t *testing.T) {

0 commit comments

Comments
 (0)