| | |
| | |
| | |
| |
|
| | |
| | |
| | |
| |
|
| | package big |
| |
|
| | import ( |
| | "fmt" |
| | "slices" |
| | "testing" |
| | ) |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | type Bits []int |
| |
|
| | func (x Bits) add(y Bits) Bits { |
| | return append(x, y...) |
| | } |
| |
|
| | func (x Bits) mul(y Bits) Bits { |
| | var p Bits |
| | for _, x := range x { |
| | for _, y := range y { |
| | p = append(p, x+y) |
| | } |
| | } |
| | return p |
| | } |
| |
|
| | func TestMulBits(t *testing.T) { |
| | for _, test := range []struct { |
| | x, y, want Bits |
| | }{ |
| | {nil, nil, nil}, |
| | {Bits{}, Bits{}, nil}, |
| | {Bits{0}, Bits{0}, Bits{0}}, |
| | {Bits{0}, Bits{1}, Bits{1}}, |
| | {Bits{1}, Bits{1, 2, 3}, Bits{2, 3, 4}}, |
| | {Bits{-1}, Bits{1}, Bits{0}}, |
| | {Bits{-10, -1, 0, 1, 10}, Bits{1, 2, 3}, Bits{-9, -8, -7, 0, 1, 2, 1, 2, 3, 2, 3, 4, 11, 12, 13}}, |
| | } { |
| | got := fmt.Sprintf("%v", test.x.mul(test.y)) |
| | want := fmt.Sprintf("%v", test.want) |
| | if got != want { |
| | t.Errorf("%v * %v = %s; want %s", test.x, test.y, got, want) |
| | } |
| |
|
| | } |
| | } |
| |
|
| | |
| | |
| | |
| | func (x Bits) norm() Bits { |
| | m := make(map[int]bool) |
| | for _, b := range x { |
| | for m[b] { |
| | m[b] = false |
| | b++ |
| | } |
| | m[b] = true |
| | } |
| | var z Bits |
| | for b, set := range m { |
| | if set { |
| | z = append(z, b) |
| | } |
| | } |
| | slices.Sort([]int(z)) |
| | return z |
| | } |
| |
|
| | func TestNormBits(t *testing.T) { |
| | for _, test := range []struct { |
| | x, want Bits |
| | }{ |
| | {nil, nil}, |
| | {Bits{}, Bits{}}, |
| | {Bits{0}, Bits{0}}, |
| | {Bits{0, 0}, Bits{1}}, |
| | {Bits{3, 1, 1}, Bits{2, 3}}, |
| | {Bits{10, 9, 8, 7, 6, 6}, Bits{11}}, |
| | } { |
| | got := fmt.Sprintf("%v", test.x.norm()) |
| | want := fmt.Sprintf("%v", test.want) |
| | if got != want { |
| | t.Errorf("normBits(%v) = %s; want %s", test.x, got, want) |
| | } |
| |
|
| | } |
| | } |
| |
|
| | |
| | |
| | func (x Bits) round(prec uint, mode RoundingMode) *Float { |
| | x = x.norm() |
| |
|
| | |
| | var min, max int |
| | for i, b := range x { |
| | if i == 0 || b < min { |
| | min = b |
| | } |
| | if i == 0 || b > max { |
| | max = b |
| | } |
| | } |
| | prec0 := uint(max + 1 - min) |
| | if prec >= prec0 { |
| | return x.Float() |
| | } |
| | |
| |
|
| | |
| | var bit0, rbit, sbit uint |
| | var z Bits |
| | r := max - int(prec) |
| | for _, b := range x { |
| | switch { |
| | case b == r: |
| | rbit = 1 |
| | case b < r: |
| | sbit = 1 |
| | default: |
| | |
| | if b == r+1 { |
| | bit0 = 1 |
| | } |
| | z = append(z, b) |
| | } |
| | } |
| |
|
| | |
| | f := z.Float() |
| | if mode == ToNearestAway { |
| | panic("not yet implemented") |
| | } |
| | if mode == ToNearestEven && rbit == 1 && (sbit == 1 || sbit == 0 && bit0 != 0) || mode == AwayFromZero { |
| | |
| | f.SetMode(ToZero).SetPrec(prec) |
| | f.Add(f, Bits{int(r) + 1}.Float()) |
| | } |
| | return f |
| | } |
| |
|
| | |
| | |
| | |
| | func (bits Bits) Float() *Float { |
| | |
| | if len(bits) == 0 { |
| | return new(Float) |
| | } |
| | |
| |
|
| | |
| | var min int |
| | for i, b := range bits { |
| | if i == 0 || b < min { |
| | min = b |
| | } |
| | } |
| |
|
| | |
| | x := NewInt(0) |
| | for _, b := range bits { |
| | badj := b - min |
| | |
| | for x.Bit(badj) != 0 { |
| | x.SetBit(x, badj, 0) |
| | badj++ |
| | } |
| | x.SetBit(x, badj, 1) |
| | } |
| |
|
| | |
| | z := new(Float).SetInt(x) |
| | if e := int64(z.exp) + int64(min); MinExp <= e && e <= MaxExp { |
| | z.exp = int32(e) |
| | } else { |
| | |
| | panic("exponent out of range") |
| | } |
| | return z |
| | } |
| |
|
| | func TestFromBits(t *testing.T) { |
| | for _, test := range []struct { |
| | bits Bits |
| | want string |
| | }{ |
| | |
| | {nil, "0"}, |
| | {Bits{0}, "0x.8p+1"}, |
| | {Bits{1}, "0x.8p+2"}, |
| | {Bits{-1}, "0x.8p+0"}, |
| | {Bits{63}, "0x.8p+64"}, |
| | {Bits{33, -30}, "0x.8000000000000001p+34"}, |
| | {Bits{255, 0}, "0x.8000000000000000000000000000000000000000000000000000000000000001p+256"}, |
| |
|
| | |
| | {Bits{0, 0}, "0x.8p+2"}, |
| | {Bits{0, 0, 0, 0}, "0x.8p+3"}, |
| | {Bits{0, 1, 0}, "0x.8p+3"}, |
| | {append(Bits{2, 1, 0} , Bits{3, 1} ...), "0x.88p+5" }, |
| | } { |
| | f := test.bits.Float() |
| | if got := f.Text('p', 0); got != test.want { |
| | t.Errorf("setBits(%v) = %s; want %s", test.bits, got, test.want) |
| | } |
| | } |
| | } |
| |
|