code / tomo

Lines41.3K C23.7K Markdown9.7K YAML5.0K Tomo2.3K
7 others 763
Python231 Shell230 make212 INI47 Text21 SVG16 Lua6
(1.2K lines)

% API

Builtins

Num

Num.1_PI

Num.1_PI : Num

The constant $\frac{1}{\pi}$.

Num.2_PI

Num.2_PI : Num

The constant $2 \times \pi$.

Num.2_SQRTPI

Num.2_SQRTPI : Num

The constant $2 \times \sqrt{\pi}$.

Num.E

Num.E : Num

The base of the natural logarithm ($e$).

Num.INF

Num.INF : Num

Positive infinity.

Num.LN10

Num.LN10 : Num

The natural logarithm of 10.

Num.LN2

Num.LN2 : Num

The natural logarithm of 2.

Num.LOG2E

Num.LOG2E : Num

The base 2 logarithm of $e$

Num.PI

Num.PI : Num

Pi ($\pi$).

Num.PI_2

Num.PI_2 : Num

$\frac{\pi}{2}$

Num.PI_4

Num.PI_4 : Num

$\frac{\pi}{4}$

Num.SQRT1_2

Num.SQRT1_2 : Num

$\sqrt{\frac{1}{2}}$

Num.SQRT2

Num.SQRT2 : Num

$\sqrt{2}$

Num.TAU

Num.TAU : Num

Tau ($2 \times \pi$)

Num.abs

Num.abs : func(n: Num -> Num)

Calculates the absolute value of a number.

Argument Type Description Default
n Num The number whose absolute value is to be computed. -

Return: The absolute value of n.

Example:

assert (-3.5).abs() == 3.5

Num.acos

Num.acos : func(x: Num -> Num)

Computes the arc cosine of a number.

Argument Type Description Default
x Num The number for which the arc cosine is to be calculated. -

Return: The arc cosine of x in radians.

Example:

assert (0.0).acos().near(1.5707963267948966)

Num.acosh

Num.acosh : func(x: Num -> Num)

Computes the inverse hyperbolic cosine of a number.

Argument Type Description Default
x Num The number for which the inverse hyperbolic cosine is to be calculated. -

Return: The inverse hyperbolic cosine of x.

Example:

assert (1.0).acosh() == 0

Num.asin

Num.asin : func(x: Num -> Num)

Computes the arc sine of a number.

Argument Type Description Default
x Num The number for which the arc sine is to be calculated. -

Return: The arc sine of x in radians.

Example:

assert (0.5).asin().near(0.5235987755982989)

Num.asinh

Num.asinh : func(x: Num -> Num)

Computes the inverse hyperbolic sine of a number.

Argument Type Description Default
x Num The number for which the inverse hyperbolic sine is to be calculated. -

Return: The inverse hyperbolic sine of x.

Example:

assert (0.0).asinh() == 0

Num.atan

Num.atan : func(x: Num -> Num)

Computes the arc tangent of a number.

Argument Type Description Default
x Num The number for which the arc tangent is to be calculated. -

Return: The arc tangent of x in radians.

Example:

assert (1.0).atan().near(0.7853981633974483)

Num.atan2

Num.atan2 : func(x: Num, y: Num -> Num)

Computes the arc tangent of the quotient of two numbers.

Argument Type Description Default
x Num The numerator. -
y Num The denominator. -

Return: The arc tangent of x/y in radians.

Example:

assert Num.atan2(1, 1).near(0.7853981633974483)

Num.atanh

Num.atanh : func(x: Num -> Num)

Computes the inverse hyperbolic tangent of a number.

Argument Type Description Default
x Num The number for which the inverse hyperbolic tangent is to be calculated. -

Return: The inverse hyperbolic tangent of x.

Example:

assert (0.5).atanh().near(0.5493061443340549)

Num.cbrt

Num.cbrt : func(x: Num -> Num)

Computes the cube root of a number.

Argument Type Description Default
x Num The number for which the cube root is to be calculated. -

Return: The cube root of x.

Example:

assert (27.0).cbrt() == 3

Num.ceil

Num.ceil : func(x: Num -> Num)

Rounds a number up to the nearest integer.

Argument Type Description Default
x Num The number to be rounded up. -

Return: The smallest integer greater than or equal to x.

Example:

assert (3.2).ceil() == 4

Num.clamped

Num.clamped : func(x: Num, low: Num, high: Num -> Num)

Returns the given number clamped between two values so that it is within that range.

Argument Type Description Default
x Num The number to clamp. -
low Num The lowest value the result can take. -
high Num The highest value the result can take. -

Return: The first argument clamped between the other two arguments.

Example:

assert (2.5).clamped(5.5, 10.5) == 5.5

Num.copysign

Num.copysign : func(x: Num, y: Num -> Num)

Copies the sign of one number to another.

Argument Type Description Default
x Num The number whose magnitude will be copied. -
y Num The number whose sign will be copied. -

Return: A number with the magnitude of x and the sign of y.

Example:

assert (3.0).copysign(-1) == -3

Num.cos

Num.cos : func(x: Num -> Num)

Computes the cosine of a number (angle in radians).

Argument Type Description Default
x Num The angle in radians. -

Return: The cosine of x.

Example:

assert (0.0).cos() == 1

Num.cosh

Num.cosh : func(x: Num -> Num)

Computes the hyperbolic cosine of a number.

Argument Type Description Default
x Num The number for which the hyperbolic cosine is to be calculated. -

Return: The hyperbolic cosine of x.

Example:

assert (0.0).cosh() == 1

Num.erf

Num.erf : func(x: Num -> Num)

Computes the error function of a number.

Argument Type Description Default
x Num The number for which the error function is to be calculated. -

Return: The error function of x.

Example:

assert (0.0).erf() == 0

Num.erfc

Num.erfc : func(x: Num -> Num)

Computes the complementary error function of a number.

Argument Type Description Default
x Num The number for which the complementary error function is to be calculated. -

Return: The complementary error function of x.

Example:

assert (0.0).erfc() == 1

Num.exp

Num.exp : func(x: Num -> Num)

Computes the exponential function $e^x$ for a number.

Argument Type Description Default
x Num The exponent. -

Return: The value of $e^x$.

Example:

assert (1.0).exp().near(2.718281828459045)

Num.exp2

Num.exp2 : func(x: Num -> Num)

Computes $2^x$ for a number.

Argument Type Description Default
x Num The exponent. -

Return: The value of $2^x$.

Example:

assert (3.0).exp2() == 8

Num.expm1

Num.expm1 : func(x: Num -> Num)

Computes $e^x - 1$ for a number.

Argument Type Description Default
x Num The exponent. -

Return: The value of $e^x - 1$.

Example:

assert (1.0).expm1().near(1.7182818284590453)

Num.fdim

Num.fdim : func(x: Num, y: Num -> Num)

Computes the positive difference between two numbers.

Argument Type Description Default
x Num The first number. -
y Num The second number. -

Return: The positive difference $\max(0, x - y)$.

Example:

assert (5.0).fdim(3) == 2

Num.floor

Num.floor : func(x: Num -> Num)

Rounds a number down to the nearest integer.

Argument Type Description Default
x Num The number to be rounded down. -

Return: The largest integer less than or equal to x.

Example:

assert (3.7).floor() == 3

Num.hypot

Num.hypot : func(x: Num, y: Num -> Num)

Computes the Euclidean norm, $\sqrt{x^2 + y^2}$, of two numbers.

Argument Type Description Default
x Num The first number. -
y Num The second number. -

Return: The Euclidean norm of x and y.

Example:

assert Num.hypot(3, 4) == 5

Num.is_between

Num.is_between : func(x: Num, low: Num, high: Num -> Bool)

Determines if a number is between two numbers (inclusive).

Argument Type Description Default
x Num The integer to be checked. -
low Num One end of the range to check (inclusive). -
high Num The other end of the range to check (inclusive). -

Return: yes if a <= x and x <= b or b <= x and x <= a, otherwise no

Example:

assert (7.5).is_between(1, 10) == yes
assert (7.5).is_between(10, 1) == yes
assert (7.5).is_between(100, 200) == no
assert (7.5).is_between(1, 7.5) == yes

Num.isfinite

Num.isfinite : func(n: Num -> Bool)

Checks if a number is finite.

Argument Type Description Default
n Num The number to be checked. -

Return: yes if n is finite, no otherwise.

Example:

assert (1.0).isfinite() == yes
assert Num.INF.isfinite() == no

Num.isinf

Num.isinf : func(n: Num -> Bool)

Checks if a number is infinite.

Argument Type Description Default
n Num The number to be checked. -

Return: yes if n is infinite, no otherwise.

Example:

assert Num.INF.isinf() == yes
assert (1.0).isinf() == no

Num.j0

Num.j0 : func(x: Num -> Num)

Computes the Bessel function of the first kind of order 0.

Argument Type Description Default
x Num The number for which the Bessel function is to be calculated. -

Return: The Bessel function of the first kind of order 0 of x.

Example:

assert (0.0).j0() == 1

Num.j1

Num.j1 : func(x: Num -> Num)

Computes the Bessel function of the first kind of order 1.

Argument Type Description Default
x Num The number for which the Bessel function is to be calculated. -

Return: The Bessel function of the first kind of order 1 of x.

Example:

assert (0.0).j1() == 0

Num.log

Num.log : func(x: Num -> Num)

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

Argument Type Description Default
x Num The number for which the natural logarithm is to be calculated. -

Return: The natural logarithm of x.

Example:

assert Num.E.log() == 1

Num.log10

Num.log10 : func(x: Num -> Num)

Computes the base-10 logarithm of a number.

Argument Type Description Default
x Num The number for which the base-10 logarithm is to be calculated. -

Return: The base-10 logarithm of x.

Example:

assert (100.0).log10() == 2

Num.log1p

Num.log1p : func(x: Num -> Num)

Computes $\log(1 + x)$ for a number.

Argument Type Description Default
x Num The number for which $\log(1 + x)$ is to be calculated. -

Return: The value of $\log(1 + x)$.

Example:

assert (1.0).log1p().near(0.6931471805599453)

Num.log2

Num.log2 : func(x: Num -> Num)

Computes the base-2 logarithm of a number.

Argument Type Description Default
x Num The number for which the base-2 logarithm is to be calculated. -

Return: The base-2 logarithm of x.

Example:

assert (8.0).log2() == 3

Num.logb

Num.logb : func(x: Num -> Num)

Computes the binary exponent (base-2 logarithm) of a number.

Argument Type Description Default
x Num The number for which the binary exponent is to be calculated. -

Return: The binary exponent of x.

Example:

assert (8.0).logb() == 3

Num.mix

Num.mix : func(amount: Num, x: Num, y: Num -> Num)

Interpolates between two numbers based on a given amount.

Argument Type Description Default
amount Num The interpolation factor (between 0 and 1). -
x Num The starting number. -
y Num The ending number. -

Return: The interpolated number between x and y based on amount.

Example:

assert (0.5).mix(10, 20) == 15
assert (0.25).mix(10, 20) == 12.5

Num.near

Num.near : func(x: Num, y: Num, ratio: Num = 1e-9, min_epsilon: Num = 1e-9 -> Bool)

Checks if two numbers are approximately equal within specified tolerances. If two numbers are within an absolute difference or the ratio between the two is small enough, they are considered near each other.

Argument Type Description Default
x Num The first number. -
y Num The second number. -
ratio Num The relative tolerance. Default is 1e-9. 1e-9
min_epsilon Num The absolute tolerance. Default is 1e-9. 1e-9

Return: yes if x and y are approximately equal within the specified tolerances, no otherwise.

Example:

assert (1.0).near(1.000000001) == yes
assert (100.0).near(110, ratio=0.1) == yes
assert (5.0).near(5.1, min_epsilon=0.1) == yes

Num.nextafter

Num.nextafter : func(x: Num, y: Num -> Num)

Computes the next representable value after a given number towards a specified direction.

Argument Type Description Default
x Num The starting number. -
y Num The direction towards which to find the next representable value. -

Return: The next representable value after x in the direction of y.

Example:

assert (1.0).nextafter(1.1) == 1.0000000000000002

Num.parse

Num.parse : func(text: Text, remainder: &Text? = none -> Num?)

Converts a text representation of a number into a floating-point number.

Argument Type Description Default
text Text The text containing the number. -
remainder &Text? If non-none, this argument will be set to the remainder of the text after the matching part. If none, parsing will only succeed if the entire text matches. none

Return: The number represented by the text or none if the entire text can't be parsed as a number.

Example:

assert Num.parse("3.14") == 3.14
assert Num.parse("1e3") == 1000
assert Num.parse("1.5junk") == none
remainder : Text
assert Num.parse("1.5junk", &remainder) == 1.5
assert remainder == "junk"

Num.percent

Num.percent : func(n: Num, precision: Num = 0.01 -> Text)

Convert a number into a percentage text with a percent sign.

Argument Type Description Default
n Num The number to be converted to a percent. -
precision Num Round the percentage to this precision level. 0.01

Return: A text representation of the number as a percentage with a percent sign.

Example:

assert (0.5).percent() == "50%"
assert (1./3.).percent(2) == "34%"
assert (1./3.).percent(precision=0.0001) == "33.3333%"
assert (1./3.).percent(precision=10.) == "30%"

Num.rint

Num.rint : func(x: Num -> Num)

Rounds a number to the nearest integer, with ties rounded to the nearest even integer.

Argument Type Description Default
x Num The number to be rounded. -

Return: The nearest integer value of x.

Example:

assert (3.5).rint() == 4
assert (2.5).rint() == 2

Num.round

Num.round : func(x: Num -> Num)

Rounds a number to the nearest whole number integer.

Argument Type Description Default
x Num The number to be rounded. -

Return: The nearest integer value of x.

Example:

assert (2.3).round() == 2
assert (2.7).round() == 3

Num.significand

Num.significand : func(x: Num -> Num)

Extracts the significand (or mantissa) of a number.

Argument Type Description Default
x Num The number from which to extract the significand. -

Return: The significand of x.

Example:

assert (1234.567).significand() == 1.2056318359375

Num.sin

Num.sin : func(x: Num -> Num)

Computes the sine of a number (angle in radians).

Argument Type Description Default
x Num The angle in radians. -

Return: The sine of x.

Example:

assert (0.0).sin() == 0

Num.sinh

Num.sinh : func(x: Num -> Num)

Computes the hyperbolic sine of a number.

Argument Type Description Default
x Num The number for which the hyperbolic sine is to be calculated. -

Return: The hyperbolic sine of x.

Example:

assert (0.0).sinh() == 0

Num.sqrt

Num.sqrt : func(x: Num -> Num)

Computes the square root of a number.

Argument Type Description Default
x Num The number for which the square root is to be calculated. -

Return: The square root of x.

Example:

assert (16.0).sqrt() == 4

Num.tan

Num.tan : func(x: Num -> Num)

Computes the tangent of a number (angle in radians).

Argument Type Description Default
x Num The angle in radians. -

Return: The tangent of x.

Example:

assert (0.0).tan() == 0

Num.tanh

Num.tanh : func(x: Num -> Num)

Computes the hyperbolic tangent of a number.

Argument Type Description Default
x Num The number for which the hyperbolic tangent is to be calculated. -

Return: The hyperbolic tangent of x.

Example:

assert (0.0).tanh() == 0

Num.tgamma

Num.tgamma : func(x: Num -> Num)

Computes the gamma function of a number.

Argument Type Description Default
x Num The number for which the gamma function is to be calculated. -

Return: The gamma function of x.

Example:

assert (1.0).tgamma() == 1

Num.trunc

Num.trunc : func(x: Num -> Num)

Truncates a number to the nearest integer towards zero.

Argument Type Description Default
x Num The number to be truncated. -

Return: The integer part of x towards zero.

Example:

assert (3.7).trunc() == 3
assert (-3.7).trunc() == -3

Num.with_precision

Num.with_precision : func(n: Num, precision: Num -> Num)

Round a number to the given precision level (specified as 10, .1, .001 etc).

Argument Type Description Default
n Num The number to be rounded to a given precision. -
precision Num The precision to which the number should be rounded. -

Return: The number, rounded to the given precision level.

Example:

assert (0.1234567).with_precision(0.01) == 0.12
assert (123456.).with_precision(100) == 123500
assert (1234567.).with_precision(5) == 1234565

Num.y0

Num.y0 : func(x: Num -> Num)

Computes the Bessel function of the second kind of order 0.

Argument Type Description Default
x Num The number for which the Bessel function is to be calculated. -

Return: The Bessel function of the second kind of order 0 of x.

Example:

assert (1.0).y0().near(0.08825696421567698)

Num.y1

Num.y1 : func(x: Num -> Num)

Computes the Bessel function of the second kind of order 1.

Argument Type Description Default
x Num The number for which the Bessel function is to be calculated. -

Return: The Bessel function of the second kind of order 1 of x.

Example:

assert (1.0).y1().near(-0.7812128213002887)

1 % API
3 # Builtins
5 # Num
6 ## Num.1_PI
8 ```tomo
9 Num.1_PI : Num
10 ```
12 The constant $\frac{1}{\pi}$.
14 ## Num.2_PI
16 ```tomo
17 Num.2_PI : Num
18 ```
20 The constant $2 \times \pi$.
22 ## Num.2_SQRTPI
24 ```tomo
25 Num.2_SQRTPI : Num
26 ```
28 The constant $2 \times \sqrt{\pi}$.
30 ## Num.E
32 ```tomo
33 Num.E : Num
34 ```
36 The base of the natural logarithm ($e$).
38 ## Num.INF
40 ```tomo
41 Num.INF : Num
42 ```
44 Positive infinity.
46 ## Num.LN10
48 ```tomo
49 Num.LN10 : Num
50 ```
52 The natural logarithm of 10.
54 ## Num.LN2
56 ```tomo
57 Num.LN2 : Num
58 ```
60 The natural logarithm of 2.
62 ## Num.LOG2E
64 ```tomo
65 Num.LOG2E : Num
66 ```
68 The base 2 logarithm of $e$
70 ## Num.PI
72 ```tomo
73 Num.PI : Num
74 ```
76 Pi ($\pi$).
78 ## Num.PI_2
80 ```tomo
81 Num.PI_2 : Num
82 ```
84 $\frac{\pi}{2}$
86 ## Num.PI_4
88 ```tomo
89 Num.PI_4 : Num
90 ```
92 $\frac{\pi}{4}$
94 ## Num.SQRT1_2
96 ```tomo
97 Num.SQRT1_2 : Num
98 ```
100 $\sqrt{\frac{1}{2}}$
102 ## Num.SQRT2
104 ```tomo
105 Num.SQRT2 : Num
106 ```
108 $\sqrt{2}$
110 ## Num.TAU
112 ```tomo
113 Num.TAU : Num
114 ```
116 Tau ($2 \times \pi$)
118 ## Num.abs
120 ```tomo
121 Num.abs : func(n: Num -> Num)
122 ```
124 Calculates the absolute value of a number.
126 Argument | Type | Description | Default
127 ---------|------|-------------|---------
128 n | `Num` | The number whose absolute value is to be computed. | -
130 **Return:** The absolute value of `n`.
133 **Example:**
134 ```tomo
135 assert (-3.5).abs() == 3.5
137 ```
138 ## Num.acos
140 ```tomo
141 Num.acos : func(x: Num -> Num)
142 ```
144 Computes the arc cosine of a number.
146 Argument | Type | Description | Default
147 ---------|------|-------------|---------
148 x | `Num` | The number for which the arc cosine is to be calculated. | -
150 **Return:** The arc cosine of `x` in radians.
153 **Example:**
154 ```tomo
155 assert (0.0).acos().near(1.5707963267948966)
157 ```
158 ## Num.acosh
160 ```tomo
161 Num.acosh : func(x: Num -> Num)
162 ```
164 Computes the inverse hyperbolic cosine of a number.
166 Argument | Type | Description | Default
167 ---------|------|-------------|---------
168 x | `Num` | The number for which the inverse hyperbolic cosine is to be calculated. | -
170 **Return:** The inverse hyperbolic cosine of `x`.
173 **Example:**
174 ```tomo
175 assert (1.0).acosh() == 0
177 ```
178 ## Num.asin
180 ```tomo
181 Num.asin : func(x: Num -> Num)
182 ```
184 Computes the arc sine of a number.
186 Argument | Type | Description | Default
187 ---------|------|-------------|---------
188 x | `Num` | The number for which the arc sine is to be calculated. | -
190 **Return:** The arc sine of `x` in radians.
193 **Example:**
194 ```tomo
195 assert (0.5).asin().near(0.5235987755982989)
197 ```
198 ## Num.asinh
200 ```tomo
201 Num.asinh : func(x: Num -> Num)
202 ```
204 Computes the inverse hyperbolic sine of a number.
206 Argument | Type | Description | Default
207 ---------|------|-------------|---------
208 x | `Num` | The number for which the inverse hyperbolic sine is to be calculated. | -
210 **Return:** The inverse hyperbolic sine of `x`.
213 **Example:**
214 ```tomo
215 assert (0.0).asinh() == 0
217 ```
218 ## Num.atan
220 ```tomo
221 Num.atan : func(x: Num -> Num)
222 ```
224 Computes the arc tangent of a number.
226 Argument | Type | Description | Default
227 ---------|------|-------------|---------
228 x | `Num` | The number for which the arc tangent is to be calculated. | -
230 **Return:** The arc tangent of `x` in radians.
233 **Example:**
234 ```tomo
235 assert (1.0).atan().near(0.7853981633974483)
237 ```
238 ## Num.atan2
240 ```tomo
241 Num.atan2 : func(x: Num, y: Num -> Num)
242 ```
244 Computes the arc tangent of the quotient of two numbers.
246 Argument | Type | Description | Default
247 ---------|------|-------------|---------
248 x | `Num` | The numerator. | -
249 y | `Num` | The denominator. | -
251 **Return:** The arc tangent of `x/y` in radians.
254 **Example:**
255 ```tomo
256 assert Num.atan2(1, 1).near(0.7853981633974483)
258 ```
259 ## Num.atanh
261 ```tomo
262 Num.atanh : func(x: Num -> Num)
263 ```
265 Computes the inverse hyperbolic tangent of a number.
267 Argument | Type | Description | Default
268 ---------|------|-------------|---------
269 x | `Num` | The number for which the inverse hyperbolic tangent is to be calculated. | -
271 **Return:** The inverse hyperbolic tangent of `x`.
274 **Example:**
275 ```tomo
276 assert (0.5).atanh().near(0.5493061443340549)
278 ```
279 ## Num.cbrt
281 ```tomo
282 Num.cbrt : func(x: Num -> Num)
283 ```
285 Computes the cube root of a number.
287 Argument | Type | Description | Default
288 ---------|------|-------------|---------
289 x | `Num` | The number for which the cube root is to be calculated. | -
291 **Return:** The cube root of `x`.
294 **Example:**
295 ```tomo
296 assert (27.0).cbrt() == 3
298 ```
299 ## Num.ceil
301 ```tomo
302 Num.ceil : func(x: Num -> Num)
303 ```
305 Rounds a number up to the nearest integer.
307 Argument | Type | Description | Default
308 ---------|------|-------------|---------
309 x | `Num` | The number to be rounded up. | -
311 **Return:** The smallest integer greater than or equal to `x`.
314 **Example:**
315 ```tomo
316 assert (3.2).ceil() == 4
318 ```
319 ## Num.clamped
321 ```tomo
322 Num.clamped : func(x: Num, low: Num, high: Num -> Num)
323 ```
325 Returns the given number clamped between two values so that it is within that range.
327 Argument | Type | Description | Default
328 ---------|------|-------------|---------
329 x | `Num` | The number to clamp. | -
330 low | `Num` | The lowest value the result can take. | -
331 high | `Num` | The highest value the result can take. | -
333 **Return:** The first argument clamped between the other two arguments.
336 **Example:**
337 ```tomo
338 assert (2.5).clamped(5.5, 10.5) == 5.5
340 ```
341 ## Num.copysign
343 ```tomo
344 Num.copysign : func(x: Num, y: Num -> Num)
345 ```
347 Copies the sign of one number to another.
349 Argument | Type | Description | Default
350 ---------|------|-------------|---------
351 x | `Num` | The number whose magnitude will be copied. | -
352 y | `Num` | The number whose sign will be copied. | -
354 **Return:** A number with the magnitude of `x` and the sign of `y`.
357 **Example:**
358 ```tomo
359 assert (3.0).copysign(-1) == -3
361 ```
362 ## Num.cos
364 ```tomo
365 Num.cos : func(x: Num -> Num)
366 ```
368 Computes the cosine of a number (angle in radians).
370 Argument | Type | Description | Default
371 ---------|------|-------------|---------
372 x | `Num` | The angle in radians. | -
374 **Return:** The cosine of `x`.
377 **Example:**
378 ```tomo
379 assert (0.0).cos() == 1
381 ```
382 ## Num.cosh
384 ```tomo
385 Num.cosh : func(x: Num -> Num)
386 ```
388 Computes the hyperbolic cosine of a number.
390 Argument | Type | Description | Default
391 ---------|------|-------------|---------
392 x | `Num` | The number for which the hyperbolic cosine is to be calculated. | -
394 **Return:** The hyperbolic cosine of `x`.
397 **Example:**
398 ```tomo
399 assert (0.0).cosh() == 1
401 ```
402 ## Num.erf
404 ```tomo
405 Num.erf : func(x: Num -> Num)
406 ```
408 Computes the error function of a number.
410 Argument | Type | Description | Default
411 ---------|------|-------------|---------
412 x | `Num` | The number for which the error function is to be calculated. | -
414 **Return:** The error function of `x`.
417 **Example:**
418 ```tomo
419 assert (0.0).erf() == 0
421 ```
422 ## Num.erfc
424 ```tomo
425 Num.erfc : func(x: Num -> Num)
426 ```
428 Computes the complementary error function of a number.
430 Argument | Type | Description | Default
431 ---------|------|-------------|---------
432 x | `Num` | The number for which the complementary error function is to be calculated. | -
434 **Return:** The complementary error function of `x`.
437 **Example:**
438 ```tomo
439 assert (0.0).erfc() == 1
441 ```
442 ## Num.exp
444 ```tomo
445 Num.exp : func(x: Num -> Num)
446 ```
448 Computes the exponential function $e^x$ for a number.
450 Argument | Type | Description | Default
451 ---------|------|-------------|---------
452 x | `Num` | The exponent. | -
454 **Return:** The value of $e^x$.
457 **Example:**
458 ```tomo
459 assert (1.0).exp().near(2.718281828459045)
461 ```
462 ## Num.exp2
464 ```tomo
465 Num.exp2 : func(x: Num -> Num)
466 ```
468 Computes $2^x$ for a number.
470 Argument | Type | Description | Default
471 ---------|------|-------------|---------
472 x | `Num` | The exponent. | -
474 **Return:** The value of $2^x$.
477 **Example:**
478 ```tomo
479 assert (3.0).exp2() == 8
481 ```
482 ## Num.expm1
484 ```tomo
485 Num.expm1 : func(x: Num -> Num)
486 ```
488 Computes $e^x - 1$ for a number.
490 Argument | Type | Description | Default
491 ---------|------|-------------|---------
492 x | `Num` | The exponent. | -
494 **Return:** The value of $e^x - 1$.
497 **Example:**
498 ```tomo
499 assert (1.0).expm1().near(1.7182818284590453)
501 ```
502 ## Num.fdim
504 ```tomo
505 Num.fdim : func(x: Num, y: Num -> Num)
506 ```
508 Computes the positive difference between two numbers.
510 Argument | Type | Description | Default
511 ---------|------|-------------|---------
512 x | `Num` | The first number. | -
513 y | `Num` | The second number. | -
515 **Return:** The positive difference $\max(0, x - y)$.
518 **Example:**
519 ```tomo
520 assert (5.0).fdim(3) == 2
522 ```
523 ## Num.floor
525 ```tomo
526 Num.floor : func(x: Num -> Num)
527 ```
529 Rounds a number down to the nearest integer.
531 Argument | Type | Description | Default
532 ---------|------|-------------|---------
533 x | `Num` | The number to be rounded down. | -
535 **Return:** The largest integer less than or equal to `x`.
538 **Example:**
539 ```tomo
540 assert (3.7).floor() == 3
542 ```
543 ## Num.hypot
545 ```tomo
546 Num.hypot : func(x: Num, y: Num -> Num)
547 ```
549 Computes the Euclidean norm, $\sqrt{x^2 + y^2}$, of two numbers.
551 Argument | Type | Description | Default
552 ---------|------|-------------|---------
553 x | `Num` | The first number. | -
554 y | `Num` | The second number. | -
556 **Return:** The Euclidean norm of `x` and `y`.
559 **Example:**
560 ```tomo
561 assert Num.hypot(3, 4) == 5
563 ```
564 ## Num.is_between
566 ```tomo
567 Num.is_between : func(x: Num, low: Num, high: Num -> Bool)
568 ```
570 Determines if a number is between two numbers (inclusive).
572 Argument | Type | Description | Default
573 ---------|------|-------------|---------
574 x | `Num` | The integer to be checked. | -
575 low | `Num` | One end of the range to check (inclusive). | -
576 high | `Num` | The other end of the range to check (inclusive). | -
578 **Return:** `yes` if `a <= x and x <= b` or `b <= x and x <= a`, otherwise `no`
581 **Example:**
582 ```tomo
583 assert (7.5).is_between(1, 10) == yes
584 assert (7.5).is_between(10, 1) == yes
585 assert (7.5).is_between(100, 200) == no
586 assert (7.5).is_between(1, 7.5) == yes
588 ```
589 ## Num.isfinite
591 ```tomo
592 Num.isfinite : func(n: Num -> Bool)
593 ```
595 Checks if a number is finite.
597 Argument | Type | Description | Default
598 ---------|------|-------------|---------
599 n | `Num` | The number to be checked. | -
601 **Return:** `yes` if `n` is finite, `no` otherwise.
604 **Example:**
605 ```tomo
606 assert (1.0).isfinite() == yes
607 assert Num.INF.isfinite() == no
609 ```
610 ## Num.isinf
612 ```tomo
613 Num.isinf : func(n: Num -> Bool)
614 ```
616 Checks if a number is infinite.
618 Argument | Type | Description | Default
619 ---------|------|-------------|---------
620 n | `Num` | The number to be checked. | -
622 **Return:** `yes` if `n` is infinite, `no` otherwise.
625 **Example:**
626 ```tomo
627 assert Num.INF.isinf() == yes
628 assert (1.0).isinf() == no
630 ```
631 ## Num.j0
633 ```tomo
634 Num.j0 : func(x: Num -> Num)
635 ```
637 Computes the Bessel function of the first kind of order 0.
639 Argument | Type | Description | Default
640 ---------|------|-------------|---------
641 x | `Num` | The number for which the Bessel function is to be calculated. | -
643 **Return:** The Bessel function of the first kind of order 0 of `x`.
646 **Example:**
647 ```tomo
648 assert (0.0).j0() == 1
650 ```
651 ## Num.j1
653 ```tomo
654 Num.j1 : func(x: Num -> Num)
655 ```
657 Computes the Bessel function of the first kind of order 1.
659 Argument | Type | Description | Default
660 ---------|------|-------------|---------
661 x | `Num` | The number for which the Bessel function is to be calculated. | -
663 **Return:** The Bessel function of the first kind of order 1 of `x`.
666 **Example:**
667 ```tomo
668 assert (0.0).j1() == 0
670 ```
671 ## Num.log
673 ```tomo
674 Num.log : func(x: Num -> Num)
675 ```
677 Computes the natural logarithm (base $e$) of a number.
679 Argument | Type | Description | Default
680 ---------|------|-------------|---------
681 x | `Num` | The number for which the natural logarithm is to be calculated. | -
683 **Return:** The natural logarithm of `x`.
686 **Example:**
687 ```tomo
688 assert Num.E.log() == 1
690 ```
691 ## Num.log10
693 ```tomo
694 Num.log10 : func(x: Num -> Num)
695 ```
697 Computes the base-10 logarithm of a number.
699 Argument | Type | Description | Default
700 ---------|------|-------------|---------
701 x | `Num` | The number for which the base-10 logarithm is to be calculated. | -
703 **Return:** The base-10 logarithm of `x`.
706 **Example:**
707 ```tomo
708 assert (100.0).log10() == 2
710 ```
711 ## Num.log1p
713 ```tomo
714 Num.log1p : func(x: Num -> Num)
715 ```
717 Computes $\log(1 + x)$ for a number.
719 Argument | Type | Description | Default
720 ---------|------|-------------|---------
721 x | `Num` | The number for which $\log(1 + x)$ is to be calculated. | -
723 **Return:** The value of $\log(1 + x)$.
726 **Example:**
727 ```tomo
728 assert (1.0).log1p().near(0.6931471805599453)
730 ```
731 ## Num.log2
733 ```tomo
734 Num.log2 : func(x: Num -> Num)
735 ```
737 Computes the base-2 logarithm of a number.
739 Argument | Type | Description | Default
740 ---------|------|-------------|---------
741 x | `Num` | The number for which the base-2 logarithm is to be calculated. | -
743 **Return:** The base-2 logarithm of `x`.
746 **Example:**
747 ```tomo
748 assert (8.0).log2() == 3
750 ```
751 ## Num.logb
753 ```tomo
754 Num.logb : func(x: Num -> Num)
755 ```
757 Computes the binary exponent (base-2 logarithm) of a number.
759 Argument | Type | Description | Default
760 ---------|------|-------------|---------
761 x | `Num` | The number for which the binary exponent is to be calculated. | -
763 **Return:** The binary exponent of `x`.
766 **Example:**
767 ```tomo
768 assert (8.0).logb() == 3
770 ```
771 ## Num.mix
773 ```tomo
774 Num.mix : func(amount: Num, x: Num, y: Num -> Num)
775 ```
777 Interpolates between two numbers based on a given amount.
779 Argument | Type | Description | Default
780 ---------|------|-------------|---------
781 amount | `Num` | The interpolation factor (between `0` and `1`). | -
782 x | `Num` | The starting number. | -
783 y | `Num` | The ending number. | -
785 **Return:** The interpolated number between `x` and `y` based on `amount`.
788 **Example:**
789 ```tomo
790 assert (0.5).mix(10, 20) == 15
791 assert (0.25).mix(10, 20) == 12.5
793 ```
794 ## Num.near
796 ```tomo
797 Num.near : func(x: Num, y: Num, ratio: Num = 1e-9, min_epsilon: Num = 1e-9 -> Bool)
798 ```
800 Checks if two numbers are approximately equal within specified tolerances. If two numbers are within an absolute difference or the ratio between the two is small enough, they are considered near each other.
802 Argument | Type | Description | Default
803 ---------|------|-------------|---------
804 x | `Num` | The first number. | -
805 y | `Num` | The second number. | -
806 ratio | `Num` | The relative tolerance. Default is `1e-9`. | `1e-9`
807 min_epsilon | `Num` | The absolute tolerance. Default is `1e-9`. | `1e-9`
809 **Return:** `yes` if `x` and `y` are approximately equal within the specified tolerances, `no` otherwise.
812 **Example:**
813 ```tomo
814 assert (1.0).near(1.000000001) == yes
815 assert (100.0).near(110, ratio=0.1) == yes
816 assert (5.0).near(5.1, min_epsilon=0.1) == yes
818 ```
819 ## Num.nextafter
821 ```tomo
822 Num.nextafter : func(x: Num, y: Num -> Num)
823 ```
825 Computes the next representable value after a given number towards a specified direction.
827 Argument | Type | Description | Default
828 ---------|------|-------------|---------
829 x | `Num` | The starting number. | -
830 y | `Num` | The direction towards which to find the next representable value. | -
832 **Return:** The next representable value after `x` in the direction of `y`.
835 **Example:**
836 ```tomo
837 assert (1.0).nextafter(1.1) == 1.0000000000000002
839 ```
840 ## Num.parse
842 ```tomo
843 Num.parse : func(text: Text, remainder: &Text? = none -> Num?)
844 ```
846 Converts a text representation of a number into a floating-point number.
848 Argument | Type | Description | Default
849 ---------|------|-------------|---------
850 text | `Text` | The text containing the number. | -
851 remainder | `&Text?` | If non-none, this argument will be set to the remainder of the text after the matching part. If none, parsing will only succeed if the entire text matches. | `none`
853 **Return:** The number represented by the text or `none` if the entire text can't be parsed as a number.
856 **Example:**
857 ```tomo
858 assert Num.parse("3.14") == 3.14
859 assert Num.parse("1e3") == 1000
860 assert Num.parse("1.5junk") == none
861 remainder : Text
862 assert Num.parse("1.5junk", &remainder) == 1.5
863 assert remainder == "junk"
865 ```
866 ## Num.percent
868 ```tomo
869 Num.percent : func(n: Num, precision: Num = 0.01 -> Text)
870 ```
872 Convert a number into a percentage text with a percent sign.
874 Argument | Type | Description | Default
875 ---------|------|-------------|---------
876 n | `Num` | The number to be converted to a percent. | -
877 precision | `Num` | Round the percentage to this precision level. | `0.01`
879 **Return:** A text representation of the number as a percentage with a percent sign.
882 **Example:**
883 ```tomo
884 assert (0.5).percent() == "50%"
885 assert (1./3.).percent(2) == "34%"
886 assert (1./3.).percent(precision=0.0001) == "33.3333%"
887 assert (1./3.).percent(precision=10.) == "30%"
889 ```
890 ## Num.rint
892 ```tomo
893 Num.rint : func(x: Num -> Num)
894 ```
896 Rounds a number to the nearest integer, with ties rounded to the nearest even integer.
898 Argument | Type | Description | Default
899 ---------|------|-------------|---------
900 x | `Num` | The number to be rounded. | -
902 **Return:** The nearest integer value of `x`.
905 **Example:**
906 ```tomo
907 assert (3.5).rint() == 4
908 assert (2.5).rint() == 2
910 ```
911 ## Num.round
913 ```tomo
914 Num.round : func(x: Num -> Num)
915 ```
917 Rounds a number to the nearest whole number integer.
919 Argument | Type | Description | Default
920 ---------|------|-------------|---------
921 x | `Num` | The number to be rounded. | -
923 **Return:** The nearest integer value of `x`.
926 **Example:**
927 ```tomo
928 assert (2.3).round() == 2
929 assert (2.7).round() == 3
931 ```
932 ## Num.significand
934 ```tomo
935 Num.significand : func(x: Num -> Num)
936 ```
938 Extracts the significand (or mantissa) of a number.
940 Argument | Type | Description | Default
941 ---------|------|-------------|---------
942 x | `Num` | The number from which to extract the significand. | -
944 **Return:** The significand of `x`.
947 **Example:**
948 ```tomo
949 assert (1234.567).significand() == 1.2056318359375
951 ```
952 ## Num.sin
954 ```tomo
955 Num.sin : func(x: Num -> Num)
956 ```
958 Computes the sine of a number (angle in radians).
960 Argument | Type | Description | Default
961 ---------|------|-------------|---------
962 x | `Num` | The angle in radians. | -
964 **Return:** The sine of `x`.
967 **Example:**
968 ```tomo
969 assert (0.0).sin() == 0
971 ```
972 ## Num.sinh
974 ```tomo
975 Num.sinh : func(x: Num -> Num)
976 ```
978 Computes the hyperbolic sine of a number.
980 Argument | Type | Description | Default
981 ---------|------|-------------|---------
982 x | `Num` | The number for which the hyperbolic sine is to be calculated. | -
984 **Return:** The hyperbolic sine of `x`.
987 **Example:**
988 ```tomo
989 assert (0.0).sinh() == 0
991 ```
992 ## Num.sqrt
994 ```tomo
995 Num.sqrt : func(x: Num -> Num)
996 ```
998 Computes the square root of a number.
1000 Argument | Type | Description | Default
1001 ---------|------|-------------|---------
1002 x | `Num` | The number for which the square root is to be calculated. | -
1004 **Return:** The square root of `x`.
1007 **Example:**
1008 ```tomo
1009 assert (16.0).sqrt() == 4
1011 ```
1012 ## Num.tan
1014 ```tomo
1015 Num.tan : func(x: Num -> Num)
1016 ```
1018 Computes the tangent of a number (angle in radians).
1020 Argument | Type | Description | Default
1021 ---------|------|-------------|---------
1022 x | `Num` | The angle in radians. | -
1024 **Return:** The tangent of `x`.
1027 **Example:**
1028 ```tomo
1029 assert (0.0).tan() == 0
1031 ```
1032 ## Num.tanh
1034 ```tomo
1035 Num.tanh : func(x: Num -> Num)
1036 ```
1038 Computes the hyperbolic tangent of a number.
1040 Argument | Type | Description | Default
1041 ---------|------|-------------|---------
1042 x | `Num` | The number for which the hyperbolic tangent is to be calculated. | -
1044 **Return:** The hyperbolic tangent of `x`.
1047 **Example:**
1048 ```tomo
1049 assert (0.0).tanh() == 0
1051 ```
1052 ## Num.tgamma
1054 ```tomo
1055 Num.tgamma : func(x: Num -> Num)
1056 ```
1058 Computes the gamma function of a number.
1060 Argument | Type | Description | Default
1061 ---------|------|-------------|---------
1062 x | `Num` | The number for which the gamma function is to be calculated. | -
1064 **Return:** The gamma function of `x`.
1067 **Example:**
1068 ```tomo
1069 assert (1.0).tgamma() == 1
1071 ```
1072 ## Num.trunc
1074 ```tomo
1075 Num.trunc : func(x: Num -> Num)
1076 ```
1078 Truncates a number to the nearest integer towards zero.
1080 Argument | Type | Description | Default
1081 ---------|------|-------------|---------
1082 x | `Num` | The number to be truncated. | -
1084 **Return:** The integer part of `x` towards zero.
1087 **Example:**
1088 ```tomo
1089 assert (3.7).trunc() == 3
1090 assert (-3.7).trunc() == -3
1092 ```
1093 ## Num.with_precision
1095 ```tomo
1096 Num.with_precision : func(n: Num, precision: Num -> Num)
1097 ```
1099 Round a number to the given precision level (specified as `10`, `.1`, `.001` etc).
1101 Argument | Type | Description | Default
1102 ---------|------|-------------|---------
1103 n | `Num` | The number to be rounded to a given precision. | -
1104 precision | `Num` | The precision to which the number should be rounded. | -
1106 **Return:** The number, rounded to the given precision level.
1109 **Example:**
1110 ```tomo
1111 assert (0.1234567).with_precision(0.01) == 0.12
1112 assert (123456.).with_precision(100) == 123500
1113 assert (1234567.).with_precision(5) == 1234565
1115 ```
1116 ## Num.y0
1118 ```tomo
1119 Num.y0 : func(x: Num -> Num)
1120 ```
1122 Computes the Bessel function of the second kind of order 0.
1124 Argument | Type | Description | Default
1125 ---------|------|-------------|---------
1126 x | `Num` | The number for which the Bessel function is to be calculated. | -
1128 **Return:** The Bessel function of the second kind of order 0 of `x`.
1131 **Example:**
1132 ```tomo
1133 assert (1.0).y0().near(0.08825696421567698)
1135 ```
1136 ## Num.y1
1138 ```tomo
1139 Num.y1 : func(x: Num -> Num)
1140 ```
1142 Computes the Bessel function of the second kind of order 1.
1144 Argument | Type | Description | Default
1145 ---------|------|-------------|---------
1146 x | `Num` | The number for which the Bessel function is to be calculated. | -
1148 **Return:** The Bessel function of the second kind of order 1 of `x`.
1151 **Example:**
1152 ```tomo
1153 assert (1.0).y1().near(-0.7812128213002887)
1155 ```