Compare commits

...

7 commits

Author SHA1 Message Date
kempeng
ed771bb615
Merge aa8a681072 into d00399e161 2024-03-01 13:54:39 -08:00
Geert van Kempen
aa8a681072 added sqrt testing 2018-08-19 14:00:50 -04:00
Geert van Kempen
3954520093 native square root function for decimal 2018-08-19 10:54:18 -04:00
Geert van Kempen
53965de086 additional decimal numbers 2018-08-19 10:44:54 -04:00
Geert van Kempen
8db667fde7 Merge branch 'patch-2' 2018-08-19 10:20:36 -04:00
kempeng
e20cd0e503
Create zero.go
Zero.go defines convenience functions for testing decimal values with the zero value. These functions make code using decimal easier to read in my opinion. EqualZero() could be renamed into IsZero()
2018-07-25 23:22:11 -04:00
kempeng
70eaa6c75d
Create helpers.go
helpers.go defines several helper functions for the create of new decimal's from int's that make code using decimal a bit easier to read. Further more it implements a.Max() and a.Min() functions in the same fashion as other arithmetic functions like Add & Sub. (the decimal package already implements Max(d, d2....) and Min(), but these two helper functions are complementary to them.
Finally a.Float() is a convenience function for a.Float64() that ignore the precise bool, allowing inline use of the float conversion function
2018-07-25 23:19:28 -04:00
5 changed files with 252 additions and 0 deletions

38
helpers.go Normal file
View file

@ -0,0 +1,38 @@
package decimal
// Float returns the decimal value as a float64
// float is a helper function for float64() and doesnot return bool exact
func (d Decimal) Float() float64 {
f, _ := d.Float64()
return f
}
// NotEqual returns true when d is not equal to d2
func (d Decimal) NotEqual(d2 Decimal) bool {
return !d.Equal(d2)
}
// Max returns the maximum value between d and d2
func (d Decimal) Max(d2 Decimal) Decimal {
return Max(d, d2)
}
// Min returns the minimum value between d and d2
func (d Decimal) Min(d2 Decimal) Decimal {
return Min(d, d2)
}
//NewFromInt returns a decimal with the value of int v
func NewFromInt(v int) Decimal {
return New(int64(v), 0)
}
//NewFromInt32 returns a decimal with the value of int v
func NewFromInt32(v int) Decimal {
return New(int64(v), 0)
}
//NewFromInt64 returns a decimal with the value of int v
func NewFromInt64(v int64) Decimal {
return New(v, 0)
}

124
numbers.go Normal file
View file

@ -0,0 +1,124 @@
package decimal
// MinusOne defines value -1 in decimal format
var MinusOne = New(-1, 0)
// OneTenth defines value 0.1 in decimal format
var OneTenth = New(1, -1)
// TwoTenth defines value 0.1 in decimal format
var TwoTenth = New(2, -1)
// Half defines value 0.5 in decimal format
var Half = New(5, -1)
// One defines value 1 in decimal format
var One = New(1, 0)
// Two defines value 2 in decimal format
var Two = New(2, 0)
// Three defines value 3 in decimal format
var Three = New(3, 0)
// Four defines value 4 in decimal format
var Four = New(4, 0)
// Five defines value 5 in decimal format
var Five = New(5, 0)
// Six defines value 6 in decimal format
var Six = New(6, 0)
// Seven defines value 6 in decimal format
var Seven = New(7, 0)
// Eight defines value 8 in decimal format
var Eight = New(8, 0)
// Nine defines value 9 in decimal format
var Nine = New(9, 0)
// Ten defines value 10 in decimal format
var Ten = New(10, 0)
// Eleven defines value 11 in decimal format
var Eleven = New(11, 0)
// Twelve defines value 12 in decimal format
var Twelve = New(12, 0)
// Thirteen defines value 13 in decimal format
var Thirteen = New(13, 0)
// Fourteen defines value 14 in decimal format
var Fourteen = New(14, 0)
// Fifteen defines value 15 in decimal format
var Fifteen = New(15, 0)
// Sixteen defines value 16 in decimal format
var Sixteen = New(16, 0)
// Seventeen defines value 17 in decimal format
var Seventeen = New(17, 0)
// Eighteen defines value 18 in decimal format
var Eighteen = New(18, 0)
// Nineteen defines value 19 in decimal format
var Nineteen = New(19, 0)
// Twenty defines value 20 in decimal format
var Twenty = New(20, 0)
// Thirty defines value 30 in decimal format
var Thirty = New(30, 0)
// Forty defines value 40 in decimal format
var Forty = New(40, 0)
// Fifty defines value 50 in decimal format
var Fifty = New(50, 0)
// Sixty defines value 60 in decimal format
var Sixty = New(60, 0)
// Seventy defines value 70 in decimal format
var Seventy = New(70, 0)
// Eighty defines value 80 in decimal format
var Eighty = New(80, 0)
// Ninty defines value 90 in decimal format
var Ninty = New(90, 0)
// Hundred defines value 100 in decimal format
var Hundred = New(100, 0)
// TwoHundred defines value 200 in decimal format
var TwoHundred = New(200, 0)
// ThreeHundred defines value 300 in decimal format
var ThreeHundred = New(300, 0)
// FourHundred defines value 400 in decimal format
var FourHundred = New(400, 0)
// FiveHundred defines value 500 in decimal format
var FiveHundred = New(500, 0)
// SixHundred defines value 600 in decimal format
var SixHundred = New(600, 0)
// SevenHundred defines value 700 in decimal format
var SevenHundred = New(700, 0)
// EightHundred defines value 800 in decimal format
var EightHundred = New(800, 0)
// NineHundred defines value 900 in decimal format
var NineHundred = New(900, 0)
// Thousand defines value 100 in decimal format
var Thousand = New(1000, 0)

35
sqrt.go Normal file
View file

@ -0,0 +1,35 @@
package decimal
const sqrtMaxIter = 10000
// Sqrt returns the square root of d, the result will have
// DivisionPrecision digits after the decimal point.
func (d Decimal) Sqrt() Decimal {
s, _ := d.SqrtRound(int32(DivisionPrecision))
return s
}
// SqrtRound returns the square root of d, the result will have
// precision digits after the decimal point. The bool precise returns whether the precision was reached
func (d Decimal) SqrtRound(precision int32) (Decimal, bool) {
if d.LessThanOrEqualZero() {
return Zero, false
}
cutoff := New(1, -precision)
lo := Zero
hi := d
var mid Decimal
for i := 0; i < sqrtMaxIter; i++ {
//mid = (lo+hi)/2;
mid = lo.Add(hi).DivRound(Two, precision)
if mid.Mul(mid).Sub(d).Abs().LessThan(cutoff) {
return mid, true
}
if mid.Mul(mid).GreaterThan(d) {
hi = mid
} else {
lo = mid
}
}
return mid, false
}

24
sqrt_test.go Normal file
View file

@ -0,0 +1,24 @@
package decimal
import (
"testing"
)
func TestSqrt(t *testing.T) {
tables := []struct {
x Decimal
n Decimal
}{
{One, One},
{Four, Two},
{Sixteen, Four},
{Two, NewFromFloat(1.4142135623730951)},
}
for _, table := range tables {
result := table.x.Sqrt()
if result.NotEqual(table.n) {
t.Errorf("Sqrt of (%v) was incorrect, got: %v, want: %v.", table.x.String(), result.String(), table.n.String())
}
}
}

31
zero.go Normal file
View file

@ -0,0 +1,31 @@
package decimal
// EqualZero returns whether the numbers represented by d equals zero.
func (d Decimal) EqualZero() bool {
return d.Equal(Zero)
}
// NotZero returns whether d is not zero
func (d Decimal) NotZero() bool {
return !d.EqualZero()
}
// GreaterThanZero (GT0) returns true when d is greater than zero.
func (d Decimal) GreaterThanZero() bool {
return d.GreaterThan(Zero)
}
// GreaterThanOrEqualZero (GTE0) returns true when d is greater than or equal to zero.
func (d Decimal) GreaterThanOrEqualZero() bool {
return d.GreaterThanOrEqual(Zero)
}
// LessThanZero returns true when d is less than zero.
func (d Decimal) LessThanZero() bool {
return d.LessThan(Zero)
}
// LessThanOrEqualZero returns true when d is less than or equal to zero.
func (d Decimal) LessThanOrEqualZero() bool {
return d.LessThanOrEqual(Zero)
}