Skip to content
Joshua Shinavier edited this page May 17, 2011 · 11 revisions

The math: library contains primitives for basic arithmetic, trigonometry, random numbers, and so on.

Arithmetic and basic math

There are two forms of arithmetic in Ripple: integer arithmetic and real-valued arithmetic. The same primitives are used for each. For example, the expression 10 3 div. evaluates to 3 using integer arithmetic (as the operands 10 and 3 are both xsd:integer values), while the mathematically equivalent 10 3e0 div. evaluates to 3.3333333333333335E0 or similar, depending on the machine, because 3e0 has the xsd:double datatype.

...the rule is...

abs

This primitive finds the absolute value of a number. It expects a single, numeric argument at the top of the stack. It pops the argument from the stack and pushes its absolute value.

The inverse of abs pops a non-negative value from the stack and pushes each of the value itself and (for positive numbers), the negation of the value.

Examples:

1)  42 abs.

  [1]  42

2)  -100 abs.

  [1]  100

3)  3.1415 abs~.

  [1]  3.1415
  [2]  -3.1415

add

This primitive finds the sum of two numbers. It expects two, numeric arguments at the top of the stack: the operands to the add operation. It pops the operands from the stack and pushes their sum.

The inverse of add is sub (see below).

Example:

1)  2 3 add.

  [1]  5

2)  2 3 add~.

  [1]  -1

ceil

This primitive finds the ceiling of a number, i.e. the lowest integer greater than or equal to the number. It expects a single, numeric argument at the top of the stack: the operand. It pops the operand from the stack and pushes the operand's ceiling value.

This primitive has no special inverse.

Examples:

1)  3.1415 ceil.

  [1]  4

2)  42 ceil.

  [1]  42

3)  -123.45 ceil.

  [1]  -123

div

This primitive finds the quotient of one number divided by another. It expects two, numeric arguments at the top of the stack: the dividend and the divisor. It pops the operands from the stack and pushes their quotient. If the divisor is equal to zero, no result is produced.

The inverse of div is mul (see below).

Examples:

1)  42 6 div.

  [1]  7

2)  1 0 div.

3)  2 3 div~.

  [1]  6

floor

This primitive finds the floor of a number, i.e. the highest integer less than or equal to the number. It expects a single, numeric argument at the top of the stack. It pops the argument from the stack and pushes the argument's floor value.

This primitive has no special inverse.

Examples:

1)  137 floor.

  [1]  137

2)  -1.4142 floor.

  [1]  -2

gt

This primitive compares two values according to Ripple's natural order. It expects two arguments a and b at the top of the stack, which do not need to be numbers (however, if they are numbers, the total order of real numbers will apply). It pops the arguments from the stack and pushes the value true if a is strictly greater than b, otherwise false.

This primitive has no special inverse.

Examples:

1)  42 0 gt.

  [1]  true

2)  42e0 42 gt.

  [1]  false

3)  "42" 42 gt.

  [1]  true

lt

This primitive compares two values according to Ripple's natural order. It expects two arguments a and b at the top of the stack, which do not need to be numbers (however, if they are numbers, the total order of real numbers will apply). It pops the arguments from the stack and pushes the value true if a is strictly less than b, otherwise false.

This primitive has no special inverse.

Examples:

1)  0 42e0 lt.

  [1]  true

2)  0 <http://example.org/42> lt.

  [1]  true

mod

This primitive finds the remainder after dividing a given number by a given modulus. It expects two, numeric arguments at the top of the stack: the dividend and the modulus. It pops the dividend and modulus from the stack, divides the dividend by the modulus, and pushes the remainder to the stack.

This primitive has no special inverse.

Examples:

1)  42 2 mod.

  [1]  0

2)  23 2e0 mod.

  [1]  1.0E0

mul

This primitive finds the product of two numbers. It expects two, numeric arguments at the top of the stack: the multiplicands. The multiplicands are popped from the stack and multiplied, then their product is pushed onto the stack.

The inverse of mul is div (see above).

Examples:

1)  6 9 mul.

  [1]  54

2)  6 9e0 mul~.

  [1]  0.6666666666666666E0

neg

This primitive finds the additive inverse of a number. It expects one, numeric argument at the top of the stack. It pops the argument off of the stack, then pushes its inverse.

This primitive is its own inverse.

Examples:

1)  42 neg.

  [1]  -42

2)  0 neg.

  [1]  0

3)  42 neg~.   

  [1]  -42

sign

This primitive is the signum function, which finds whether a number is negative, zero, or positive. It expects a single, numeric argument at the top of the stack. It pops the argument off of the stack and pushes:

  • -1 if the number is less than zero. or
  • 0 if the number is equal to zero, or
  • 1 if the number is greater than zero

This primitive has no special inverse.

Examples:

  [1]  1

2)  -2 sign.

  [1]  -1

3)  42 dup. sub. sign.

  [1]  0

sub

This primitive finds the difference between two numbers. It expects two, numeric arguments at the top of the stack: the minuend and the subtrahend. It pops the arguments from the stack and pushes their difference.

The inverse of sub is add (see above).

Examples:

1)  0 1 sub.

  [1]  -1

2)  0 1 sub~.

  [1]  1

Roots and exponentials

cbrt

exp

log

log10

pow

sqrt

Trigonometry

acos

asin

atan

cos

cosh

sin

sinh

tan

tanh

Random numbers

random

Clone this wiki locally