math: Math Utilities

Table of Content:

Introduction

The math: module provides mathematical functions and constants.

Function usages are given in the same format as in the reference doc for the builtin module. In particular, all the commands in this module conform to the pattern of commands that operate on numbers.

Variables

$math:e

$math:e

The value of e: 2.718281…. This variable is read-only.

$math:pi

$math:pi

The value of π: 3.141592…. This variable is read-only.

Functions

math:abs

math:abs $number

Computes the absolute value $number. Examples:

~> math:abs 1.2
▶ (float64 1.2)
~> math:abs -5.3
▶ (float64 5.3)

math:acos

math:acos $number

Outputs the arccosine of $number, in radians (not degrees). Examples:

~> math:acos 1
▶ (float64 1)
~> math:acos 1.00001
▶ (float64 NaN)

math:acosh

math:acosh $number

Outputs the inverse hyperbolic cosine of $number. Examples:

~> math:acosh 1
▶ (float64 0)
~> math:acosh 0
▶ (float64 NaN)

math:asin

math:asin $number

Outputs the arcsine of $number, in radians (not degrees). Examples:

~> math:asin 0
▶ (float64 0)
~> math:asin 1
▶ (float64 1.5707963267948966)
~> math:asin 1.00001
▶ (float64 NaN)

math:asinh

math:asinh $number

Outputs the inverse hyperbolic sine of $number. Examples:

~> math:asinh 0
▶ (float64 0)
~> math:asinh inf
▶ (float64 +Inf)

math:atan

math:atan $number

Outputs the arctangent of $number, in radians (not degrees). Examples:

~> math:atan 0
▶ (float64 0)
~> math:atan $math:inf
▶ (float64 1.5707963267948966)

math:atanh

math:atanh $number

Outputs the inverse hyperbolic tangent of $number. Examples:

~> math:atanh 0
▶ (float64 0)
~> math:atanh 1
▶ (float64 +Inf)

math:ceil

math:ceil $number

Computes the ceiling of $number. Read the Go documentation for the details of how this behaves. Examples:

~> math:ceil 1.1
▶ (float64 2)
~> math:ceil -2.3
▶ (float64 -2)

math:cos

math:cos $number

Computes the cosine of $number in units of radians (not degrees). Examples:

~> math:cos 0
▶ (float64 1)
~> math:cos 3.14159265
▶ (float64 -1)

math:cosh

math:cosh $number

Computes the hyperbolic cosine of $number. Example:

~> math:cosh 0
▶ (float64 1)

math:floor

math:floor $number

Computes the floor of $number. Read the Go documentation for the details of how this behaves. Examples:

~> math:floor 1.1
▶ (float64 1)
~> math:floor -2.3
▶ (float64 -3)

math:is-inf

math:is-inf &sign=0 $number

Tests whether the number is infinity. If sign > 0, tests whether $number is positive infinity. If sign < 0, tests whether $number is negative infinity. If sign == 0, tests whether $number is either infinity.

~> math:is-inf 123
▶ $false
~> math:is-inf inf
▶ $true
~> math:is-inf -inf
▶ $true
~> math:is-inf &sign=1 inf
▶ $true
~> math:is-inf &sign=-1 inf
▶ $false
~> math:is-inf &sign=-1 -inf
▶ $true

math:is-nan

math:is-nan $number

Tests whether the number is a NaN (not-a-number).

~> math:is-nan 123
▶ $false
~> math:is-nan (float64 inf)
▶ $false
~> math:is-nan (float64 nan)
▶ $true

math:log

math:log $number

Computes the natural (base e) logarithm of $number. Examples:

~> math:log 1.0
▶ (float64 1)
~> math:log -2.3
▶ (float64 NaN)

math:log10

math:log10 $number

Computes the base 10 logarithm of $number. Examples:

~> math:log10 100.0
▶ (float64 2)
~> math:log10 -1.7
▶ (float64 NaN)

math:log2

math:log2 $number

Computes the base 2 logarithm of $number. Examples:

~> math:log2 8
▶ (float64 3)
~> math:log2 -5.3
▶ (float64 NaN)

math:max

math:max $number...

Outputs the maximum number in the arguments. If there are no arguments an exception is thrown. If any number is NaN then NaN is output.

Examples:

~> put ?(math:max)
▶ ?(fail 'arity mismatch: arguments here must be 1 or more values, but is 0 values')
~> math:max 3
▶ (float 3)
~> math:max 3 5 2
▶ (float 5)
~> range 100 | math:max (all)
▶ (float 99)

math:min

math:min $number...

Outputs the minimum number in the arguments. If there are no arguments an exception is thrown. If any number is NaN then NaN is output.

Examples:

~> put ?(math:min)
▶ ?(fail 'arity mismatch: arguments here must be 1 or more values, but is 0 values')
~> math:min 3
▶ (float 3)
~> math:min 3 5 2
▶ (float 2)
~> range 100 | math:min (all)
▶ (float 0)

math:pow

math:pow $base $exponent

Output the result of raising $base to the power of $exponent. Examples:

~> math:pow 3 2
▶ (float64 9)
~> math:pow -2 2
▶ (float64 4)

See also math:pow10.

math:pow10

math:pow10 $exponent

Output the result of raising ten to the power of $exponent which must be an integer. Note that $exponent > 308 results in +Inf and $exponent < -323 results in zero. Examples:

~> math:pow10 2
▶ (float64 100)
~> math:pow10 -3
▶ (float64 0.001)

See also math:pow.

math:round

math:round $number

Outputs the nearest integer, rounding half away from zero.

~> math:round -1.1
▶ (float64 -1)
~> math:round 2.5
▶ (float64 3)

math:round-to-even

math:round-to-even $number

Outputs the nearest integer, rounding ties to even. Examples:

~> math:round-to-even -1.1
▶ (float64 -1)
~> math:round-to-even 2.5
▶ (float64 2)

math:sin

math:sin $number

Computes the sine of $number in units of radians (not degrees). Examples:

~> math:sin 0
▶ (float64 0)
~> math:sin 3.14159265
▶ (float64 3.5897930298416118e-09)

math:sinh

math:sinh $number

Computes the hyperbolic sine of $number. Example:

~> math:sinh 0
▶ (float64 0)

math:sqrt

math:sqrt $number

Computes the square-root of $number. Examples:

~> math:sqrt 0
▶ (float64 0)
~> math:sqrt 4
▶ (float64 2)
~> math:sqrt -4
▶ (float64 NaN)

math:tan

math:tan $number

Computes the tangent of $number in units of radians (not degrees). Examples:

~> math:tan 0
▶ (float64 0)
~> math:tan 3.14159265
▶ (float64 -0.0000000035897930298416118)

math:tanh

math:tanh $number

Computes the hyperbolic tangent of $number. Example:

~> math:tanh 0
▶ (float64 0)

math:trunc

math:trunc $number

Outputs the integer portion of $number.

~> math:trunc -1.1
▶ (float64 -1)
~> math:trunc 2.5
▶ (float64 2)