-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.go
57 lines (46 loc) · 1.1 KB
/
util.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package godelbrot
import (
"log"
"math/big"
)
// Parse a big.Float
func parseBig(number string) (*big.Float, error) {
bits := digits2bits(uint(len(number)))
f, _, err := big.ParseFloat(number, DefaultBase, bits, big.ToNearestEven)
return f, err
}
func emitBig(b *big.Float) string {
digits := bits2digits(b.MinPrec())
return b.Text('e', int(digits))
}
const maxi64 = ^uint64(0) >> 1
// Returns number of bits given number of digits
func digits2bits(digits uint) uint {
if uint64(digits) > maxi64 {
log.Panic("int64 would overflow")
}
bits := big.NewInt(int64(digits))
two := big.NewInt(2)
ten := big.NewInt(10)
bits.Exp(ten, bits, nil)
count := uint(1)
for ; bits.Cmp(two) != -1; count++ {
bits.Div(bits, two)
}
return count
}
// Returns number of digits given number of bits
func bits2digits(bits uint) uint {
if uint64(bits) > maxi64 {
log.Panic("int64 would overflow")
}
digits := big.NewInt(int64(bits))
two := big.NewInt(2)
ten := big.NewInt(10)
digits.Exp(two, digits, nil)
count := uint(1)
for ; digits.Cmp(ten) != -1; count++ {
digits.Div(digits, ten)
}
return count
}