From db0d553813c41be7d156e35410f44f32c9c6f6e4 Mon Sep 17 00:00:00 2001 From: Daniil Lashin Date: Thu, 12 Jul 2018 16:12:52 +0300 Subject: [PATCH] prettify code --- math/exp.go | 2 + math/integer.go | 99 ------------------------------------------------- math/log.go | 2 + math/misc.go | 2 + math/pow.go | 34 +---------------- 5 files changed, 8 insertions(+), 131 deletions(-) delete mode 100644 math/integer.go diff --git a/math/exp.go b/math/exp.go index 92e8f6dc0..e02e1dc33 100644 --- a/math/exp.go +++ b/math/exp.go @@ -5,6 +5,8 @@ import ( "math/big" ) +// https://github.com/ALTree/bigfloat + // Exp returns a big.Float representation of exp(z). Precision is // the same as the one of the argument. The function returns +Inf // when z = +Inf, and 0 when z = -Inf. diff --git a/math/integer.go b/math/integer.go deleted file mode 100644 index 7eff4d3b0..000000000 --- a/math/integer.go +++ /dev/null @@ -1,99 +0,0 @@ -// Copyright 2017 The go-ethereum Authors -// This file is part of the go-ethereum library. -// -// The go-ethereum library is free software: you can redistribute it and/or modify -// it under the terms of the GNU Lesser General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// The go-ethereum library is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU Lesser General Public License for more details. -// -// You should have received a copy of the GNU Lesser General Public License -// along with the go-ethereum library. If not, see . - -package math - -import ( - "fmt" - "strconv" -) - -const ( - // Integer limit values. - MaxInt8 = 1<<7 - 1 - MinInt8 = -1 << 7 - MaxInt16 = 1<<15 - 1 - MinInt16 = -1 << 15 - MaxInt32 = 1<<31 - 1 - MinInt32 = -1 << 31 - MaxInt64 = 1<<63 - 1 - MinInt64 = -1 << 63 - MaxUint8 = 1<<8 - 1 - MaxUint16 = 1<<16 - 1 - MaxUint32 = 1<<32 - 1 - MaxUint64 = 1<<64 - 1 -) - -// HexOrDecimal64 marshals uint64 as hex or decimal. -type HexOrDecimal64 uint64 - -// UnmarshalText implements encoding.TextUnmarshaler. -func (i *HexOrDecimal64) UnmarshalText(input []byte) error { - int, ok := ParseUint64(string(input)) - if !ok { - return fmt.Errorf("invalid hex or decimal integer %q", input) - } - *i = HexOrDecimal64(int) - return nil -} - -// MarshalText implements encoding.TextMarshaler. -func (i HexOrDecimal64) MarshalText() ([]byte, error) { - return []byte(fmt.Sprintf("%#x", uint64(i))), nil -} - -// ParseUint64 parses s as an integer in decimal or hexadecimal syntax. -// Leading zeros are accepted. The empty string parses as zero. -func ParseUint64(s string) (uint64, bool) { - if s == "" { - return 0, true - } - if len(s) >= 2 && (s[:2] == "0x" || s[:2] == "0X") { - v, err := strconv.ParseUint(s[2:], 16, 64) - return v, err == nil - } - v, err := strconv.ParseUint(s, 10, 64) - return v, err == nil -} - -// MustParseUint64 parses s as an integer and panics if the string is invalid. -func MustParseUint64(s string) uint64 { - v, ok := ParseUint64(s) - if !ok { - panic("invalid unsigned 64 bit integer: " + s) - } - return v -} - -// NOTE: The following methods need to be optimised using either bit checking or asm - -// SafeSub returns subtraction result and whether overflow occurred. -func SafeSub(x, y uint64) (uint64, bool) { - return x - y, x < y -} - -// SafeAdd returns the result and whether overflow occurred. -func SafeAdd(x, y uint64) (uint64, bool) { - return x + y, y > MaxUint64-x -} - -// SafeMul returns multiplication result and whether overflow occurred. -func SafeMul(x, y uint64) (uint64, bool) { - if x == 0 || y == 0 { - return 0, false - } - return x * y, y > MaxUint64/x -} diff --git a/math/log.go b/math/log.go index de8d97f5f..b803d1a8e 100644 --- a/math/log.go +++ b/math/log.go @@ -5,6 +5,8 @@ import ( "math/big" ) +// https://github.com/ALTree/bigfloat + // Log returns a big.Float representation of the natural logarithm of // z. Precision is the same as the one of the argument. The function // panics if z is negative, returns -Inf when z = 0, and +Inf when z = diff --git a/math/misc.go b/math/misc.go index 49f1c0e77..0ffc96468 100644 --- a/math/misc.go +++ b/math/misc.go @@ -2,6 +2,8 @@ package math import "math/big" +// https://github.com/ALTree/bigfloat + // agm returns the arithmetic-geometric mean of a and b. // a and b must have the same precision. func agm(a, b *big.Float) *big.Float { diff --git a/math/pow.go b/math/pow.go index 9c5613a7f..1ea0c6a21 100644 --- a/math/pow.go +++ b/math/pow.go @@ -2,6 +2,8 @@ package math import "math/big" +// https://github.com/ALTree/bigfloat + // Pow returns a big.Float representation of z**w. Precision is the same as the one // of the first argument. The function panics when z is negative. func Pow(z *big.Float, w *big.Float) *big.Float { @@ -29,13 +31,6 @@ func Pow(z *big.Float, w *big.Float) *big.Float { return x.Quo(big.NewFloat(1), Pow(zExt, wNeg)).SetPrec(z.Prec()) } - // w integer fast path (disabled because introduces rounding - // errors) - if false && w.IsInt() { - wi, _ := w.Int64() - return powInt(z, int(wi)) - } - // compute w**z as exp(z log(w)) x := new(big.Float).SetPrec(z.Prec() + 64) logZ := Log(new(big.Float).Copy(z).SetPrec(z.Prec() + 64)) @@ -44,28 +39,3 @@ func Pow(z *big.Float, w *big.Float) *big.Float { return x.SetPrec(z.Prec()) } - -// fast path for z**w when w is an integer -func powInt(z *big.Float, w int) *big.Float { - - // get mantissa and exponent of z - mant := new(big.Float) - exp := z.MantExp(mant) - - // result's exponent - exp = exp * w - - // result's mantissa - x := big.NewFloat(1).SetPrec(z.Prec()) - - // Classic right-to-left binary exponentiation - for w > 0 { - if w%2 == 1 { - x.Mul(x, mant) - } - w >>= 1 - mant.Mul(mant, mant) - } - - return new(big.Float).SetMantExp(x, exp) -}