KeyJ's Calculator Help

Introduction

KeyJ's Calculator is a quite simple desk calculator application, comparable to other stand-alone programmable calculators (HP, Casio, TI, ...).

Simple Usage

It's as simple as it could be: Type an mathematical expression (e.g. 5*4), press ENTER, and you'll get the result (that is, 20).

Expression Syntax

Expressions are evaluated just as they would be written down to paper, with correct brackets and operator precedence (see below).

Numbers can be written as simple decimal numbers such as 37, 23.42 or 1.5482E-4.
Integer constants can also be written in binary, hexadecimal or octal notation when preceded by a % (percent), $ (dollar), or & (ampersand) symbol, respectively. Examples are %101011 (which is 43 decimal), #B0A7 (yields 45223), or &775 (509).
Additionally, there is one predefined constant, Pi.

Operators

Sorted by precedence, from highest to lowest class:
 1  ^  Power
 2  *  Multiplication
 2  /  Division
 3  +  Addition
 3  -  Subtraction
 4  =  Equal -- yields 1 if both sides are aqual, 0 otherwise
 4  <  Less than -- yields 1 only if the left side is less than the right side
 4  >  Greater than -- yields 1 only if the left side is greater than the right side
 5  &  Logical And -- yields 1 only if both sides are nonzero
 5  |  Logical Or -- yields 1 only if any of the sides is nonzero
(Note that <= and >= do not work!)

Functions

The following functions are currently supported by KeyJ's Calculator:

neg(a)
The negative value of a.
abs(a)
The absolute value of a.
sgn(a)
The sign of a (yields -1, 0 or +1).
not(a)
Logical Not -- yields 1 only if a is zero
round(a)
Round a to the nearest integer.
trunc(a), int(a)
The integer part of a. (Round a to the next integer towards zero.)
frac(a)
The fractional part of a.
sqr(a)
The square of a.
sqrt(a)
The square root of a.
exp(a)
Raises the constant e to the a-th (exponential function).
ln(a)
The natural logarithm of a.
lg(a)
The 10th logarithm of a.
ld(a)
The binary logarithm of a.
deg2rad(a)
Converts an angle in degrees to radiants.
rad2deg(a)
Converts an angle in radiants to degrees.
random(a)
An pseudo-random number in the interval [0;a[.
sin(a), cos(a), tan(a), cot(a), sec(a), csc(a)
arcsin(a), arccos(a), arctan(a), arccot(a), arcsec(a), arccsc(a)
sinh(a), cosh(a), tanh(a), coth(a), sech(a), csch(a)
arcsinh(a), arccosh(a), arctanh(a), arccoth(a), arcsech(a), arccsch(a)
Trigonometric functions. Note that they perform the calculations in radiants, not degrees. Use deg2rad and rad2deg if needed.
log(a,b)
The logarithm of a to the base b.
root(a,b)
The b-th root of a.
mod(a,b)
The remainder of the integer (!) division a/b.
fmod(a,b)
The remainder of the division a/b.

Scripting

KeyJ's Calculator features a very simple (but somewhat counter-intuitive :) scripting language. Every line entered in the interactive command line is actually executed as a simple single-line script. At the end of any script, the last value assigned to a variable by a calculation statement is written to the console log.

Script statements

Comments
All lines starting with #, // or ; are considered as comments.
Simple Calculation Statements
All lines that only consist of a single numerical expression (e.g. x^2-4*y+z) are computed and the result is stored in the special Variable Ans (Answer), or _ (underscore). The result of computations that lead to mathematical errors (e.g. division by zero, non-positive logarithm, and so on) is zero, but program execution continues. Use the special variable ` (backtick) to check if an operation failed. It contains the value 1 only if the last operation failed, or 0 if it succeeded.
Assignments
Variable:=Expression
The expression is evaluated and the result is stored in the given variable, but not in Ans.
Variable names can be in the range from a to z and are case-insensitive.
write|print
"String"
write|print Expression[:Length[:Decimals]]
write|print hex|bin|oct Expression[:Length]
The string or the result of the expression is written to the console log. The result is padded with spaces and right-aligned if length is given. If decimals is given, exactly as much decimals as specified are written out.
write, as opposed to print, does not immediately write the line to the console log, but appends it to a line buffer. This way it's possible to write multiple values in a single console line.
The hex|bin|oct qualifiers write the value as a 32-bit integer value in hexadecimal, binary or octal notation, respectively.
input Variable
Lets the user input a value (or the result of an expression) into the specified variable.
label Label
Define a jump label. Valid Label names range from 0 to 9 and a to z (case-insensitive, again).
goto Label
Jumps to the specified label.
if Condition goto Label
Evaluates the numerical expression called condition. If the result is nonzero, execution continues at the specified label.
if Condition
     ... Commands ...
[else
     ... Commands ...]
endif
Executes the commands following the if statement only if the condition evaluates to a nonzero value. Otherwise, the else block is executed, if it exists.
skip Condition
     Command
Skips the following command if condition evaluates to a nonzero value. Another way to tell it is this: the following command is only executed if condition is zero.
exit
Aborts execution of the current script.