7.2 Numbers
7.2.1 Flonums
7.2.2 Fixnums
On this page:
(+  )
(-)
(*)
(/  )
(**)
(<)
(<=)
(==)
(!=)
(>=)
(>)
abs
min
max
floor
ceiling
round
truncate
sin
cos
tan
asin
acos
atan
sqrt
log
exp
to_  single
from_  int
to_  int
bit_  field
8.15.0.12
7.2.1 Flonums🔗ℹ

 import: rhombus/flonum package: rhombus-lib

A flonum is a number that is represented using an IEEE 64-bit floating-point representation. A flonum is a plain number, so number operations like + work on flonums and mixtures of flonums with other numbers. In some cases, using functions and operators from rhombus/flonum can reduce run time by allowing flonum operations to more efficiently communicate intermediate results.

The functions exported by rhombus/flonum explicitly require flonum arguments and produce flonum results. They can produce different results from functions like math.sqrt, typically returning #nan when the result would not be a flonum, such as when a negative number is passed to math.sqrt.

Some operations, like +, statically specialize for performance when all arguments have static information for the Flonum annotation. There is no performance difference between that inferred specialization and these operators, and a conversion is inferred only when the result value is the same for unspecialized and flonum-specific operations.

operator

operator ((x :: Flonum) flonum.(+) (y :: Flonum)) :: Flonum:

  ~order: addition

 

operator

operator ((x :: Flonum) flonum.(-) (y :: Flonum)) :: Flonum:

  ~order: addition

 

operator

operator (flonum.(-) (x :: Flonum)) :: Flonum:

  ~order: addition

 

operator

operator ((x :: Flonum) flonum.(*) (y :: Flonum)) :: Flonum:

  ~order: multiplication

 

operator

operator ((x :: Flonum) flonum.(/) (y :: Flonum)) :: Flonum:

  ~order: multiplication

 

operator

operator ((x :: Flonum) flonum.(**) (y :: Flonum)) :: Flonum:

  ~order: exponentiation

The same as operators like +, but restricted to flonum arguments and results. Note that operators like + statically specialize for performance when all arguments have static information associated with the Flonum annotation.

operator

operator ((x :: Flonum) flonum.(<) (y :: Flonum)) :: Boolean:

  ~order: order_comparison

 

operator

operator ((x :: Flonum) flonum.(<=) (y :: Flonum)) :: Boolean:

  ~order: order_comparison

 

operator

operator ((x :: Flonum) flonum.(==) (y :: Flonum)) :: Boolean:

  ~order: order_comparison

 

operator

operator ((x :: Flonum) flonum.(!=) (y :: Flonum)) :: Boolean:

  ~order: order_comparison

 

operator

operator ((x :: Flonum) flonum.(>=) (y :: Flonum)) :: Boolean:

  ~order: order_comparison

 

operator

operator ((x :: Flonum) flonum.(>) (y :: Flonum)) :: Boolean:

  ~order: order_comparison

The same as operators like <, but restricted to flonum arguments. Note that operators like < statically specialize for performance when all arguments have static information associated with the Flonum annotation.

function

fun flonum.abs(x :: Flonum) :: Flonum

 

function

fun flonum.min(x :: Flonum, y :: Flonum, ...) :: Flonum

 

function

fun flonum.max(x :: Flonum, y :: Flonum, ...) :: Flonum

 

function

fun flonum.floor(x :: Flonum) :: Flonum

 

function

fun flonum.ceiling(x :: Flonum) :: Flonum

 

function

fun flonum.round(x :: Flonum) :: Flonum

 

function

fun flonum.truncate(x :: Flonum) :: Flonum

 

function

fun flonum.sin(x :: Flonum) :: Flonum

 

function

fun flonum.cos(x :: Flonum) :: Flonum

 

function

fun flonum.tan(x :: Flonum) :: Flonum

 

function

fun flonum.asin(x :: Flonum) :: Flonum

 

function

fun flonum.acos(x :: Flonum) :: Flonum

 

function

fun flonum.atan(x :: Flonum) :: Flonum

 

function

fun flonum.sqrt(x :: Flonum) :: Flonum

 

function

fun flonum.log(x :: Flonum) :: Flonum

 

function

fun flonum.exp(x :: Flonum) :: Flonum

The same as functions like math.abs, but restricted to flonum arguments and results.

Some related operations, such as math.sqrt, do not always produce flonum results for flonum arguments. In those cases, the corresponding function like flonum.sqrt produces #nan, instead. For example, flonum.sqrt(-1.0) is #nan instead of the complex number #{0.0+1.0i}.

function

fun flonum.to_single(x :: Flonum) :: Flonum

Potentially reduces the precision of a flonum so that it matches a number that has a IEEE 32-bit floating-point representation.

function

fun flonum.from_int(n :: Int) :: Flonum

 

function

fun flonum.to_int(x :: Flonum) :: Int

Converts flonums from and to integers. The flonum.from_int function produces #inf or #neginf when n has a large enough magnitude. The flonum.to_int function truncates flonums that have a fractional component, and it reports an error when given #inf, #neginf, or #nan.

function

fun flonum.bit_field(x :: Flonum,

                     start :: Int.in(0, 64),

                     end :: Int.in(0, 64))

  :: Int

Returns an integer whose bitwise representation matches the bits of x’s IEEE representation from start (inclusive) to end (exclusive). The end index must be at least as large as start. Bit 0 corresponds to the low bit of the mantissa, while bit 63 is the sign bit.