modus
Menu
On this page

The module math

A module of commands: each command is a function with typed parameters, not a module that parses text. It does not run as a process: it has no entry. The console loads it and calls its exports with the call form . The tool makes the description of each export from this source: the name, the parameters, the result, and the first sentence of the comment (tools/modustool/build/describe.odin).

What it is for. It is the first module that a reader loads to see the call form at work: integers in, an integer or a text out, an error record for a refusal. Each export is a pure function: it reads its parameters only and keeps no state, so a direct call and an isolated call give the same answer.

Example, in the console:

load("math")
gcd(84, 36) + power(2, 10)      1036
roman(2026)                     "MMXXVI"
square_root(-4)                 an error record: Too_Small

isolate("math") gives the same answers through a sandboxed process.

What it does not do: no export checks an overflow of 64 bits, except fibonacci, which stops at the last number that fits, and gcd, which refuses the one integer that has no absolute value. power(10, 19), for example, wraps.

See: Module contract: The call form

math module with no entry

Hash
2cb12148ffb846dce82e57ec3f9830d678497a9efd0f972b5f24fbb6083e2436
Size
19521 bytes
Exports
8
Imports
0
Imported by
0
License
MIT

A module of commands: each command is a function with typed parameters, not a module that parses text

FactValue
KindModules with no entry
Sourcesrc/modules/libraries/math
Boot imageyes: in the boot image, 19521 bytes in this build
Build settingsnone: the defaults of the source
Importsnone
Imported bynone

Neighbours

It imports no module, and no module imports it. The module graph shows it with the other modules that have no edge.

Exports

ExportWhat it does
gcd(first: Integer, second: Integer) -> Integergives the greatest common divisor
power(base: Integer, exponent: Integer) -> Integergives an integer to a power
fibonacci(index: Integer) -> Integergives the Fibonacci number of an index
next_prime(from: Integer) -> Integergives the first prime from an integer
number(text: Text) -> Integergives the integer that a text holds
square_root(value: Integer) -> Integergives the largest integer whose square is not above a value, or -1 with an error record: the error kind Too_Small, when the value is negative
roman(number: Integer) -> Textgives an integer in Roman numerals
reverse(text: Text) -> Textgives the bytes of a text, last first

gcd

gcd(first: , second: ) -> 

gcd gives the greatest common divisor.

It uses the absolute values, so the result is never negative; gcd(0, n) is the absolute value of n, and gcd(0, 0) is 0. It fails with -1 and an error record: Too_Small when an argument is the most negative integer (-9223372036854775808), whose absolute value does not fit in 64 bits; the record names the argument and the least value that the call takes.

Example: gcd(84, 36) gives 12.

Parameters
  • first:
  • second:
Result
When it fails
a negative result, and the error record
Source
src/modules/libraries/math/math.odin:42

power

power(base: , exponent: ) -> 

power gives an integer to a power.

It squares and multiplies, so a large exponent costs about 63 steps at most. An exponent of 0 or less gives 1. The result wraps when it passes 64 bits: no error says so.

Example: power(2, 10) gives 1024.

Parameters
  • base:
  • exponent:
Result
Source
src/modules/libraries/math/math.odin:63

fibonacci

fibonacci(index: ) -> 

fibonacci gives the Fibonacci number of an index.

fibonacci(0) is 0 and fibonacci(1) is 1. An index above 92 gives the number of 92, the last that fits in 64 bits; an index of 0 or less gives 0.

Parameters
  • index:
Result
Source
src/modules/libraries/math/math.odin:83

next_prime

next_prime(from: ) -> 

next_prime gives the first prime from an integer.

The integer itself counts: next_prime(7) is 7. Below 2 the result is 2.

Result: the prime, or a negative number with an error record:

  • Bad_Value (-1): the integer is above 2^40, because the search by trial division takes too long for a call.
Parameters
  • from:
Result
When it fails
a negative result, and the error record
Source
src/modules/libraries/math/math.odin:101

number

number(text: ) -> 

number gives the integer that a text holds. A Text parameter is the address of a slice record: {address, count}.

It reads an optional "-", then decimal digits, and stops at the first other byte: number("12ab") is 12, and a text with no digit gives 0. It never fails; a number of more than 19 digits wraps.

Parameters
  • text:
Result
Source
src/modules/libraries/math/math.odin:121

square_root

square_root(value: ) -> 

square_root gives the largest integer whose square is not above a value, or -1 with an error record: the error kind Too_Small, when the value is negative.

It is the export of this module that shows an error record: the record names the first argument, its value, and the limit 0.

Example: square_root(99) gives 9.

Parameters
  • value:
Result
When it fails
a negative result, and the error record
Source
src/modules/libraries/math/math.odin:144

roman

roman(number: ) -> 

roman gives an integer in Roman numerals.

The integer is taken from 0 to 3999: a larger one gives MMMCMXCIX, and 0 or less gives an empty text. The result is the count of bytes of the whole text; a buffer that is too short gets its first bytes.

Example: roman(2026) gives "MMXXVI".

Parameters
  • number:
Result
Source
src/modules/libraries/math/math.odin:173

reverse

reverse(text: ) -> 

reverse gives the bytes of a text, last first.

It reverses bytes, not characters: a character of several bytes of UTF-8 comes out broken. The result is the count of bytes of the text.

Parameters
  • text:
Result
Source
src/modules/libraries/math/math.odin:195

Read from the source of the module, as the build reads it to describe the exports (tools/modustool/build/describe.odin).

Integer wire form

A wire form of the module contract: one word of a call. A signed integer of 64 bits, in a register.

Error_Record record of a module

A record of the module math: 56 bytes, aligned on 8 bytes, 0 bytes of padding.

  • kindi6408 bytes
  • uniti6488 bytes
  • operationi64168 bytes
  • argumenti64248 bytes
  • valuei64328 bytes
  • limiti64408 bytes
  • foundi64488 bytes

Text wire form

A wire form of the module contract: one word of a call. The address of a slice record {address, count} of UTF-8 bytes.