Esc
Start typing to search...

Real Module

Trait methods for the Real a constraint (Float and Decimal).

The Real trait constrains a type variable to Float or Decimal — the continuous-domain numeric types. It provides two methods:

  • toRational — convert a Float or Decimal to a Decimal representation.
  • fromInt — inject an integer literal into a Real type.

See the Math module for transcendental functions (sqrt, exp, log, etc.) that are constrained by Real a.

Functions

Real.fromInt

Real a => Int -> a

Convert an integer to a Float or Decimal.

Example:
import Real

let x : Decimal = Real.fromInt 5
x
Try it

Notes: The concrete type (Float or Decimal) is determined by the surrounding context or a type annotation. A bare Real.fromInt 5 without annotation produces AmbiguousType.

See also: Real.toRational

Real.toRational

Real a => a -> Decimal

Convert a Float or Decimal to its Decimal representation.

Example:
import Real

Real.toRational 3.14
Try it

Notes: For Float input, the conversion uses Decimal::try_from(f64). This may lose precision for floats that cannot be represented exactly in decimal form. Decimal input is returned unchanged.

See also: Real.fromInt