num

package module
v0.0.0-...-aa9ae16 Latest Latest
Warning

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

Go to latest
Published: Sep 27, 2022 License: BSD-3-Clause Imports: 6 Imported by: 0

README

num

GoDoc

Go module for dealing with fractions, and a utility for dividing two numbers and returning the simplified fraction.

Example use

> frac 2/5
⅖
> frac 0.8
⅘
> frac 123
123

Use only 100 iterations when creating a fraction that represents the given float:

> frac -m 100 0.777777777
10/13

Installation

go install github.com/xyproto/num/cmd/frac@latest

General info

  • License: BSD-3

Documentation

Index

Examples

Constants

View Source
const (
	// DefaultMaxIterations is the default value of iterations when converting a float to a fractional number.
	DefaultMaxIterations = 400

	// Inifinite iterations
	I = -1

	// A large number of iterations
	L = 9999
)

Variables

View Source
var (
	Zero = &Frac{0, 1, DefaultMaxIterations, true}
	One  = &Frac{1, 1, DefaultMaxIterations, true}

	ErrDivByZero = errors.New("division by zero")
)

Functions

This section is empty.

Types

type Frac

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

func Abs

func Abs(f *Frac) *Frac

Return the absolute value

func Add

func Add(a, b *Frac) *Frac

Add two fractions and return the result

func AddInt

func AddInt(f *Frac, x int) *Frac

Add an integer and reduce the result

func Cos

func Cos(f *Frac) *Frac

Cos returns the sin of the number

func Div

func Div(a, b *Frac) (*Frac, error)

Divide two fractions and return the result

func DivInt

func DivInt(f *Frac, x int) (*Frac, error)

Divide by an integer and reduce the result

func Mul

func Mul(a, b *Frac) (*Frac, error)

Multiply two fractions and return the result

func MulInt

func MulInt(f *Frac, x int) (*Frac, error)

Multiply with an integer and reduce the result

func MustNew

func MustNew(num, dom int64) *Frac

MustNew must create a new fractional number. If it is not possible, no error will be returned and it will panic.

func New

func New(num, dom int64) (*Frac, error)

New creates a new fractional number. Takes a numinator and a denominator. The maximum number of iterations that should be used for reducing the fraction during calculations is set to the default value.

func NewFromFloat64

func NewFromFloat64(f float64, maxIterations int) *Frac

Try to convert a float to a fraction Takes a float and a maximum number of iterations to find the fraction The maximum number of iterations can be -1 to iterate as much as necessary Returns a bool that is True if the maximum number of iterations has not been reached

func NewFromInt

func NewFromInt(num int) *Frac

Create a new fraction that is "N/1"

func NewFromInt64

func NewFromInt64(num int64) *Frac

Create a new fraction that is "N/1"

func NewFromRat

func NewFromRat(rat *big.Rat) *Frac

Creates a new fraction from a rational number (big.Rat)

func NewFromString

func NewFromString(exp string) (*Frac, error)

Creates a new fraction from a string on the form "N/D", where N is the numerator and D is the denominator. For example: "1/2" or "3/8".

func NewZero

func NewZero() *Frac

NewZero returns a fraction that is "0/1"

func Sin

func Sin(f *Frac) *Frac

Sin returns the sin of the number

func Sqrt

func Sqrt(f *Frac) *Frac

Sqrt returns the square root of the number

Example
x := NewFromInt(9)
fmt.Println(Sqrt(x).Float64())
Output:

3

func Square

func Square(f *Frac) *Frac

Multiply this number by itself

func Sub

func Sub(a, b *Frac) *Frac

Subtract two fractions and return the result

func SubInt

func SubInt(f *Frac, x int) *Frac

Subtract an integer and reduce the result

func (*Frac) Abs

func (f *Frac) Abs()

func (*Frac) Add

func (f *Frac) Add(x *Frac)

Add another fraction, don't return anything

func (*Frac) AddInt

func (f *Frac) AddInt(x int)

Add an integer and reduce the result

func (*Frac) AddInt64

func (f *Frac) AddInt64(x int64)

Add an int64 and reduce the result

func (*Frac) Copy

func (f *Frac) Copy() *Frac

Copy creates a copy

func (*Frac) Div

func (f *Frac) Div(x *Frac)

Divide by another fraction, don't return anything

func (*Frac) DivInt

func (f *Frac) DivInt(x int)

Divide by an integer and reduce the result

func (*Frac) Equal

func (f *Frac) Equal(b *Frac) bool

Quickly check if one fraction is equal to the other, by only multiplying and comparing

func (*Frac) ExactFloat64

func (f *Frac) ExactFloat64() bool

func (*Frac) Float64

func (f *Frac) Float64() float64

Return the fraction as a float64. Some precision may be lost.

func (*Frac) GreaterThan

func (f *Frac) GreaterThan(b *Frac) bool

Quickly check if one fraction is larger than the other, by only multiplying and comparing

func (*Frac) Int

func (f *Frac) Int() int

Return the fraction as an int, not rounded

func (*Frac) Int64

func (f *Frac) Int64() int64

Return the fraction as an int, not rounded

func (*Frac) IsZero

func (f *Frac) IsZero() bool

IsZero checks if this fraction is 0

func (*Frac) LessThan

func (f *Frac) LessThan(b *Frac) bool

Quickly check if one fraction is less than the other, by only multiplying and comparing

func (*Frac) Mul

func (f *Frac) Mul(x *Frac)

Multiply by another fraction, don't return anything

func (*Frac) MulInt

func (f *Frac) MulInt(x int)

Multiply with an integer and reduce the result

func (*Frac) Rat

func (f *Frac) Rat() *big.Rat

Returns a rational number (big.Rat)

func (*Frac) Round

func (f *Frac) Round() int

Round of the fraction to an int

func (*Frac) SetExact

func (f *Frac) SetExact(exact bool)

func (*Frac) SetMaxReduceIterations

func (f *Frac) SetMaxReduceIterations(maxReduceIterations int)

Change the maximum number of iterations that should be used for reductions

func (*Frac) Splitup

func (f *Frac) Splitup() (int64, *Frac)

Split up a fraction into an integer part, and the rest as another fraction

func (*Frac) Sqrt

func (f *Frac) Sqrt()

Take the square root of this number

func (*Frac) Square

func (f *Frac) Square()

Multiply this number by itself

func (*Frac) String

func (f *Frac) String() string

Return the fraction as a string

func (*Frac) Sub

func (f *Frac) Sub(x *Frac)

Subtract another fraction, don't return anything

func (*Frac) SubInt

func (f *Frac) SubInt(x int)

Subtract an integer and reduce the result

func (*Frac) SubInt64

func (f *Frac) SubInt64(x int64)

Subtract an int64 and reduce the result

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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