Skip to content

Latest commit

 

History

History
454 lines (325 loc) · 8.1 KB

basic.md

File metadata and controls

454 lines (325 loc) · 8.1 KB

basic

Basic calculator

Index

Operation Description
abs Absolute value
add, a, + Addition
ceil Ceiling
cube, cb Cube of a number
cube.root, cbrt Cube root
div, d, / Division
div.mod Division with modulus
div.rem, dr Division with remainder
div/i Euclidean integer division
exp Natural exponential
floor Floor
log Natural logarithm
log10 Decimal logarithm
mod Modulus
mul, m, * Multiplication
neg Negation
pow, ** Exponentiation
rem, % Remainder
sign Sign
square, sq Square of a number
square.root, sqrt Square Root
sub, s, - Subtraction

Operations

abs

If x is less than zero, the negated value of x, otherwise p0.

For complex numbers, the distance of x from zero on the complex plane.

Stack effects:

( x:Int -- Int )
( x:Dec -- Dec )
( x:Complex -- Float )

Example:

Input Stack
-6 abs 6
c 2+2i abs 5 round 2.82843

add

Adds x to y.

Aliases: a, +

Stack effects:

( x:Int y:Int -- Int )
( x:Dec y:Dec -- Dec )
( x:Rat y:Rat -- Rat )
( x:Complex y:Complex -- Complex )

Example:

Input Stack
c 6 2 a 8
c 1.1 2.2 a 3.3
c 1/2 1/4 add 3/4
c 6+6i 2+2i a 8+8i

ceil

The nearest integer value greater than or equal to x.

Stack effects:

( x:Dec -- x:Dec )

Example:

Input Stack
6.6 ceil 7

cube

Cube of a nubmer, x³

Alias: cb

Macro definition:

def cube dup dup mul mul

Example:

Input Stack
3 cb 27

cube.root

The cube root of x. If x is less than zero, an 'invalid argument' error is raised.

Alias: cbrt

Stack effects:

( x:Dec -- Dec )

Example:

Input Stack
27 27
cbrt 3

div

Divides x by y. If y is zero, a division by zero error is raised.

Aliases: d, /

Stack effects:

( x:Dec y:Dec -- Dec )
( x:Rat y:Rat -- Rat )
( x:Complex y:Complex -- Complex )

Example:

Input Stack
c 12 4 d 3
c 11 2 d 5.5
c 1/4 4 div 1/16
c 12+12i 4+12i d 1.2-0.6i

div.mod

The quotient quo and remainder mod when dividing x by y using Euclidean division. If y is zero, a division by zero error is raised.

Stack effects:

( x:Int y:Int -- quo:Int mod:Int )

Example:

Input Stack
c -21 4 div.mod quo: -6 | mod: 3
c -21 4 div.rem quo: -5 | rem: -1

div.rem

The quotient quo and remainder rem when dividing x by y using truncated division. If y is zero, a division by zero error is raised.

Alias: dr

Stack effects:

( x:Int y:Int -- quo:Int rem:Int )

Example:

Input Stack
c -21 4 div.mod quo: -6 | mod: 3
c -21 4 div.rem quo: -5 | rem: -1

div/i

Divides x by y using Euclidean division. If y is zero, a division by zero error is raised.

Stack effects:

( x:Int y:Int -- Int )

Example:

Input Stack
-21 4 div/i -6

exp

Natural exponential of x.

Stack effects:

( x:Dec -- Dec )
( x:Complex -- Complex )

Example:

Input Stack
2 exp 5 round 7.38906

floor

The nearest integer value less than or equal to x.

Stack effects:

( x:Dec -- x:Dec )

Example:

Input Stack
6.12 floor 6

log

Natural logarithm of x.

Stack effects:

( x:Dec -- Dec )
( x:Complex -- Complex )

Example:

Input Stack
7.5 log 4 round 2.0149

log10

Natural logarithm of x.

Stack effects:

( x:Dec -- Dec )
( x:Complex -- Complex )

Example:

Input Stack
50 log10 5 round 1.69897

mod

The modulus when x is divided by y. If y is zero, a division by zero error is raised.

Stack effects:

( x:Int y:Int -- Int )

Example:

Input Stack
c -21 4 mod 3
c -21 4 rem -1

mul

Multiplies x by y.

Aliases: m, *

Stack effects:

( x:Int y:Int -- Int )
( x:Dec y:Dec -- Dec )
( x:Rat y:Rat -- Rat )
( x:Complex y:Complex -- Complex )

Example:

Input Stack
c 6 2 mul 12
c 6.6 2.2 mul 14.52
c 1/16 4 mul 1/4
c 2+3i 4+4i m -4+20i

neg

Changes the sign of x.

Stack effects:

( x:Int -- Int )
( x:Dec -- Dec )
( x:Rat -- Rat )
( x:Complex -- Complex )

Example:

Input Stack
-6 -6
neg 6
neg -6

pow

Raises x to the power of y. If x is negative and -1 < y < 1, an invalid operation error is raised. Certain values are errors when using a decimal number and are not when using an integer. For example:

| x | y | Int | Decimal | | 0 | 0 | 1 | invalid operation | | 0 | -1 | 1 | infinity |

Alias: **

Stack effects:

( x:Int y:Int -- Int )
( x:Dec y:Dec -- Dec )
( x:Complex y:Complex -- Complex )

Example:

Input Stack
c 6 2 pow 36
c 6.6 2.2 pow 2 round 63.53
c 6+6i 2+2i pow 2 round 13.57-6.32i

rem

The remainder when x is divided by y. If y is zero, a 'division by zero' error is raised.

Alias: %

Stack effects:

( x:Int y:Int -- Int )

Example:

Input Stack
c -21 4 mod 3
c -21 4 rem -1

sign

Places -1 on the stack if x is negative, 1 if x is positive, or 0 if x is zero.

Stack effects:

( x:Int -- Int/s )
( x:Dec -- Int/s )
( x:Rat y:Rat -- Rat )

Example:

Input Stack
c -6 sign -1
c 6 sign 1
c 0 sign 0

square

The square of a number, x²

Alias: sq

Macro definition:

def square dup mul

Example:

Input Stack
3 sq 9

square.root

The square root of x. If x is not Complex and is less than zero, an 'invalid argument' error is raised.

Alias: sqrt

Stack effects:

( x:Dec -- Dec )
( x:Complex -- Complex )

Example:

Input Stack
c 1.5625 sqrt 1.25
c -4+0i sqrt 0+2i

sub

Subtracts the value of y from x

Aliases: s, -

Stack effects:

( x:Int y:Int -- Int )
( x:Dec y:Dec -- Dec )
( x:Rat y:Rat -- Rat )
( x:Complex y:Complex -- x:Complex )

Example:

Input Stack
c 6 2 sub 4
c 6.6 2.2 sub 4.4
c 3/4 1/2 sub 1/4
c 6+6i 2+2i sub 4+4i