zeroshade commented on code in PR #43958: URL: https://github.com/apache/arrow/pull/43958#discussion_r1750698526
########## go/arrow/decimal/decimal.go: ########## @@ -0,0 +1,467 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package decimal + +import ( + "errors" + "fmt" + "math" + "math/big" + "math/bits" + "unsafe" + + "github.com/apache/arrow/go/v18/arrow/decimal128" + "github.com/apache/arrow/go/v18/arrow/decimal256" + "github.com/apache/arrow/go/v18/arrow/internal/debug" +) + +type Num[T Decimal32 | Decimal64 | Decimal128 | Decimal256] interface { + Negate() T + Add(T) T + Sub(T) T + Mul(T) T + Div(T) (res, rem T) + Pow(T) T + + FitsInPrecision(int32) bool + Abs() T + Sign() int + Rescale(int32, int32) (T, error) + Cmp(T) int + + IncreaseScaleBy(int32) T + ReduceScaleBy(int32, bool) T + + ToFloat32(int32) float32 + ToFloat64(int32) float64 + ToBigFloat(int32) *big.Float + + ToString(int32) string +} + +type ( + Decimal32 int32 + Decimal64 int64 + Decimal128 = decimal128.Num + Decimal256 = decimal256.Num +) + +func MaxPrecision[T Decimal32 | Decimal64 | Decimal128 | Decimal256]() int { + // max precision is computed by Floor(log10(2^(nbytes * 8 - 1) - 1)) + var z T + return int(math.Floor(math.Log10(math.Pow(2, float64(unsafe.Sizeof(z))*8-1) - 1))) +} + +func (d Decimal32) Negate() Decimal32 { return -d } +func (d Decimal64) Negate() Decimal64 { return -d } + +func (d Decimal32) Add(rhs Decimal32) Decimal32 { return d + rhs } +func (d Decimal64) Add(rhs Decimal64) Decimal64 { return d + rhs } + +func (d Decimal32) Sub(rhs Decimal32) Decimal32 { return d - rhs } +func (d Decimal64) Sub(rhs Decimal64) Decimal64 { return d - rhs } + +func (d Decimal32) Mul(rhs Decimal32) Decimal32 { return d * rhs } +func (d Decimal64) Mul(rhs Decimal64) Decimal64 { return d * rhs } + +func (d Decimal32) Div(rhs Decimal32) (res, rem Decimal32) { + return d / rhs, d % rhs +} + +func (d Decimal64) Div(rhs Decimal64) (res, rem Decimal64) { + return d / rhs, d % rhs +} + +// about 4-5x faster than using math.Pow which requires converting to float64 +// and back to integers +func intPow[T int32 | int64](base, exp T) T { + result := T(1) + for { + if exp&1 == 1 { + result *= base + } + exp >>= 1 + if exp == 0 { + break + } + base *= base + } + + return result +} + +func (d Decimal32) Pow(rhs Decimal32) Decimal32 { + return Decimal32(intPow(int32(d), int32(rhs))) +} + +func (d Decimal64) Pow(rhs Decimal64) Decimal64 { + return Decimal64(intPow(int64(d), int64(rhs))) +} + +func (d Decimal32) Sign() int { + if d == 0 { + return 0 + } + return int(1 | (d >> 31)) +} + +func (d Decimal64) Sign() int { + if d == 0 { + return 0 + } + return int(1 | (d >> 63)) +} + +func (n Decimal32) Abs() Decimal32 { + if n < 0 { + return -n + } + return n +} + +func (n Decimal64) Abs() Decimal64 { + if n < 0 { + return -n + } + return n +} + +func (n Decimal32) FitsInPrecision(prec int32) bool { + debug.Assert(prec > 0, "precision must be > 0") + debug.Assert(prec <= 9, "precision must be <= 9") + return n.Abs() < Decimal32(math.Pow10(int(prec))) +} + +func (n Decimal64) FitsInPrecision(prec int32) bool { + debug.Assert(prec > 0, "precision must be > 0") + debug.Assert(prec <= 18, "precision must be <= 18") + return n.Abs() < Decimal64(math.Pow10(int(prec))) +} + +func (n Decimal32) ToString(scale int32) string { + f := (&big.Float{}).SetInt64(int64(n)) + if scale < 0 { + f.SetPrec(32).Mul(f, (&big.Float{}).SetInt64(int64(intPow(10, -scale)))) + } else { + f.SetPrec(32).Quo(f, (&big.Float{}).SetInt64(int64(intPow(10, scale)))) + } + return f.Text('f', int(scale)) +} Review Comment: simplified -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
