gofloat

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 20, 2021 License: MIT Imports: 1 Imported by: 2

README

gofloat

Godoc build Coverage Releases Go Report Card LICENSE

Gofloat is a library that does floating-point number calculations with fixed precision.

Description

why gofloat
  • without gofloat
package main

import "fmt"

func main() {
	f1 := 0.1
	f2 := 0.2
	if f1+f2 == 0.3 {
		fmt.Println(`0.1 + 0.2 = `, f1+f2)
	} else {
		fmt.Println(`0.1 + 0.2 != `, f1+f2)
	}
	//Output: 0.1 + 0.2 != 0.30000000000000004
}
  • with gofloat
package main

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

func main() {
	f1 := gofloat.ToFloat(0.1, 1)
	f2 := gofloat.ToFloat(0.2, 1)
	if f1.Add(f2).Float64() == 0.3 {
		fmt.Println(`0.1 + 0.2 = `, f1.Add(f2).Float64())
	} else {
		fmt.Println(`0.1 + 0.2 != `, f1.Add(f2).Float64())
	}
	//Output: 0.1 + 0.2 = 0.3
}

  • gofloat makes sure to do calculations on floating-point numbers with fix number of precision digits
  • Converts floating-point numbers into gofloat's Float type with sent precision length
  • Do Calculations in gofloat's Float and then can be converted back into floating-point

Usage

Documentation is available via GoDoc.

package main

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

func main() {

	// convert Pi and Sqrt(2) into Float with three precisions
	pi := gofloat.ToFloat(math.Pi, 3)
	sqrt2 := gofloat.ToFloat(math.Sqrt2, 3)
	fmt.Println(pi.Float64(), sqrt2.Float64()) //Output: 3.142 1.414

	// add Sqrt(2) to Pi and get results with 3 precisions
	f := pi.Add(sqrt2)
	fmt.Println(f.Float64()) //Output: 4.556

	// subtract Sqrt(2) from Pi and get result with 3 precision
	f = pi.Sub(sqrt2)
	fmt.Println(f.Float64()) //output: 1.728

	// multiply Pi by Sqrt(2) and get result with 3 precision
	f = pi.Multiply(sqrt2)
	fmt.Println(f.Float64()) //Output: 4.443

	// divide Pi by Sqrt(2) and get result with 3 precision
	f = pi.Divide(sqrt2)
	fmt.Println(f.Float64()) //Output: 2.222
}

Please see the example programs in the examples directory for reference.

Limitations

  • if the calculated result is larger than maximum float64 value, result will be incorrect.

Documentation

Overview

Gofloat is a library that does floating-point number calculations with fixed precision.

Below examples show why Gofloat library is useful when doing floating-point calculations.

Without Gofloat

package main

import "fmt"

func main() {
	f1 := 0.1
	f2 := 0.2
	if f1+f2 == 0.3 {
		fmt.Println(\`0.1 + 0.2 = \`, f1+f2)
	} else {
		fmt.Println(\`0.1 + 0.2 != \`, f1+f2)
	}
//Output: 0.1 + 0.2 != 0.30000000000000004
}

With Gofloat

package main

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

func main() {
	f1 := gofloat.ToFloat(0.1, 1)
	f2 := gofloat.ToFloat(0.2, 1)
	if f1.Add(f2).Float64() == 0.3 {
		fmt.Println(\`0.1 + 0.2 = \`, f1.Add(f2).Float64())
	} else {
		fmt.Println(\`0.1 + 0.2 != \`, f1.Add(f2).Float64())
	}
//Output: 0.1 + 0.2 = 0.3
}

Gofloat make sure to do calculations on floating-point numbers with fix number of precision digits. it converts floating-point numbers into gofloat's `Float` type with sent precision length and do calculations in gofloat's `Float` type and it can be converted back into floating-point.

Below example shows how to use the library in applications.

package main

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

func main() {

	// convert Pi and Sqrt(2) into Float with three precisions
	pi := gofloat.ToFloat(math.Pi, 3)
	sqrt2 := gofloat.ToFloat(math.Sqrt2, 3)
	fmt.Println(pi.Float64(), sqrt2.Float64()) //Output: 3.142 1.414

	// add Sqrt(2) to Pi and get results with 3 precisions
	f := pi.Add(sqrt2)
	fmt.Println(f.Float64()) //Output: 4.556

	// subtract Sqrt(2) from Pi and get result with 3 precision
	f = pi.Sub(sqrt2)
	fmt.Println(f.Float64()) //output: 1.728

	// multiply Pi by Sqrt(2) and get result with 3 precision
	f = pi.Multiply(sqrt2)
	fmt.Println(f.Float64()) //Output: 4.443

	// divide Pi by Sqrt(2) and get result with 3 precision
	f = pi.Divide(sqrt2)
	fmt.Println(f.Float64()) //Output: 2.222
}

Please see the example programs in the examples directory for reference.

Limitations.

If the calculated result is larger than maximum float64 value, result will be incorrect.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Float

type Float struct {
	// contains filtered or unexported fields
}

func ToFloat

func ToFloat(f float64, precision int) Float

ToFloat returns a Float of f to given precision

Example
package main

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

func main() {
	// convert Pi and Sqrt(2) into Float with three precisions
	pi := gofloat.ToFloat(math.Pi, 3)
	sqrt2 := gofloat.ToFloat(math.Sqrt2, 3)

	fmt.Println(pi.Float64(), sqrt2.Float64())
}
Output:

3.142 1.414

func (Float) Add

func (f Float) Add(x Float) Float

Add returns a Float of f + x

Example
package main

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

func main() {
	// add Sqrt(2) to Pi and get results with 3 precisions
	pi := gofloat.ToFloat(math.Pi, 3)
	sqrt2 := gofloat.ToFloat(math.Sqrt2, 3)

	f := pi.Add(sqrt2)
	fmt.Println(`PI + Sqrt(2) =`, f.Float64())
}
Output:

PI + Sqrt(2) = 4.556

func (Float) Divide

func (f Float) Divide(x Float) Float

Divide returns a Float of f / x

Special cases are :

x.Float64 = 0 returns Float value 0
Example
package main

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

func main() {
	// divide Pi by Sqrt(2) and get result with 3 precision
	pi := gofloat.ToFloat(math.Pi, 3)
	sqrt2 := gofloat.ToFloat(math.Sqrt2, 3)

	f := pi.Divide(sqrt2)
	fmt.Println(`PI / Sqrt(2) =`, f.Float64())
}
Output:

PI / Sqrt(2) = 2.222

func (Float) Float64

func (f Float) Float64() float64

Float64 returns a float64 value of f

Example
package main

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

func main() {
	// convert Pi and Sqrt(2) into Float with three precisions and convert back to float64
	pi := gofloat.ToFloat(math.Pi, 3)
	sqrt2 := gofloat.ToFloat(math.Sqrt2, 3)

	fmt.Println(pi.Float64(), sqrt2.Float64())
}
Output:

3.142 1.414

func (Float) Multiply

func (f Float) Multiply(x Float) Float

Multiply returns a Float of f * x

Example
package main

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

func main() {
	// multiply Pi by Sqrt(2) and get result with 3 precision
	pi := gofloat.ToFloat(math.Pi, 3)
	sqrt2 := gofloat.ToFloat(math.Sqrt2, 3)

	f := pi.Multiply(sqrt2)
	fmt.Println(`PI * Sqrt(2) =`, f.Float64())
}
Output:

PI * Sqrt(2) = 4.443

func (Float) Sub

func (f Float) Sub(x Float) Float

Sub returns a Float of f - x

Example
package main

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

func main() {
	// subtract Sqrt(2) from Pi and get result with 3 precision
	pi := gofloat.ToFloat(math.Pi, 3)
	sqrt2 := gofloat.ToFloat(math.Sqrt2, 3)

	f := pi.Sub(sqrt2)
	fmt.Println(`PI - Sqrt(2) =`, f.Float64())
}
Output:

PI - Sqrt(2) = 1.728

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL