Skip to content

Commit

Permalink
Merge pull request #5 from senpathi/develop
Browse files Browse the repository at this point in the history
Performance upgrade
  • Loading branch information
senpathi authored Jun 21, 2024
2 parents b295162 + 2d1dae5 commit 0f18307
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 32 deletions.
3 changes: 2 additions & 1 deletion examples/usage/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ package main

import (
"fmt"
"github.com/senpathi/gofloat"
"math"

"github.com/senpathi/gofloat"
)

func main() {
Expand Down
62 changes: 37 additions & 25 deletions gofloat.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,71 +7,83 @@ import (

// Float is to keep float's integer value, decimal value and its precision
type Float struct {
integer int64
decimal int64
precision int
integer int64
decimal int64
precision int
precisionFactor int64
}

// ToFloat returns a Float of f to given precision
func ToFloat(f float64, precision int) Float {
floatingLength := int64(math.Trunc(math.Pow(10, float64(precision))))
rounded := math.Round(f * float64(floatingLength))
precision = int(math.Abs(float64(precision)))
floatingLength := math.Pow10(precision)

return toFloat(f, precision, int64(floatingLength))
}

func toFloat(f float64, precision int, precisionFactor int64) Float {
rounded := math.Round(f * float64(precisionFactor))
return Float{
integer: int64(rounded) / floatingLength,
decimal: int64(rounded) % floatingLength,
precision: precision,
integer: int64(rounded) / precisionFactor,
decimal: int64(rounded) % precisionFactor,
precision: precision,
precisionFactor: precisionFactor,
}
}

// Add returns a Float of f + x
func (f Float) Add(x Float) Float {
max := getMaxPrecision(f.precision, x.precision)
maxP, maxFact := getMaxPrecision(f, x)
sum := f.Float64() + x.Float64()
return ToFloat(sum, max)
return toFloat(sum, maxP, maxFact)
}

// Sub returns a Float of f - x
func (f Float) Sub(x Float) Float {
max := getMaxPrecision(f.precision, x.precision)
maxP, maxFact := getMaxPrecision(f, x)
sub := f.Float64() - x.Float64()
return ToFloat(sub, max)
return toFloat(sub, maxP, maxFact)
}

// Multiply returns a Float of f * x
func (f Float) Multiply(x Float) Float {
max := getMaxPrecision(f.precision, x.precision)
maxP, maxFact := getMaxPrecision(f, x)
mul := f.Float64() * x.Float64()
return ToFloat(mul, max)
return toFloat(mul, maxP, maxFact)
}

// Divide returns a Float of f / x
//
// Special cases are :
// x.Float64 = 0 returns Float value 0
//
// x.Float64 = 0 returns Float value 0
func (f Float) Divide(x Float) Float {
if x.Float64() == 0 {
return ToFloat(0, 0)
return Float{
integer: 0,
decimal: 0,
precision: f.precision,
precisionFactor: f.precisionFactor,
}
}
max := getMaxPrecision(f.precision, x.precision)
maxP, maxFact := getMaxPrecision(f, x)
dev := f.Float64() / x.Float64()
return ToFloat(dev, max)
return toFloat(dev, maxP, maxFact)
}

// Float64 returns a float64 value of f
func (f Float) Float64() float64 {
floatingLength := int64(math.Trunc(math.Pow(10, float64(f.precision))))
return (float64(f.integer*floatingLength) + float64(f.decimal)) / float64(floatingLength)
return (float64(f.integer*f.precisionFactor) + float64(f.decimal)) / float64(f.precisionFactor)
}

func (f Float) String() string {
return strconv.FormatFloat(f.Float64(), 'f', f.precision, 64)
}

func getMaxPrecision(p1, p2 int) (max int) {
max = p1
if p1 < p2 {
max = p2
func getMaxPrecision(p1, p2 Float) (max int, maxFactor int64) {
if p1.precision < p2.precision {
return p2.precision, p2.precisionFactor
}
return

return p1.precision, p1.precisionFactor
}
3 changes: 2 additions & 1 deletion gofloat_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ package gofloat_test

import (
"fmt"
"github.com/senpathi/gofloat"
"math"

"github.com/senpathi/gofloat"
)

func ExampleToFloat() {
Expand Down
13 changes: 8 additions & 5 deletions gofloat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,14 @@ func setupTestTable(operator string) []TestInput {
precision2: rand.Intn(9),
}

length1 := math.Trunc(math.Pow(10, float64(inputs[i].precision1)))
max := getMaxPrecision(inputs[i].precision1, inputs[i].precision2)
lengthMax := math.Trunc(math.Pow(10, float64(max)))
rounded1 := ToFloat(inputs[i].input1, inputs[i].precision1).Float64()
rounded2 := ToFloat(inputs[i].input2, inputs[i].precision2).Float64()
length1 := math.Trunc(math.Pow10(inputs[i].precision1))

f1 := ToFloat(inputs[i].input1, inputs[i].precision1)
rounded1 := f1.Float64()
f2 := ToFloat(inputs[i].input2, inputs[i].precision2)
rounded2 := f2.Float64()
_, maxFact := getMaxPrecision(f1, f2)
lengthMax := float64(maxFact)

switch operator {
case `NONE`:
Expand Down

0 comments on commit 0f18307

Please sign in to comment.