nustack
ReferenceNu STD

fractions

Nu surface for Python's `fractions` module - exact rational arithmetic.

Module nustd.fractions.

Nu surface for Python's fractions module - exact rational arithmetic.

Mirrors fractions 1-1: Fraction is the class (a Form), backed by fractions.Fraction. fractions has no module-level functions, so there are just two layers: forms (the class) and interactions (the constructor and method atoms; property reads use core GetAttr, arithmetic and comparison use the core atoms). Import it like the stdlib:

from nustd.fractions import Fraction
import nustd.fractions as fractions    # fractions.Fraction.of(1, 3), ...

ScalarQuery

NameSortCallMeaning
Fractionscalar_queryFraction()fractions.Fraction as a Form - an exact rational number.

Fraction

fractions.Fraction as a Form - an exact rational number.

Fraction()

Path nustd.fractions.Fraction. Kind ScalarQuery, sort scalar_query, cardinality scalar.

Named Fraction to mirror from fractions import Fraction. Build one with Fraction.of(num, den) (or from_float / from_decimal / from_str); read its parts as properties; combine with arithmetic and comparison operators.

Methods

Fraction.of(numerator, denominator=1)

Build a fraction from numerator and denominator: Fraction(n, d).

Builds Fraction.

Arguments

NameTypeDefaultMeaning
numeratorIntArg
denominatorIntArg1

Undocumented: example.

Fraction.from_float(value)

The exact fraction equal to a float: Fraction.from_float(f).

Builds Fraction.

Arguments

NameTypeDefaultMeaning
valueFloatArg

Undocumented: example.

Fraction.from_decimal(value)

The exact fraction equal to a Decimal: Fraction.from_decimal(d).

Builds Fraction.

Arguments

NameTypeDefaultMeaning
valueDecimalArg

Undocumented: example.

Fraction.from_str(value)

Parse a fraction string: Fraction(s) (e.g. "3/4", "1.5").

Builds Fraction.

Arguments

NameTypeDefaultMeaning
valueStrArg

Undocumented: example.

.numerator()

The numerator (in lowest terms).

Builds Int.

Undocumented: example.

.denominator()

The denominator (in lowest terms, always positive).

Builds Int.

Undocumented: example.

.limit_denominator(max_denominator=1000000)

The closest fraction with denominator at most max_denominator.

Builds Fraction.

Arguments

NameTypeDefaultMeaning
max_denominatorIntArg1000000

Undocumented: example.

.as_integer_ratio()

The (numerator, denominator) pair as a tuple.

Builds Tuple.

Undocumented: example.

a + b

Builds Fraction.

Arguments

NameTypeDefaultMeaning
otherFractionArg | IntArg

Undocumented: summary, example.

a - b

Builds Fraction.

Arguments

NameTypeDefaultMeaning
otherFractionArg | IntArg

Undocumented: summary, example.

a * b

Builds Fraction.

Arguments

NameTypeDefaultMeaning
otherFractionArg | IntArg

Undocumented: summary, example.

a / b

Builds Fraction.

Arguments

NameTypeDefaultMeaning
otherFractionArg | IntArg

Undocumented: summary, example.

a // b

Builds Fraction.

Arguments

NameTypeDefaultMeaning
otherFractionArg | IntArg

Undocumented: summary, example.

a % b

Builds Fraction.

Arguments

NameTypeDefaultMeaning
otherFractionArg | IntArg

Undocumented: summary, example.

a ** b

Builds Fraction.

Arguments

NameTypeDefaultMeaning
otherIntArg

Undocumented: summary, example.

-a

Builds Fraction.

Undocumented: summary, example.

abs(a)

Builds Fraction.

Undocumented: summary, example.

+a

Builds Fraction.

Undocumented: summary, example.

a > b

Builds Bool.

Arguments

NameTypeDefaultMeaning
otherFractionArg | IntArg

Undocumented: summary, example.

a < b

Builds Bool.

Arguments

NameTypeDefaultMeaning
otherFractionArg | IntArg

Undocumented: summary, example.

a >= b

Builds Bool.

Arguments

NameTypeDefaultMeaning
otherFractionArg | IntArg

Undocumented: summary, example.

a <= b

Builds Bool.

Arguments

NameTypeDefaultMeaning
otherFractionArg | IntArg

Undocumented: summary, example.

.eq(other)

Whether two fractions are equal.

Builds Bool.

Arguments

NameTypeDefaultMeaning
otherFractionArg | IntArg

Undocumented: example.

.ne(other)

Whether two fractions differ.

Builds Bool.

Arguments

NameTypeDefaultMeaning
otherFractionArg | IntArg

Undocumented: example.

Inherited methods

From nu.lang.forms.Form:

CallBuildsMeaning
.is_empty()BoolTrue if this Form yields the EMPTY sentinel.
.is_invalid()BoolTrue if this Form yields the INVALID sentinel.
.is_sentinel()BoolTrue if this Form yields either sentinel (EMPTY or INVALID).
.not_empty()BoolTrue if this Form does not yield EMPTY.
.not_invalid()BoolTrue if this Form does not yield INVALID.

Undocumented: example.

On this page