Giter Club home page Giter Club logo

gofloat's Introduction

gofloat

gofloat_logo

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.

gofloat's People

Contributors

senpathi avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

gofloat's Issues

Go version

Update the go version to the current version (1.22.4)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.