Arithmetic / Logic

From MemCP
Revision as of 11:59, 28 August 2026 by Wikiservice (talk | contribs) (Refresh MemCP documentation: accuracy, operational guidance, performance profile and maintained API reference)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search


Arithmetic / Logic

Generated from MemCP commit c42e19eba on 27 August 2026. See Full SCM API documentation.

The Arithmetic / Logic module provides the mathematical and logical foundation of MemCP Scheme and compiled SQL expressions. It includes:

  • Type predicates for integers, numbers, NULL/nil, booleans, and related runtime values;
  • Arithmetic such as addition, subtraction, multiplication, division, modulo, and numeric conversion;
  • Comparison and SQL truth including ordering, equality, NULL-aware predicates, and three-valued-logic helpers;
  • Mathematical functions for rounding, extrema, powers, logarithms, and aggregate reduction;
  • Bitwise and optimizer primitives used by generated query plans.

Functions intended only for generated plans remain documented because they are part of the runtime interface, but application code should prefer the corresponding SQL or ordinary Scheme operation where one exists.

int?

tells if the value is a integer

Allowed number of parameters: 1–1

Parameters

  • value (any): value

Returns

  • value (bool)

number?

tells if the value is a number

Allowed number of parameters: 1–1

Parameters

  • value (any): value

Returns

  • value (bool)

symbol?

tells if the value is a symbol

Allowed number of parameters: 1–1

Parameters

  • value (any): value

Returns

  • value (bool)

+

adds two or more numbers

Allowed number of parameters: 0–10000

Parameters

  • value... (number): values to add (variadic)

Returns

  • value (number)

sql_sum_reduce

adds two SQL SUM values while treating NULL as the empty aggregate identity

Allowed number of parameters: 2–2

Parameters

  • left (number|nil): partial sum
  • right (number|nil): next value

Returns

  • value (number|nil)

-

subtracts two or more numbers from the first one

Allowed number of parameters: 0–10000

Parameters

  • value... (number): values (variadic)

Returns

  • value (number)

sql_add_numeric_literals

adds two SQL numeric literals using their exact decimal spellings

Allowed number of parameters: 2–2

Parameters

  • a (number): left literal
  • b (number): right literal

Returns

  • value (number)

sql_sub_numeric_literals

subtracts two SQL numeric literals using their exact decimal spellings

Allowed number of parameters: 2–2

Parameters

  • a (number): left literal
  • b (number): right literal

Returns

  • value (number)

*

multiplies two or more numbers

Allowed number of parameters: 0–10000

Parameters

  • value... (number): values (variadic)

Returns

  • value (number)

/

divides two or more numbers from the first one

Allowed number of parameters: 0–10000

Parameters

  • value... (number): values (variadic)

Returns

  • value (number)

mod

returns the remainder of integer division (modulo)

Allowed number of parameters: 2–2

Parameters

  • a (number): dividend
  • b (number): divisor

Returns

  • value (number)

<=

compares two numbers or strings; returns nil if either value is nil

Allowed number of parameters: 2–2

Parameters

  • a (any): first value
  • b (any): second value

Returns

  • value (bool)

<

compares two numbers or strings; returns nil if either value is nil

Allowed number of parameters: 2–2

Parameters

  • a (any): first value
  • b (any): second value

Returns

  • value (bool)

>

compares two numbers or strings; returns nil if either value is nil

Allowed number of parameters: 2–2

Parameters

  • a (any): first value
  • b (any): second value

Returns

  • value (bool)

>=

compares two numbers or strings; returns nil if either value is nil

Allowed number of parameters: 2–2

Parameters

  • a (any): first value
  • b (any): second value

Returns

  • value (bool)

equal?

compares two values of the same type, (equal? nil nil) is true

Allowed number of parameters: 2–2

Parameters

  • a (any): first value
  • b (any): second value

Returns

  • value (bool)

equal??

performs a SQL compliant sloppy equality check on primitive values (number, int, string, bool. nil), strings are compared case insensitive, (equal? nil nil) is nil

Allowed number of parameters: 2–2

Parameters

  • a (any): first value
  • b (any): second value

Returns

  • value (bool)

sql_not

negates a SQL predicate while preserving nil as UNKNOWN

Allowed number of parameters: 1–1

Parameters

  • value (any): SQL predicate value

Returns

  • value (bool|nil)

equal_collate

performs SQL equality with a specified collation (e.g. *_ci case-insensitive, *_bin case-sensitive); returns nil if either arg is nil

Allowed number of parameters: 3–3

Parameters

  • a (any): left side
  • b (any): right side
  • collation (string): collation name

Returns

  • value (bool)

notequal_collate

performs SQL inequality with a specified collation; returns nil if either arg is nil

Allowed number of parameters: 3–3

Parameters

  • a (any): left side
  • b (any): right side
  • collation (string): collation name

Returns

  • value (bool)

!

negates the boolean value

Allowed number of parameters: 1–1

Parameters

  • value (bool): value

Returns

  • value (bool)

not

negates the boolean value

Allowed number of parameters: 1–1

Parameters

  • value (bool): value

Returns

  • value (bool)

nil?

returns true if value is nil

Allowed number of parameters: 1–1

Parameters

  • value (any): value

Returns

  • value (bool)

min

returns the smallest value

Allowed number of parameters: 0–10000

Parameters

  • value... (number|string): value (variadic)

Returns

  • value (number|string)

max

returns the highest value

Allowed number of parameters: 0–10000

Parameters

  • value... (number|string): value (variadic)

Returns

  • value (number|string)

floor

rounds the number down

Allowed number of parameters: 1–1

Parameters

  • value (number): value

Returns

  • value (number)

ceil

rounds the number up

Allowed number of parameters: 1–1

Parameters

  • value (number): value

Returns

  • value (number)

round

rounds the number

Allowed number of parameters: 1–1

Parameters

  • value (number): value

Returns

  • value (number)

sql_decimal_output

normalizes a DECIMAL-derived result to its declared output scale

Allowed number of parameters: 2–2

Parameters

  • value (number|nil): DECIMAL-derived value
  • scale (int): declared decimal scale

Returns

  • value (number|nil)

sql_abs

SQL ABS(): returns absolute value, NULL-safe

Allowed number of parameters: 1–1

Parameters

  • value (number): value

Returns

  • value (number)

sqrt

returns the square root of a number

Allowed number of parameters: 1–1

Parameters

  • value (number): value

Returns

  • value (number)

sql_rand

SQL RAND(): returns a random float in [0,1)

Allowed number of parameters: 0–0

Parameters

This function has no parameters.

Returns

  • value (number)

parameterize_sql_select_literals

replaces safe literals in a top-level MySQL SELECT and returns normalized SQL, positional runtime bindings, and its stable shape hash

Allowed number of parameters: 1–1

Parameters

  • query (string)

Returns

  • value (list)