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
Array
Array
Array.make
Array.length
Array.get
Array.set
Array.contains
Array.copy
Array.to_  list
Array.to_  sequence
0.45+9.2.0.2

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.

annotation

flonum.Array

Matches any flonum array, which is like a normal array, but contains only flonums. A flonum array is always mutable, but it does not satisfy Array or MutableArray.

A flonum array potentially provides better performance than a normal array of flonums, because the elements are known to be flonums without a check on access. In particular, flonum calculations that read and write flonum arrays may perform better by reducing allocation compared to normal arrays.

Like an array, a flonum array is indexable using [], assignable via [] and :=, supports membership tests using the in operator, and can be used as sequence. Two flonum arrays are equal by is_now as long as they have the same length and their elements are pairwise equal by flonum.(==).

function

fun flonum.Array(x :: Fxnum, ...) :: flonum.Array

 

function

fun flonum.Array.make(len :: Nat, x :: Flonum = 0.0) :: flonum.Array

Like Array and Array.make, but creates a flonum array.

method

method (arr :: flonum.Array).length() :: Int

 

method

method (arr :: flonum.Array).get(n :: Nat) :: Flonum

 

method

method (arr :: flonum.Array).set(n :: Nat, x :: Flonum) :: Void

 

method

method (arr :: flonum.Array).contains(v :: Any) :: Boolean

 

method

method (arr :: flonum.Array).copy(

  start :: Nat = 0,

  end :: Nat = arr.length()

) :: flonum.Array

 

method

method (arr :: flonum.Array).to_list() :: List.of(Flonum)

 

method

method (arr :: flonum.Array).to_sequence()

  :: Sequence.assume_of(Flonum)

The flonum.Array.contains method accepts any argument value to find, but it will only succeed for flonums. The comparsion operation is always flonum.(==).