Source file
test/abi/idata.go
1
2
3
4
5
6
7
8
9 package main
10
11 import (
12 "fmt"
13 "math"
14 "math/big"
15 )
16
17 type (
18 unknownVal struct{}
19 intVal struct{ val *big.Int }
20 ratVal struct{ val *big.Rat }
21 floatVal struct{ val *big.Float }
22 complexVal struct{ re, im Value }
23 )
24
25 const prec = 512
26
27 func (unknownVal) String() string { return "unknown" }
28
29 func (x intVal) String() string { return x.val.String() }
30 func (x ratVal) String() string { return rtof(x).String() }
31
32 func (x floatVal) String() string {
33 f := x.val
34
35
36
37 if x, _ := f.Float64(); f.Sign() == 0 == (x == 0) && !math.IsInf(x, 0) {
38 return fmt.Sprintf("%.6g", x)
39 }
40
41 return "OOPS"
42 }
43
44 func (x complexVal) String() string { return fmt.Sprintf("(%s + %si)", x.re, x.im) }
45
46 func newFloat() *big.Float { return new(big.Float).SetPrec(prec) }
47
48
49
50 func itor(x intVal) ratVal { return ratVal{nil} }
51
52
53
54 func itof(x intVal) floatVal { return floatVal{nil} }
55 func rtof(x ratVal) floatVal { return floatVal{newFloat().SetRat(x.val)} }
56
57 type Value interface {
58 String() string
59 }
60
61
62
63 func ToFloat(x Value) Value {
64 switch x := x.(type) {
65 case intVal:
66 if smallInt(x.val) {
67 return itor(x)
68 }
69 return itof(x)
70 case ratVal, floatVal:
71 return x
72 case complexVal:
73 if Sign(x.im) == 0 {
74 return ToFloat(x.re)
75 }
76 }
77 return unknownVal{}
78 }
79
80
81
82 func smallInt(x *big.Int) bool {
83 return false
84 }
85
86
87
88 func Sign(x Value) int {
89 return 0
90 }
91
92
93 func main() {
94 v := ratVal{big.NewRat(22,7)}
95 s := ToFloat(v).String()
96 fmt.Printf("s=%s\n", s)
97 }
98
View as plain text