ESC
No recent searches
Search by
Standard Library  /  Rational

Rational

Utilities for working with the Rational type.

Edit on GitHub
Added in 0.6.0 No other changes yet.
from "rational" include Rational
1/2r
3/4r

Values

Functions and constants included in the Rational module.

Rational.fromNumber

Added in 0.6.0 No other changes yet.
fromNumber : (number: Number) => Rational

Converts a Number to a Rational.

Parameters:

number: The value to convert

Returns:

Rational: The Number represented as a Rational

Rational.toNumber

Added in 0.6.0 No other changes yet.
toNumber : (rational: Rational) => Number

Converts a Rational to a Number.

Parameters:

rational: The value to convert

Returns:

Number: The Rational represented as a Number

Rational.numerator

Added in 0.6.0 No other changes yet.
numerator : (x: Rational) => Number

Finds the numerator of the rational number.

Parameters:

x: The rational number to inspect

Returns:

Number: The numerator of the rational number

Rational.denominator

Added in 0.6.0 No other changes yet.
denominator : (x: Rational) => Number

Finds the denominator of the rational number.

Parameters:

x: The rational number to inspect

Returns:

Number: The denominator of the rational number

Rational.toIntegerRatio

Added in 0.6.0 No other changes yet.
toIntegerRatio : (x: Rational) => (Number, Number)

Gets the numerator and denominator of the rational.

Parameters:

x: The rational to split

Returns:

(Number, Number): The numerator and denominator of the rational

Examples:

Rational.toIntegerRatio(1/2r) == (1, 2)
Rational.toIntegerRatio(2/8r) == (1, 4)

Rational.fromIntegerRatio

Added in 0.6.0 No other changes yet.
fromIntegerRatio : (numerator: Number, denominator: Number) => Rational

Creates a rational from a numerator and denominator.

Parameters:

numerator: The numerator
denominator: The denominator

Returns:

Rational: The reduced rational

Throws:

InvalidArgument(String)

  • If the numerator is not an integer
  • If the denominator is not an integer

Examples:

Rational.fromIntegerRatio(1, 2) == 1/2r
Rational.fromIntegerRatio(2, 8) == 1/4r

Rational.(+)

Added in 0.6.0 No other changes yet.
(+) : (x: Rational, y: Rational) => Rational

Computes the sum of its operands.

Parameters:

x: The first operand
y: The second operand

Returns:

Rational: The sum of the two operands

Examples:

use Rational.{ (+) }
assert 1/2r + 1/4r == 3/4r

Rational.(-)

Added in 0.6.0 No other changes yet.
(-) : (x: Rational, y: Rational) => Rational

Computes the difference of its operands.

Parameters:

x: The first operand
y: The second operand

Returns:

Rational: The difference of the two operands

Examples:

use Rational.{ (-) }
assert 1/2r - 1/4r == 1/4r

Rational.(*)

Added in 0.6.0 No other changes yet.
(*) : (x: Rational, y: Rational) => Rational

Computes the product of its operands.

Parameters:

x: The first operand
y: The second operand

Returns:

Rational: The product of the two operands

Examples:

use Rational.{ (*) }
assert 1/2r * 1/4r == 1/8r

Rational.(/)

Added in 0.6.0 No other changes yet.
(/) : (x: Rational, y: Rational) => Rational

Computes the quotient of its operands.

Parameters:

x: The first operand
y: The second operand

Returns:

Rational: The quotient of the two operands

Examples:

use Rational.{ (/) }
assert 1/2r / 1/4r == 2/1r

Rational.(==)

Added in 0.6.0 No other changes yet.
(==) : (x: Rational, y: Rational) => Bool

Checks if the first value is equal to the second value.

Parameters:

x: The first value
y: The second value

Returns:

Bool: true if the first value is equal to the second value or false otherwise

Examples:

use Rational.{ (==) }
assert 1/2r == 1/2r

Rational.(!=)

Added in 0.6.0 No other changes yet.
(!=) : (x: Rational, y: Rational) => Bool

Checks if the first value is not equal to the second value.

Parameters:

x: The first value
y: The second value

Returns:

Bool: true if the first value is not equal to the second value or false otherwise

Examples:

use Rational.{ (!=) }
assert 1/2r != 1/4r

Rational.(<)

Added in 0.6.0 No other changes yet.
(<) : (x: Rational, y: Rational) => Bool

Checks if the first value is less than the second value.

Parameters:

x: The first value
y: The second value

Returns:

Bool: true if the first value is less than the second value or false otherwise

Examples:

use Rational.{ (<) }
assert 1/4r < 1/2r

Rational.(>)

Added in 0.6.0 No other changes yet.
(>) : (x: Rational, y: Rational) => Bool

Checks if the first value is greater than the second value.

Parameters:

x: The first value
y: The second value

Returns:

Bool: true if the first value is greater than the second value or false otherwise

Examples:

use Rational.{ (>) }
assert 1/2r > 1/4r

Rational.(<=)

Added in 0.6.0 No other changes yet.
(<=) : (x: Rational, y: Rational) => Bool

Checks if the first value is less than or equal to the second value.

Parameters:

x: The first value
y: The second value

Returns:

Bool: true if the first value is less than or equal to the second value or false otherwise

Examples:

use Rational.{ (<=) }
assert 1/4r <= 1/2r
use Rational.{ (<=) }
assert 1/2r <= 1/2r

Rational.(>=)

Added in 0.6.0 No other changes yet.
(>=) : (x: Rational, y: Rational) => Bool

Checks if the first value is greater than or equal to the second value.

Parameters:

x: The first value
y: The second value

Returns:

Bool: true if the first value is greater than or equal to the second value or false otherwise

Examples:

use Rational.{ (>=) }
assert 1/2r >= 1/4r
use Rational.{ (>=) }
assert 1/2r >= 1/2r

Sign up for farm-to-inbox developer news

You can unsubscribe at any time. Read our privacy policy.

Copyright © 2024 The Grain Programming Language