Calculate
Function reference for c-calculate

General

c-c <expression>

Shorthand syntax for c-calculate.
1
> c-c 11 * 2
2
22
Copied!

c-c m [r | d]

Shorthand syntax for switching trigonometric modes.
1
> c-c m r
2
Set calculation mode to radians
3
​
4
> c-c m d
5
Set calculation mode to degrees
Copied!

deg, rad

Special modifiers that provide a shortcut for converting values between degrees and radians.
1
> c-c m r
2
Set calculation mode to radians
3
​
4
> c-c sin(90)
5
0.893996663600557
6
​
7
> c-c sin(90 deg)
8
1
9
​
10
> c-c sin(pi/2 rad)
11
1
12
​
13
> c-c 45 deg
14
0.785398163397448
15
​
16
> c-c m d
17
Set calculation mode to degrees
18
​
19
> c-c cos(pi)
20
0.998497149863863
21
​
22
> c-c cos(pi rad)
23
-1
24
​
25
> c-c cos(180 deg)
26
-1
27
​
28
> c-c pi/4 rad
29
45
Copied!

0b...

Prefix for writing numbers in binary notation.
1
> c-c 0b1111
2
15
Copied!

0o...

Prefix for writing numbers in octal notation.
1
> c-c 0o77
2
63
Copied!

0x...

Prefix for writing numbers in hexadecimal notation.
1
> c-c 0xffffff
2
16777215
3
​
4
> c-c 0xFFFFFF
5
16777215
Copied!

a'n

​Radix notation. This allows you to express a number n in any base a, from 1 to 64.
1
> c-c 2'10000110000
2
1072
3
​
4
> c-c 8'2060
5
1072
6
​
7
> c-c 25'1hm
8
1072
9
​
10
> c-c 32'11g
11
1072
12
​
13
> c-c 47'mC
14
1072
Copied!

Operators

CalcBot supports the following operators, and evaluates them in the order listed:

++a, --a

Add or subtracts 1 from a, and assigns the result to a. Returns the value of a after it was incremented (the value assigned to a).
1
> c-c x = 2
2
2
3
​
4
> c-c ++x
5
3
6
​
7
> c-c x
8
3
Copied!

a++, a--

Add or subtracts 1 from a, and assigns the result to a. Returns the value of a before it was incremented.
1
> c-c x = 2
2
2
3
​
4
> c-c x++
5
2
6
​
7
> c-c x
8
3
Copied!

not n

Negates n. If n is a truthy value, false (0) is returned. Otherwise, true (1) is returned.
1
> c-c not true
2
0
3
​
4
> c-c not false is true
5
1
Copied!

~n

Invert the bits of n. The fractional part of n will be truncated if there is any.
1
> c-c ~255
2
-256
3
​
4
> c-c ~~255
5
255
Copied!

n!

Take the factorial of n. For example, 6! is equivalent to 6 * 5 * 4 * 3 * 2 * 1.
1
> c-c 6!
2
720
Copied!

a ^ b

Raise a to the power of b.
1
> c-c 2 ^ 3
2
8
Copied!

a * b

Multiply a and b.
1
> c-c 2 * 4
2
8
Copied!

a / b

Divide a by b.
1
> c-c 15 / 5
2
3
Copied!

a % b

Divide a by b and return the remainder of the result. This is also known as modulus division, or remainder division.
1
> c-c 8 % 2
2
0
Copied!

a + b

Add a and b.
1
> c-c 1 + 1
2
2
Copied!

a - b

Subtract b from a.
1
> c-c 1 - 1
2
0
Copied!

a << b

Shift all the bits in a to the left b times. For example, 1 << 3 is equal to 2 ^ 3. After shiting by 3 bits, the resulting binary is 1000, equivalent to 8.
1
> c-c 1 << 3
2
8
3
​
4
> c-c (5 << 2) + 5
5
25
Copied!

a >> b

Shift all the bits in a to the right b times. Bits at the end of the number will get discarded.
1
> c-c 8 >> 3
2
1
3
​
4
> c-c 25 >> 3
5
3
Copied!

a == b

Returns true (1) if a is equal to b.
1
> c-c 3 == 1 + 2
2
1
3
​
4
> c-c not false == true
5
1
Copied!

a != b

Returns true (1) if a is not equal to b.
1
> c-c 3 != 1 + 2
2
0
3
​
4
> c-c re(3i + 2) != im(3i + 2)
5
1
Copied!

a ~== b

Returns true (1) if a is approximately equal to b. The difference between them must be less than 1 * 10 ^ -6. For complex numbers, this operator will compare the real and imaginary components separately.
This operator is intended to be used when comparing the results of certain mathematical operations that produce slightly imprecise results (like prime notation).
1
> c-c 3.0000002 ~== 3
2
1
3
​
4
> c-c 3i + 2 ~== 2.9999999i + 2
5
1
Copied!

a ~!= b

Negates the behavior of the ~== operator.
1
> c-c 3 ~!= 3
2
0
3
​
4
> c-c 5i + 2 ~!= i
5
1
Copied!

a > b

Returns true (1) if a is greater than b.
1
> c-c 3 > 2
2
1
Copied!

a < b

Returns true (1) if a is less than b.
1
> c-c 3 < 2
2
0
Copied!

a >= b

Returns true (1) if a is greater than or equal to b.
1
> c-c 3 >= 2
2
1
3
​
4
> c-c 4 >= 4
5
1
Copied!

a <= b

Returns true (1) if a is less than or equal to b.
1
> c-c 3 <= 2
2
0
3
​
4
> c-c 4 <= 4
5
1
Copied!

a & b

Compares the bits of a and b one by one. If both bits have a value of 1, the corresponding bit in the new number will also be 1.
1
> c-c isodd(n) = n & 1
2
isodd(n) = n & 1
3
​
4
> c-c isodd(7)
5
1
6
​
7
> c-c 0b111 & 0b010
8
2
Copied!

a | b

Compares the bits of a and b one by one. If either bit has a value of 1, the corresponding bit in the new number will also be 1.
1
> c-c 0b1100 | 0b0011
2
15
3
​
4
> c-c 178 | 0
5
178
Copied!

a && b

Returns true if both a and b are truthy values.
1
> c-c 3 && 4
2
1
3
​
4
> c-c 3 && 0
5
0
Copied!

a || b

Returns true if either a or b are truthy values.
1
> c-c 3 || 4
2
1
3
​
4
> c-c 3 || 0
5
1
6
​
7
> c-c 0 || 0
8
0
Copied!

a = b

Assigns the value of b to the symbol a. If a isn't a valid symbol, this operation will throw an error.
1
> c-c x = y = 100
2
100
3
​
4
> c-c x + y
5
200
6
​
7
> c-c 3x + 4 = 0
8
Variable names can only consist of letters and underscores.
Copied!

a ^= b, a *= b, a /= b, a %= b, a += b, a -= b, a <<= b, a >>= b, a &= b, a |= b, a &&= b, a ||= b

Compound assignment operators. For example, writing a ^= b is a shortcut for writing a = a ^ b; writing a += b is a shortcut for a = a + b, etc. If a isn't a valid symbol, this operation will throw an error.
1
> c-c x = 24
2
24
3
​
4
> c-c x /= 6
5
4
6
​
7
> c-c x
8
4
Copied!

Control flow functions

bool(v)

Returns true (1) if v is a truthy value. Otherwise, false (0) is returned.
1
> c-c bool(3i)
2
1
3
​
4
> c-c bool(0)
5
0
Copied!

if(cond, true_exp, false_exp = NaN)

Returns the value of true_exp if cond resolves to a truthy value. Otherwise, false_exp is returned. If cond resolves to a falsy value but false_exp was not provided, NaN is returned.
1
> c-c x=5
2
5
3
​
4
> c-c if(x > 2, 2x, x)
5
10
Copied!

loop(exp, start, end, step = 1, accum_exp = cur + acc)

Evaluates exp where the special variable ind represents the current index of the loop. ind will be initially set to start; then it will increment by step until it reaches end, at which point the loop will break and return the value of acc.
If step is not provided, it will be set to either 1 or -1 depending on the values of start and end.
accum_exp is an expression that contains two special variables, cur and acc. cur represents the current value of exp, while acc represents the combined values of all old values of cur. Therefore, you can set accum_exp to, for example, get the sum of a sequence exp bounded by start and end. See the examples below for various ways you can utilize accum_exp.
1
> c-c loop(ind, 0, 5, 1)
2
5
3
​
4
> c-c loop(2ind, 0, 5, 1)
5
10
6
​
7
> c-c loop(3ind+1, 11, 22, 2, cur * acc)
8
12075581440
Copied!

try(exp, error_exp)

Returns the value of exp. If an error is generated while evaluating exp, error_exp will be returned instead.
1
> c-c try(circle(3i - 2), 1)
2
1
3
​
4
> c-c try(6^2, 2)
5
36
6
​
7
> c-c try(circle(3i - 2), (3i)!)
8
The `!` operator's left argument must be of type `number`.
Copied!

Summation and product

sum(exp, variable, start, end)

Returns the summation of exp, evaluated from when variable = start to variable = end. Both bounds are inclusive.
1
> c-c sum(n, n, 1, 100)
2
5050
3
​
4
> c-c sum(n^n/n, n, 1, 6)
5
8477
Copied!

product(exp, variable, start, end)

Returns the product of exp, evaluated from when variable = start to variable = end. Both bounds are inclusive.
1
> c-c product(n, n, 1, 10)
2
3628800
3
​
4
> c-c 10!
5
3628800
Copied!

Substitution

subst(exp, variable, value)

Substitutes value for the variable in the given expression. For example, subst(x^2+5x+6, x, 0) substitutes 0 for x in the expression x^2+5x+6, giving 6.
1
> c-c subst(x^2+5x+6, x, 0)
2
6
3
​
4
> c-c subst((y+5)(y-2), y, -5)
5
0
Copied!

Trigonometric functions

sin(angle), cos(angle), tan(angle)

Returns the sine, cosine, or tangent of the angle.
1
> c-c sin(pi/2)
2
1
3
​
4
> c-c cos(pi/2)
5
0
6
​
7
> c-c tan(pi/4)
8
1
Copied!

csc(angle), sec(angle), cot(angle)

Reciprocal functions of sin(angle), cos(angle), and tan(angle) respectively. For example, csc(angle) = 1 / sin(angle).
1
> c-c csc(pi/2)
2
1
3
​
4
> c-c sec(pi/4)
5
1.414213562373095
6
​
7
> c-c cot(pi/4)
8
1
Copied!

asin(value), acos(value), atan(value)

Inverse functions of sin(angle), cos(angle), and tan(angle) respectively.
1
> c-c asin(1)
2
1.5707963267948966
3
​
4
> c-c acos(0)
5
1.5707963267948966
6
​
7
> c-c atan(1)
8
0.7853981633974483
Copied!

atan2(y, x)

Two-argument inverse tangent function.
1
> c-c atan2(-2, 1)
2
-1.1071487177940904
Copied!

acsc(value), asec(value), acot(value)

Inverse functions of csc(angle), sec(angle), and cot(angle) respectively.
1
> c-c acsc(1)
2
1.5707963267948966
3
​
4
> c-c asec(sqrt(2))
5
0.7853981633974484
6
​
7
> c-c acot(1)
8
0.7853981633974483
Copied!

sinh(value), cosh(value), tanh(value)

Returns the hyperbolic sine, cosine, or tangent of the value.
1
> c-c sinh(e/2)
2
1.8179831047980461
3
​
4
> c-c cosh(e/2)
5
2.074864470111516
6
​
7
> c-c tanh(e/2)
8
0.8761936651700128
Copied!

csch(value), sech(value), coth(value)

Reciprocal functions of sinh(value), cosh(value), and tanh(value) respectively. For example, csch(value) = 1 / sinh(value).
1
> c-c csch(e/2)
2
0.5500601173689602
3
​
4
> c-c sech(e/2)
5
0.48195919030135675
6
​
7
> c-c coth(e/2)
8
1.1413001939542262
Copied!

asinh(value), acosh(value), atanh(value)

Inverse functions of sinh(value), cosh(value), and tanh(value) respectively.
1
> c-c asinh(1/2)
2
0.48121182505960347
3
​
4
> c-c acosh(3/2)
5
0.9624236501192069
6
​
7
> c-c atanh(1/2)
8
0.5493061443340548
Copied!

acsch(value), asech(value), acoth(value)

Inverse functions of csch(angle), sech(angle), and coth(angle) respectively.
1
> c-c acsch(1/2)
2
1.4436354751788103
3
​
4
> c-c asech(1/2)
5
1.3169578969248166
6
​
7
> c-c acoth(1/2)
8
-1.5707963267948966i + 0.5493061443340548
Copied!

Degree / radian conversion

dtr(degree), rad(degree)

Returns the given value converted to radians.
1
> c-c dtr(180)
2
3.141592653589793
3
​
4
> c-c rad(180)
5
3.141592653589793
Copied!

rtd(radian), deg(radian)

Returns the given value converted to degrees.
1
> c-c rtd(pi)
2
180
3
​
4
> c-c deg(pi)
5
180
Copied!

circle(value)

Returns the specified portion of one full revolution of a circle. For example, circle(0.5) returns half of a full revolution. If the current trigonometric mode is degrees, this function returns value * 360; otherwise, if it is radians, this function returns value * 2 * pi.
1
> c-c m d
2
Set calculation mode to degrees
3
​
4
> c-c circle(0.5)
5
180
Copied!

Exponential / logarithmic functions

scientific(a, b)

Returns a * 10 ^ b.
1
> c-c scientific(5.1262, 4)
2
51262
Copied!

exp(x)

Exponential function with base e. Returns e ^ x.
1
> c-c exp(2)
2
7.3890560989306495
Copied!

log(x, y = 10)

Logarithmic function with base 10 by default.
1
> c-c log(10)
2
1
3
​
4
> c-c log(8, 2)
5
3
Copied!

ln(x)

Inverse function of exp(x). Equivalent to the logarithmic function with base e, or log(x, e).
1
> c-c ln(e)
2
1
Copied!

Root / power functions

sqrt(n)

Returns the square root of n.
1
> c-c sqrt(16)
2
4
3
​
4
> c-c sqrt(-4)
5
2i
Copied!

cbrt(n)

Returns the cube root of n.
1
> c-c cbrt(27)
2
3
Copied!

root(n, i)

Returns the ith root of n. For example, root(16, 2) is equal to sqrt(16).
1
> c-c root(16, 2)
2
4
3
​
4
> c-c root(729, 6)
5
3
Copied!

pow(n, p)

Returns n raised to the p power. This function is implicitly called when using the alternative syntax: n ^ p.
1
> c-c pow(16, 1/2)
2
4
3
​
4
> c-c pow(2, 3)
5
8
6
​
7
> c-c pow(27, 1/3)
8
3
Copied!

Complex numbers

re(z)

Returns the real part of complex number z.
1
> c-c re(3i + 2)
2
2
Copied!

im(z)

Returns the imaginary part of complex number z.
1
> c-c im(3i + 2)
2
3
Copied!

arg(z)

Returns the argument of complex number z.
1
> c-c arg(3i + 2)
2
0.9827937232473291
Copied!

conj(z)

Returns the complex conjugate of z.
1
> c-c conj(3i + 2)
2
-3i + 2
Copied!

Sequences

fib(n)

Returns the nth term of the Fibonacci sequence.
1
> c-c fib(8)
2
21
Copied!

Miscellaneous functions

erf(z)

Returns an approximation of the error function of z.
1
> c-c erf(0.5)
2
0.5204999077232427
Copied!

erfc(z)

The complementary error function.
1
> c-c erfc(0.3)
2
0.6713732158964137
3
​
4
> c-c 1-erf(0.3)
5
0.6713732158964137
Copied!

rand()

Returns a random number from 0 (inclusive) to 1 (non-inclusive).
1
> c-c rand()
2
(results will vary)
Copied!
Here is a custom function implementation that will generate random integers from a selected minimum and maximum, inclusively:
1
> c-c randint(min, max = false) = if (max is false, floor(rand() * (min + 1)), floor(rand() * (max - min + 1)) + min)
2
Custom function created
3
​
4
> c-c randint(15, 35)
5
(random integer from 15 to 35, inclusive)
Copied!

factorial(n)

Returns the factorial of n. This function is implicitly called when using the alternative syntax: n!
1
> c-c factorial(6)
2
720
Copied!

gamma(n)

Returns the gamma function of n using Lanczos's approximation.
1
> c-c gamma(5)
2
24
3
​
4
> c-c gamma(3i + 4)
5
-1.511251952289958i - 1.1294284935320542
Copied!

ncr(n, k)

Combinations function. Returns the coefficient of the term x ^ k in the polynomial expansion of the binomial (1 + x) ^ n. This is also the number in row n column k of Pascal's triangle.
1
> c-c ncr(8, 2)
2
28
Copied!

npr(n, r)

Permutations function. Computes the number of ways to obtain an ordered subset of r elements from a set of n elements.
1
> c-c npr(8, 2)
2
56
Copied!

abs(n)

Returns the absolute value of n.
1
> c-c abs(-4)
2
4
3
​
4
> c-c abs(4)
5
4
Copied!

lerp(v1, v2, t)

Returns a value linearly interpolated fromv1 to v2 by a constant t. For example, lerp(0, 10, 0.5) returns the midpoint of 0 and 10.
1
> c-c lerp(0, 10, 0.5)
2
5
Copied!

invlerp(v1, v2, v)

Calculates the linear parameter that produces the interpolant v from v1 to v2.
1
> c-c invlerp(0, 10, 5)
2
0.5
Copied!

siground(n, d)

Returns n rounded to d significant digits.
1
> c-c siground(3.1567, 2)
2
3.2
Copied!

round(n, s = 1)

Returns n rounded to the nearest s, integer by default.
1
> c-c round(0.45)
2
0
3
​
4
> c-c round(0.5)
5
1
6
​
7
> c-c round(0.15, 0.25)
8
0.25
Copied!

ceil(n, s = 1)

Returns n rounded up to the next s, integer by default.
1
> c-c ceil(0.45)
2
1
3
​
4
> c-c ceil(0)
5
0
6
​
7
> c-c ceil(0.26, 0.25)
8
0.5
Copied!

floor(n, s = 1)

Returns n rounded down to the next s, integer by default.
1
> c-c floor(0.65)
2
0
3
​
4
> c-c floor(1)
5
1
6
​
7
> c-c floor(0.74, 0.4)
8
0.4
Copied!

min(a, b)

Returns the lesser value of a and b.
1
> c-c min(1, 3)
2
1
Copied!

max(a, b)

Returns the greater value of a and b.
1
> c-c max(1, 3)
2
3
Copied!

gcf(a, b)

Computes the greatest common factor of a and b.
1
> c-c gcf(35, 7)
2
7
Copied!

lcm(a, b)

Computes the least common multiple of a and b.
1
> c-c lcm(4, 5)
2
20
Copied!

clamp(n, l, r)

Returns n, clamped to the given range set by l (left, negative) and r (right, positive)
1
> c-c clamp(0.5, 5, 6)
2
5
3
​
4
> c-c clamp(4.2, 4, 5)
5
4.2
6
​
7
> c-c clamp(9, 1, 2)
8
2
Copied!

sign(n)

Returns the polarity of n.
1
> c-c sign(pi)
2
1
3
​
4
> c-c sign(0)
5
0
6
​
7
> c-c sign(-pi)
8
-1
Copied!

size(n)

Returns the amount of bits in the binary representation of n. The fractional part of n will be truncated if there is any.
1
> c-c size(0b1111)
2
4
3
​
4
> c-c size(255)
5
8
Copied!
Last modified 4d ago