*** Help on syntax ***
There are two types of input: expressions and commands. Commands always start with a forward slash, '/'. To get help on various commands, type '? commands'.
** Expressions **
Expressions are simple mathematical formulas of what you want to calculate. These expressions are made up out of numbers, variables, functions and operators. Some examples:
** Numbers **
Numbers consist of digits, an optional decimal point '.', and an optional exponent. Additionally, bases other than 10 are also supported using a base prefix. These prefixes are:
0b... - binary (base 2)
0o... - octal (base
0d... - decimal (base 10)
0x... - hexadecimal (base 16)
\b\... - base 'b', where 2 <= b <= 36
The decimal point and exponent are supported even for bases other than 10. Bases higher than 10 use 'a' through 'z' for digits, where 'a' represents 10 and z represents 36. Alphabetical digits are not case sensitive. Exponents are also in their respective base.
0b11011011 = 219
0x34fc.4 = 13564.25
0x1.8_e2 = 1.5 * 16**2 = 384
\24\3fg5 = 50501
Note that for the 0x and \b\ prefixes, an underscore before the 'e' of the exponent is mandatory, to distinguish the exponent from the digit 'e'. See the /setbase command (enter '? /setbase') for information on altering the default output base.
** Identifiers **
Variables, constants and functions are referenced by an identifier. Identifiers always start with a letter, and are followed by zero or more letters and/or digits. Rather than the digits in numbers, the letters in variables are case sensitive. To (re)assign a variable or funtion, use the := operator:
x := 3 + 45
f(a) := 2*a
g(b) := f(b/2)+x
foo(a,b) := a/b
As you can see, functions with more than one argument are supported as well. Note that, on assignment, any other variable or function referenced in the definition is copied as is. Therefore, in above example, reassigning the function f() after assigning g() does not change the meaning of g(). This also means that recursive functions are not supported - using f() in the definition of f() either uses the previous definition of f(), or generates an error that f is undefined.
Any whitespace (tabs or spaces) between operators, numbers and identifiers in expressions is ignored. Therefore, 'foo ( a, b) := a / b' has exactly the same meaning als 'foo(a,b):=a/b'. For an exhaustive list of operators, built-in functions and built-in constants, type '? operators', '? functions' and '? constants', respectively.
** #-Syntax **
Whenever you enter an expression, you'll notice that the result is prefixed by a '#' and a number in square brackets. Each result is stored in a buffer, and you can use this number to reference it later on, using '#' immediately followed by a number (no whitespace is allowed between '#' and the number). To simply refer to the last result, type '#' without a number. Alternatively, you can use the ans() function. Enter '? ans' for more details on this subject.
Clears the output window.
Shows a list of definitions.
/for i:from..to [step x], expression
Iterates the variable 'i' from 'from' to 'to' (with an optional step size 'x'), evaluating 'expression' each iteration, the result printed to the screen and stored in the result buffer.
/for x:2..4*5, 8*x
/for i:0..1 step 0.1, sqrt(i)
Exits the application.
Shows a list of all the results stored in the result buffer.
/setbase b1 [, b2 [, b3 ... ] ]
Sets one or more output bases. Acceptable bases range from 2 to 36.
*** Help on Operators ***
Kol Q l8or fully supports most the arithmetic operators of C-like programming languages, and some more. Note that, as this tool is mainly targeted at programmers, ^ is the bitwise XOR operator and not the power operator. For power, use **.
(): Grouping parentheses
+: Addition (binary) or positive (unary)
-: Subtraction (binary) or negation (unary)
%: Modulo (remainder)
&, &&: Bitwise and logical AND
|, ||: Bitwise and logical OR
^, ^^: Bitwise and logical XOR
~, !: Bitwise and logical NOT
<<, >>: Bitwise shift left and right
>>>: Arithmetic shift right (extends the sign-bit)
=, ==: Equality
!=, <>: Nonequality
<, <=, >, >=: Order
** A note on logical operators **
While Kol Q l8or does not support boolean types, it interprets 0 as false and any other value as true, like many C-like languages. The result of a logical expression however, will always be 0 (false) or 1 (true)
|| and && support short-circuiting. That is, the right-hand side of the operator is not evaluated unless the left-hand side is false in case of || or true in case of &&. This is particularly useful for expensive computations.
** Operator precedence and associativity **
The precedence and associativity are also similar to languages such as C and C++. The operators here are listed from most precedent to least precedent.
unary +, unary -, ~, !
*, /, %
binary +, binary -
<<, >>, >>>
<, <=, >, >=
=, ==, !=, <>
All operators are left-associative, with the exception of **.
Calculates the absolute value of x
Calculates the inverse cosine of x
Yields the result of the x'th calculation (#x), or 0 if it doesn't exist. If x<0, it counts backwards (so ans(-1) returns the last calculation). ans(0) returns the number of results currently stored in the result buffer.
The main difference between #x and ans(x) is that #x is evaluated immediately, while ans(x) is evaluated every time it is encountered. This makes ans(x) with negative x particularly useful in /for loops, to refer to results of earlier iterations.
apply(i:from..to, r:init, expression [, stopcondition])
Initializes the variable 'r' with 'init', then iterates the variable 'i' from 'from' to 'to', evaluating 'expression' each time and assigns the result to 'r'. After all iterations, or as soon as the optional 'stopcondition' evaluates to true, it returns the final variable 'r'.
apply(x:1..10, r:0, r+x) => 1+2+3+...+10
apply(i:1..#0, c:0, c+ans(i)) => the sum of all results in the result buffer
apply(i:1..Infinity, r:1, r*2, r>1000) => the first power of two after 1000
Calculates the inverse sine of x
Calculates the inverse tangent of x
Rounds x up towards the nearest integer
Calculates the cosine of x
Calculates the hyperbolic cosine of x
Raises e to the x'th power, or e**x
Calculates the factorial of x
Discards everthing before the decimal point (equivalent to frac(x))
Rounds x down towards the nearest integer
Discards everthing before the decimal point (equivalent to float(x))
Evaluates 'condition'. If true, it evaluates and returns 'truepart'. Otherwise, it evaluates and returns 'falsepart'.
Truncates x (discards everything after the decimal point)
Calculates the natural (base-e) logarithm of x (equivalent to ln(x))
Calculates the base-10 logarithm of x (equivalent to log10(x))
Calculates the base-10 logarithm of x (equivalent to log(x))
Calculates the natural (base-e) logarithm of x (equivalent to loge(x))
Calculates the sine of x
Calculates the hyperbolic sine of x
Calculates the square root of x
Calculates the tangent of x
Calculates the hyperbolic tangent of x
A constant for infinity.
Not a number.
The base of the natural logarithm.
The ratio of a circle's circumference to its diameter.
*** Tips and Tricks ***
apply(), ans() and /for can be very powerful tools for more complicated calculations. Here are some examples.
Calculate the sum of result x through y
sumresults(x,y) := apply(i:x..y, r:0, r+ans(i))
Verify whether a number is prime (warning, can be slow for large primes)
isprime(n) := n >= 2 && (n <= 3 || (n % 2 != 0 && apply(i:1..sqrt(n)/2, r:1, n % (i*2+1) != 0, !r)))
Generate Fibonacci numbers
/for i:0..20, ans(-1) + ans(-2)
(repeat the /for loop immediately after for even more numbers)