rational

package
v0.0.0-...-c99f163 Latest Latest
Warning

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

Go to latest
Published: Dec 20, 2025 License: MIT Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var One = sync.OnceValue(func() *Number {
	return New64(1, 1)
})
View Source
var Zero = sync.OnceValue(func() *Number {
	return New64(0, 1)
})

Functions

This section is empty.

Types

type Number

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

Number represents a rational number.

func FromRational

func FromRational(r *big.Rat) *Number

FromRational creates a new rational number from a big.Rat.

func New

func New(a, b *big.Int) *Number

New creates a new rational number from int numerator and denominator.

func New64

func New64(a, b int64) *Number

New64 creates a new rational number from int64 numerator and denominator.

func (*Number) Add

func (r *Number) Add(other *Number) *Number

Add adds two rational numbers.

func (*Number) Cmp

func (r *Number) Cmp(other *Number) int

Cmp compares two rational numbers: -1 if r < other, 0 if r == other, 1 if r > other.

func (*Number) Constructive

func (r *Number) Constructive() constructive.Real

Constructive converts the rational number to a constructive real.

func (*Number) Divide

func (r *Number) Divide(other *Number) *Number

Divide divides two rational numbers.

func (*Number) Inverse

func (r *Number) Inverse() *Number

Inverse returns the multiplicative inverse of the rational number.

func (*Number) IsZero

func (r *Number) IsZero() bool

IsZero checks if the rational number is zero.

func (*Number) Multiply

func (r *Number) Multiply(other *Number) *Number

Multiply multiplies two rational numbers.

func (*Number) Negate

func (r *Number) Negate() *Number

Negate negates the rational number.

func (*Number) ShiftLeft

func (r *Number) ShiftLeft(n int) *Number

ShiftLeft shifts the rational number left by n bits (multiplies by 2^n). Supports signed shifts: negative n shifts right.

func (*Number) ShiftRight

func (r *Number) ShiftRight(n int) *Number

ShiftRight shifts the rational number right by n bits (divides by 2^n). Supports signed shifts: negative n shifts left.

func (*Number) Sign

func (r *Number) Sign() int

Sign returns the sign of the rational number: -1 for negative, 0 for zero, 1 for positive.

func (*Number) String

func (r *Number) String() string

String returns the string representation of the rational number. If the denominator is 1, it returns just the numerator. Otherwise, it returns "numerator/denominator".

func (*Number) Subtract

func (r *Number) Subtract(other *Number) *Number

Subtract subtracts two rational numbers.

Jump to

Keyboard shortcuts

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