finos / morphir-elm / Morphir.SDK.Number

This module provides a way to represent a number without the risk of rounding issues or division by zero for any of the basic operations: +, -, *, /. More accurately a Number represents an arbitrary-precision rational number. If you need irrational numbers please use a Float.


type Number
    = Rational BigInt BigInt

Represents an arbitrary-precision rational number.

Convert from

fromInt : Basics.Int -> Number

Create a Number by converting it from an Int

Comparison

equal : Number -> Number -> Basics.Bool

Checks if two numbers are equal.

equal one one == True

equal one (divide ten ten) == True

equal one zero == False

notEqual : Number -> Number -> Basics.Bool

Checks if two numbers are not equal.

notEqual one zero == True

notEqual zero one == True

notEqual one one == False

lessThan : Number -> Number -> Basics.Bool

Checks if the first number is less than the second

lessThanOrEqual : Number -> Number -> Basics.Bool

Checks if the first number is less than or equal to the second

greaterThan : Number -> Number -> Basics.Bool

Checks if the first number is greater than the second

greaterThanOrEqual : Number -> Number -> Basics.Bool

Checks if the first number is greater or equal than the second

Arithmetic

add : Number -> Number -> Number

Adds two numbers together.

subtract : Number -> Number -> Number

Subtracts one number from the other.

multiply : Number -> Number -> Number

Multiplies two numbers together

divide : Number -> Number -> Result DivisionByZero Number

Division

abs : Number -> Number

Takes the absolute value of the number

negate : Number -> Number

Negate the given number, thus flipping the sign.

reciprocal : Number -> Number

Calculates the reciprocal of the number

Convert to

toFractionalString : Number -> String

Create a fractional representation of the number

toDecimal : Number -> Maybe Morphir.SDK.Decimal.Decimal

Turn a number into a decimal. NOTE: it is possible for this operation to fail if the Number is a rational number for 0.

coerceToDecimal : Morphir.SDK.Decimal.Decimal -> Number -> Morphir.SDK.Decimal.Decimal

Turn a number into a decimal, by providing a default value in the case things go awry.

Misc

simplify : Number -> Maybe Number

Tries to simplify the number.

isSimplified : Number -> Basics.Bool

Tells if the number is simplified

Constants

zero : Number

Constant for 0

one : Number

Constant for one