This part of the documentation explains the syntax of valid SpeedCrunch input. As you will see, SpeedCrunch honors most conventions for mathematical expressions. You will find using SpeedCrunch to be very natural and intuitive, especially so if you are already familiar with a programming language.
When you would like to specify a non-integer value, simply enter the number as you would write it on paper,
with either a period (.
) or a comma (,
) as the decimal separator. By default, these can be
used interchangeably, i.e. both 1.234
and 1,234
represent the same number. However, this
behavior can be changed; see Radix Character for more information.
Trailing zeros after the decimal point (like in 12.300
) or leading zeros before it (0012.3
) are redundant and can be included or omitted to the user’s preference. Expressions like .5
as a shorthand notation for 0.5
are also permitted.
When dealing with very small or very large numbers (think the size of an atom or of a galaxy) the notation above is inconvenient. These are more commonly expressed in scientific notation; for instance, 1.234*10-9 is preferable to 0.000000001234.
Naturally, in SpeedCrunch this could be written as 1.234*10^-9
, but there’s also a shorthand notation: 1.234e-9
. Here, the e
represents *10^
, but it is considered a part of the number literal and treated with higher precedence. For example, 1e2^3
is equivalent to (1e2)^3 = 100^3
. The scale of a number (sometimes called its exponent) always begins with the scale character E
or e
followed by a signed integer. So e+10
, e-4
, E-0
are all valid scale expressions. If the sign is ‘+’, you may simply omit it: e0
, E10
. The significand (i.e. the part preceding the exponent) is required; exactly one exponent must be specified.
Compared to most calculators, SpeedCrunch can accept very large numbers without overflowing (e.g. both 1e+536870911
and 1e-536870911
are still valid). However, only about 78 significant digits are stored at any point. Any digits beyond that are lost.
In addition to decimal (base-10) numbers, SpeedCrunch provides support for binary (base-2), octal (base-8) and hexadecimal (base-16) numbers. You can enter a number in any of these bases by marking it with the corresponding prefix:
0b
or 0B
for binary, e.g. 0b10010
.0o
or 0O
for octal, e.g. 0o1412
.0d
or 0D
for decimal. These can be omitted since decimal is the default base.0x
, 0X
, or #
for hexadecimal. The additional six digits are represented by the upper or lower case letters a
to f
, e.g. 0xdeadbeef
or 0xDEADBEEF
.You may even enter fractional values in any of these bases. Note that scientific notation is not supported for non-decimal bases, however. Examples:
0b1.01
= 1.25
0xf.a
= 15.625
To have SpeedCrunch output its results in a base other than decimal, you may use one of the functions bin()
, oct()
, dec()
, or hex()
:
hex(12341)
= 0x3035
The effect of these functions only applies to the immediate result and doesn’t carry to future operations:
0x2 * hex(12341)
= 24682
To change the base that is used for displaying results, select one of the corresponding settings in
.SpeedCrunch stores integers with a precision of up to 256 bits. Since this would be unwieldy, the binary representation of a negative number in SpeedCrunch is not its two’s complement. Instead, like with other bases, the value and the sign are represented separately:
bin(-1)
= -0b1
See mask()
and unmask()
to convert a negative number into the two’s complement form.
Any integer larger than the 256-bit limit will be silently converted into a floating point number, making it susceptible to rounding errors.
To specify large integers, using the shift operators (1 << n
) is preferable to exponentiation (2 ^ n
) as the latter are floating point
calculations and thus susceptible to rounding errors.
When writing an expression like 10+5*4
, which operation will be executed first? The common rules of operator precedence tell us that in this case multipication shall be computed first, hence the result is 30
. We also distinguish unary operators (which act on a single number/operand) and binary operators (which link two operands).
SpeedCrunch supports the following operators, listed in order of decreasing precedence:
Operator | Description | Examples |
---|---|---|
(...) |
|
(2+3)*4 = 5*4 = 20 |
x! |
|
5! = 120 |
a ^ b , a ** b |
|
2^2^3 = 2^8 = 256 |
+x , -x |
Unary plus and minus | --5 = +5 |
a \ b |
|
5\4 = 1 |
a * b , a b , a / b |
New in version 0.12: Implicit multiplication was added SpeedCrunch 0.12. |
3 sqrt(2) |
a + b , a - b |
Addition and subtraction | |
a << n , a >> n |
|
|
a & b |
|
0b11 & 0b10 = 0b10 |
a | b |
|
0b10 | 0b01 = 0b11 |
-> , in |
|
|
The following operators used to be supported, but were either removed from recent SpeedCrunch versions or are considered deprecated. Generally, these features were removed because of significant problems so you may want to avoid them even if they’re still supported in your version of SpeedCrunch.
Operator | Description | Examples |
---|---|---|
x% |
Deprecated since version 0.12: This operator was removed in SpeedCrunch 0.12 as it was confusing and not very useful. The reasons for its removal are discussed in more detail in issue #239. |
10% = 0.1 |
f x |
Deprecated since version 0.12: Use of this feature is discouraged because it allows for very ambiguous expressions. It will likely be removed in a future release. |
sqrt 2 = sqrt(2) |