Skip to content

A Go binding for the MPFR library to enable arbitrary-precision floating-point arithmetic with well-defined rounding. This is not ready for production use yet, but we welcome contributions!

License

Notifications You must be signed in to change notification settings

mexicantexan/go-mpfr

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

35 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

go-mpfr

A Go binding for the MPFR library to enable arbitrary-precision floating-point arithmetic with well-defined rounding. This is not ready for production use yet, but we welcome contributions! Please note that this has no association with the MPFR project.

Inspiration/The Why

This was sparked due to a need for high-precision floating-point calculations in Go. The standard library's math/big package is great for most applications, but it lacks certain features that mpfr supports out of the box. This project is being used by another in order to perform some high precision calculations, which is why all of the functionality of mpfr has not been implemented yet. Only the necessary functions have been implemented so far.

Inspiration for this project comes from GMP! Huge shoutout to all the contributors there and for mpfr for providing such a great library!!

Features

  • High-precision floating-point calculations using MPFR.
  • Automatic memory management in Go, with finalizers for MPFR structs.
  • Support for multiple rounding modes (nearest, up, down, zero, etc.).
  • Ability to set precision as needed.

Install

First, ensure that GMP and MPFR are installed on your system:

Ubuntu/Debian:

sudo apt-get update && sudo apt-get install libmpfr-dev libgmp-dev

macOS (Homebrew):

brew install mpfr gmp

Windows:

Using MSYS2 Package Manager:

pacman -S mingw-w64-x86_64-gmp mingw-w64-x86_64-mpfr

Using Chocolatey or Scoop:

  • Install Chocolatey or Scoop.

Installing via Chocolatey

choco install gmp mpfr

Or with Scoop:

scoop install gmp mpfr

Then, install the package using go get:

go get github.com/mexicantexan/go-mpfr

Usage

A quick example showing different setters, getters, and function calling:

package main

import (
	"fmt"
	mpfr "github.com/mexicantexan/go-mpfr"
        "math/big"
)

func main() {
	// add two separate floats into a new float
	x := mpfr.NewFloat().SetFloat64(1.5)
	y := mpfr.FromFloat64(2.25) // there are many ways to create a float
	sum := mpfr.Add(x, y, mpfr.RoundToNearest)
	fmt.Printf("1.5 + 2.25 = %v\n", sum.Float64())

	// perform "inplace" operations 
	x.Add(y)
	fmt.Printf("1.5 + 2.25 = %v\n", x.Float64())

	// add multiple floats (summing 1.5, 2.25, 3.75, and 1.0)
	t := mpfr.FromInt(1)
	u := mpfr.FromInt64(2)
	v := mpfr.FromUint64(3)
	x = mpfr.FromFloat64(4.01)
	y = mpfr.FromBigInt(big.NewInt(5))
	z := mpfr.NewFloat().SetInt(6)
	t.Add(u, v, x, y, z)
	fmt.Printf("1 + 2 + 3 + 4.01 + 5 + 6 = %v\n", t.Float64())
        fmt.Printf("1 + 2 + 3 + 4.01 + 5 + 6 = %v\n", t.Int64())
	
	// set precision 
	x.SetPrec(256)

	// set RoundingMode 
	x.SetRoundingMode(mpfr.RoundToward0)
}

Run the example above:

go run main.go

Contributing

We welcome contributions! See our CONTRIBUTING guide (TODO) for how to get started.

License

Please see the LICENSE file for details.

Support the Project

If you find this project helpful and would like to support its development:

Buy Me A Coffee

About

A Go binding for the MPFR library to enable arbitrary-precision floating-point arithmetic with well-defined rounding. This is not ready for production use yet, but we welcome contributions!

Resources

License

Contributing

Stars

Watchers

Forks

Sponsor this project

Packages

No packages published

Languages