Number Types

Numbers in Idio are either “small” integer fixnums or “arbitrary” precision bignums which can be integers, floating point numbers and support a notion of exactness.

There is no support for non-finite quantities such as Nan or infinities.

You can also use the C base types, C/char, C/int, etc. and supported typedefs thereof, although they are usually passed verbatim within C-oriented modules like libc and operations involving them only accept like types.

Reader Forms

Reading numbers or, rather, identifying numbers as distinct from “words”, is complicated as we need to differentiate between, say, 3e3, a number, and 2pi, a symbol (almost certainly referencing a number).

Broadly, a number looks like:

  • [+-]?[0-9]+

  • [+-]?[0-9]+.[0-9]*

  • [+-]?[0-9]+.[0-9]*E[+-]?[0-9]+

(There must be a numeric part before any . otherwise it will be interpreted as the value-index operator.)

The exponent character, E in the last example, can be one of several characters:

  • for base-16 numbers (see below): e and E quite obviously clash with the possible digits of hexadecimal numbers so for base-16 numbers you can use the exponent characters s/S or l/L

  • other exponent-able numbers can use d/D, e/E, f/F – and s/S and l/L

Hence, +10, 1., -20.1, 0.3e1, -4e-5, 6L7 are all valid number formats and a number will be constructed by the reader.

Non-base-10 Numbers

There are several reader input forms for non-base-10 numbers all of which effectively call read-number with an appropriate radix:

Non-base-10 reader number formats

form

radix

example

decimal equivalent

#b

2

#b101

5

#o

8

#o101

65

#d

10

#d101

101

#x

16

#x101

257

read-number supports bases up to 36 although there are no dedicated reader input forms.

Number Predicates

function integer? o

test if o is an integer

a fixnum or an integer bignum

Param o:

object to test

Return:

#t if o is an integer, #f otherwise

function number? o

test if o is a number

fixnum or bignum

Param o:

object to test

Return:

#t if o is an number, #f otherwise

Number Constructors

function unicode/unicode->integer cp

convert cp to an integer

Param cp:

unicode to convert

Return:

integer conversion of cp

Rtype:

fixnum

function read-number src [radix]

read a number from src

Param str:

source to read from

Type str:

handle or string

Param radix:

use radix, defaults to 10

Type radix:

fixnum, optional

Return:

number

read-number supports bases up to 36 for which the normal hexadecimal digits are extended to all ASCII Alphabetic code points: 0-9a-z and 0-9A-Z

Example:

read-number "i9" 19               ; 351 (18 * 19 + 9)

Number Functions

function + [n ...]

add numbers

Param n:

number

Type n:

number

Return:

the result of adding the arguments

Rtype:

number

If any arguments are bignums then every argument is converted to a bignum before calculating the result.

An attempt is made to convert the result to a fixnum if possible.

+ with no argument returns 0.

function - n1 [n2 ...]

subtract numbers

Param n1:

number

Type n1:

number

Param n2:

number

Type n2:

number

Return:

the result of subtracting the arguments

Rtype:

number

If any arguments are bignums then every argument is converted to a bignum before calculating the result.

An attempt is made to convert the result to a fixnum if possible.

- with one argument is equivalent to 0 - n1.

- with more than one argument is equivalent to r = n1 then repeating across the remaining arguments with r = r - n2, r = r - n3, etc..

function * [n ...]

multiply numbers

Param n:

number

Type n:

number

Return:

the result of multiplying the arguments

Rtype:

number

If any arguments are bignums then every argument is converted to a bignum before calculating the result.

An attempt is made to convert the result to a fixnum if possible.

* with no argument returns 1.

function / n1 [n2 ...]

divide numbers

Param n1:

number

Type n1:

number

Param n2:

number

Type n2:

number

Return:

the result of dividing the arguments

Rtype:

number

All arguments are converted to bignums before calculating the result.

An attempt is made to convert the result to a fixnum if possible.

/ with one argument is equivalent to 1 / n1.

/ with more than one argument is equivalent to r = n1 then repeating across the remaining arguments with r = r / n2, r = r / n3, etc..

function lt n1 [n2 ...]

apply the less-than comparator to numbers

Param n1:

number

Type n1:

number

Param n2:

number

Type n2:

number

Return:

the result of comparing the arguments

Rtype:

boolean

If any arguments are bignums then every argument is converted to a bignum before calculating the result.

lt with one argument is #t.

lt with more than one argument has each subsequent argument compared to the one to its left. The result is #t if all subsequent arguments are less-than the argument to their left otherwise the result is #f.

function le n1 [n2 ...]

apply the less-than-or-equal comparator to numbers

Param n1:

number

Type n1:

number

Param n2:

number

Type n2:

number

Return:

the result of comparing the arguments

Rtype:

boolean

If any arguments are bignums then every argument is converted to a bignum before calculating the result.

le with one argument is #t.

le with more than one argument has each subsequent argument compared to the one to its left. The result is #t if all subsequent arguments are less-than-or-equal to the argument to their left otherwise the result is #f.

function eq n1 [n2 ...]

apply the equality comparator to numbers

Param n1:

number

Type n1:

number

Param n2:

number

Type n2:

number

Return:

the result of comparing the arguments

Rtype:

boolean

If any arguments are bignums then every argument is converted to a bignum before calculating the result.

eq with one argument is #t.

eq with more than one argument has each subsequent argument compared to the one to its left. The result is #t if all subsequent arguments are equal to the argument to their left otherwise the result is #f.

function ne n1 [n2 ...]

apply the inequality comparator to numbers

Param n1:

number

Type n1:

number

Param n2:

number

Type n2:

number

Return:

the result of comparing the arguments

Rtype:

boolean

If any arguments are bignums then every argument is converted to a bignum before calculating the result.

ne with one argument is #t.

ne with more than one argument has each subsequent argument compared to the one to its left. The result is #t if all subsequent arguments are not equal to the argument to their left otherwise the result is #f.

function ge n1 [n2 ...]

apply the greater-than-or-equal comparator to numbers

Param n1:

number

Type n1:

number

Param n2:

number

Type n2:

number

Return:

the result of comparing the arguments

Rtype:

boolean

If any arguments are bignums then every argument is converted to a bignum before calculating the result.

ge with one argument is #t.

ge with more than one argument has each subsequent argument compared to the one to its left. The result is #t if all subsequent arguments are greater-than-or-equal the argument to their left otherwise the result is #f.

function gt n1 [n2 ...]

apply the greater-than comparator to numbers

Param n1:

number

Type n1:

number

Param n2:

number

Type n2:

number

Return:

the result of comparing the arguments

Rtype:

boolean

If any arguments are bignums then every argument is converted to a bignum before calculating the result.

gt with one argument is #t.

gt with more than one argument has each subsequent argument compared to the one to its left. The result is #t if all subsequent arguments are greater-than the argument to their left otherwise the result is #f.

function binary-* n1 n2

multiply two numbers

Param n1:

number

Type n1:

number

Param n2:

number

Type n2:

number

Return:

the result of multiplying the arguments

Rtype:

number

If the arguments are C/ types then the C arithmetic function is invoked.

Otherwise, if any arguments are bignums then every argument is converted to a bignum before calculating the result.

An attempt is made to convert the result to a fixnum if possible.

The use of the function binary-* is normally the result of the * infix operator.

function binary-+ n1 n2

add two numbers

Param n1:

number

Type n1:

number

Param n2:

number

Type n2:

number

Return:

the result of adding the arguments

Rtype:

number

If the arguments are C/ types then the C arithmetic function is invoked.

Otherwise, if any arguments are bignums then every argument is converted to a bignum before calculating the result.

An attempt is made to convert the result to a fixnum if possible.

The use of the function binary-+ is normally the result of the + infix operator.

function binary-- n1 n2

subtract two numbers

Param n1:

number

Type n1:

number

Param n2:

number

Type n2:

number

Return:

the result of subtracting the arguments

Rtype:

number

If the arguments are C/ types then the C arithmetic function is invoked.

Otherwise, if any arguments are bignums then every argument is converted to a bignum before calculating the result.

An attempt is made to convert the result to a fixnum if possible.

The use of the function binary-- is normally the result of the - infix operator.

function binary-/ n1 n2

divide two numbers

Param n1:

number

Type n1:

number

Param n2:

number

Type n2:

number

Return:

the result of dividing the arguments

Rtype:

number

If the arguments are C/ types then the C arithmetic function is invoked.

Otherwise, all arguments are converted to bignums before calculating the result.

An attempt is made to convert the result to a fixnum if possible.

The use of the function binary-/ is normally the result of the / infix operator.

function binary-lt n1 n2

apply the less-than comparator to numbers

Param n1:

number

Type n1:

number

Param n2:

number

Type n2:

number

Return:

the result of comparing the arguments

Rtype:

boolean

If any arguments are bignums then every argument is converted to a bignum before calculating the result.

The use of the function binary-lt is normally the result of the lt infix operator.

function binary-le n1 n2

apply the less-than-or-equal comparator to numbers

Param n1:

number

Type n1:

number

Param n2:

number

Type n2:

number

Return:

the result of comparing the arguments

Rtype:

boolean

If any arguments are bignums then every argument is converted to a bignum before calculating the result.

The use of the function binary-le is normally the result of the le infix operator.

function binary-eq n1 n2

apply the equality comparator to numbers

Param n1:

number

Type n1:

number

Param n2:

number

Type n2:

number

Return:

the result of comparing the arguments

Rtype:

boolean

If any arguments are bignums then every argument is converted to a bignum before calculating the result.

The use of the function binary-eq is normally the result of the eq infix operator.

function binary-ne n1 n2

apply the inequality comparator to numbers

Param n1:

number

Type n1:

number

Param n2:

number

Type n2:

number

Return:

the result of comparing the arguments

Rtype:

boolean

If any arguments are bignums then every argument is converted to a bignum before calculating the result.

The use of the function binary-ne is normally the result of the ne infix operator.

function binary-ge n1 n2

apply the greater-than-or-equal comparator to numbers

Param n1:

number

Type n1:

number

Param n2:

number

Type n2:

number

Return:

the result of comparing the arguments

Rtype:

boolean

If any arguments are bignums then every argument is converted to a bignum before calculating the result.

The use of the function binary-ge is normally the result of the ge infix operator.

function binary-gt n1 n2

apply the greater-than comparator to numbers

Param n1:

number

Type n1:

number

Param n2:

number

Type n2:

number

Return:

the result of comparing the arguments

Rtype:

boolean

If any arguments are bignums then every argument is converted to a bignum before calculating the result.

The use of the function binary-gt is normally the result of the gt infix operator.

Last built at 2024-05-17T06:10:58Z+0000 from 62cca4c (dev) for Idio 0.3.b.6