Math++

From Esolang
Jump to: navigation, search

Math++ is an esoteric programming language by SuperJedi224, defined by this java implementation.

All Math++ variables are 64-bit IEEE 754 floats. The language has 26 variables (each represented by a lowercase letter) and a Map data structure.

Syntax

The program consists of one or more lines each of which consists of an expression followed, optionally, by a greater than symbol (">") then a target designator.

The following target designators are recognized:

out

The default target designator. Prints the result to stdout.

<any variable name>

Stores the result to that variable

{<any value>}

Associates the result with the specified key in the map

$

Round the result towards zero and go to that line. Going to line zero is treated as an exit statement, going to any other out-of-bounds line number yields an exception.

Language features

Binary Operators

In ascending order by precedence Math++ has the following binary operators (parentheses may be used to modify the order of operations as needed):

Boolean OR

a|b

Short-circuiting logical OR operator. Returns a if a is nonzero, b otherwise.

Boolean AND

a&b

Short-circuiting logical AND operator. Returns 0 if a is zero, b otherwise.

Addition

a+b

Returns the sum of a and b.

Subtraction

a-b

Returns the difference of a and b.

Multiplication

a*b

Returns the product of a and b.

Division

a/b

Returns the quotient of a and b.

Modulus

a%b

Equivalent to (a-b*_(a/b))

Unary Operators

Math++ also has the following Unary operators (which always have higher precedence than any binary operators):

Unary Minus

-a

Returns -1*a.

Common Logarithm

log a

The space is optional but should typically be included for readability. Returns log10(a). Yields -Infinity when a=0, and NaN when a<0.

Natural Logarithm

ln a

The space is optional but should typically be included for readability. Returns loge(a). Yields -Infinity when a=0, and NaN when a<0.

Sine

sin a

The space is optional but should typically be included for readability. Returns sine(a). Uses radians.

Cosine

cos a

The space is optional but should typically be included for readability. Returns cosine(a). Uses radians.

Tangent

tan a

The space is optional but should typically be included for readability. Returns tangent(a). Uses radians.

Secant

sec a

The space is optional but should typically be included for readability. Returns secant(a)=1/cosine(a). Uses radians.

Cosecant

csc a

The space is optional but should typically be included for readability. Returns cosecant(a)=1/sine(a). Uses radians.

Cotangent

cot a

The space is optional but should typically be included for readability. Returns cotangant(a)=1/tangent(a). Uses radians.

Square Root

sqrt a

The space is optional but should typically be included for readability. Returns the square root of a (NaN if a is negative).

Cube Root

cbrt a

The space is optional but should typically be included for readability. Returns the cube root of a.

Floor

_a

Returns a rounded down (towards zero) to the nearest integer.

Boolean NOT

!a

Returns 1 if a is 0, and 0 otherwise.

IO

Output, as mentioned above, is handled by using the target designator "out" or by omitting the target designator on an expresssion.

A number can be taken from the input by using a question mark (?) in an expression.

Constants

The following special expressions return constant values:

$e

Returns Euler's number.

$pi

Returns pi.

$phi

Returns phi=(1+sqrt(5))/2.

Pseudorandom Values

A pseudorandom value between 0 and 1 (potentially including zero, but not including one) can be generated by using the special expression "$rand".

Using the Map

As was explained in the syntax section, associating a value with a key in the map can be done using {<key>} as the target designator for an expression producing the value.

Retrieving the value associated with a key can likewise be done by using {<key>} in an expression.

Sample Programs

Truth Machine

2+2*!?>$
1>out
2>$
0>out

Countdown from 100

100>a
a>out
a-1>a
2*!!a>$

Square Root of input

sqrt?

Sum of two inputs

?+?

Square of input

?>a
a*a>out