Overview

Cl Calc is an open-source, lightweight, command-line style online calculator. While it has clean and simple interface, it is quite powerful due to the fact that its core is based on the MathJS library.

Some of the Cl Calc features include:

  • High precision (long arithmetic) math expression evaluation.
  • Variety of built-in mathematical, scientific, programming and utility functions. This includes algebra, arithmetic, combinatorics, probability, matrix, statistics, trigonometry, logical, encoding functions and more.
  • User defined variables and functions.
  • Many built-in datatypes, including complex numbers, fractions, vectors, matrices and colors.
  • Base64 Encoding, Unicode, hexadecimal and binary numbers support.
  • Persistent links to store or share your calculations.
  • Pretty-printing expressions with TeX.

Cl Calc is written purely in JavaScript and it evaluates all expressions directly in your browser. This means that your data is never sent to the server for processing or storage.

Cl Calc is completely open-source (source is available on GitLab) and licensed under MIT license. It is hosted in transparent manner through GitHub Pages.

DISCLAIMER: Since Cl Calc uses floating-point arithmetic rounding errors and other errors that lead to precision loss are inevitable. Never use Cl Calc for scientific, financial and any other calculations that need precise results. Also remember that as any software product Cl Calc and libraries it uses may have bugs and may produce incorrect results. Always double-check your calculations using multiple tools.

Quick Start

Simply type an expression and hit enter to calculate the result:

Try It!(2 + 3) * 517

You can use some of the built-in constants:

Try It!e^(i*pi)-1 + 1.2246467991473532e-16i

or you can define your own:

Try It!meaning_of_life = 4242meaning_of_life > 0true

You can also use some of the built-in functions:

Try It!sin(pi/2) + cos(pi/2)1

or define your own as well

Try It!tax_percent = 1313full_price(price) = price * (1 + tax_percent / 100)full_price(99.99)112.9887

You can work with vectors (aka lists):

Try It!x = 1:5[1, 2, 3, 4, 5]x = x * 2[2, 4, 6, 8, 10]mean(x)6

matrices:

Try It!x = [[1, 2], [3, 4]] * inv([[1, 2], [3, 4]])[[1, 0], [0, 1]]det(x)1

units:

Try It!90 km/h to m/s25 m / s110 V * 20 A * 30 days to kWh1584 kWh

colors:

Try It!hsl(#ffff0000 + #ff0000ff)[300, 100, 50]colorHsl(300, 100, 50)#FFFF00FF

and much more!

User Interface

Cl Calc user interface is designed to resemble the interface of a traditional UNIX terminal. You type expression or command into the command line and press Enter key to evaluate it.

Terminal

Terminal user interface is based on jQuery Terminal library. It supports a variety of key combinations that are described in the documentation section. Below is a brief overview of these key combinations:

Key CombinationAction
TabAutocomplete current keyword. If multiple options are available hit Tab to show the list of available completion options.
Up/DownNavigate through command history.
Ctrl+RSearch in command history.
Ctrl+LClear terminal. Alternatively, you can type clear to achieve the same result.
Shift+EnterInsert a new line.
Ctrl+A, HomeMove cursor to the beginning of the line.
Ctrl+E, EndMove cursor to the end of the line.

If you need to save your Cl Calc session for later use, or to share it with someone else you can click create link button () in the top left corner of the window. It will open a pop-up with the generated link that you can copy. If you open this link later, the whole content of your Cl Calc window will be restored.

Alternatively, you can create link that will only contain a single command from Cl Calc, by clicking on corresponding create expression link button () on the right side of the window. When you open this link, it will only restore its command and not the whole command log.

Please note that since Cl Calc doesn't store or send your commands to the server, all information required to restore your session is encoded directly in the link itself. This means that these links can get quite long.

Pretty Printing

You can view "pretty-printed" version of your calculations logs by clicking on the Toggle TeX Panel button (). Pretty-printing can be especially useful if you work with matrices or vectors as they will be displayed in more recognizable form compared to what terminal displays.

Settings

Cl Calc settings are configured through the settings dialog which can be accessed by pressing () button. Settings are saved in the browser local storage. So far there are two settings available: toggle thousands separator and precision.

Enabling thousands separator will group digits in thousands (i.e. by three digits) and use empty space as a delimiter. Selection and copying behavior is unchanged, so the delimiters won't be copied into the clipboard.

Precision setting allows to configure maximum number of significant digits for calculations using big numbers. It can be set in a range of 8 - 4096. Be careful with using high precision as it may decrease responsiveness of Cl Calc. See Precision for more information about precision.

MathJS Overview

Cl Calc uses MathJS library for expression parsing and evaluation. It is an extensive math library for JavaScript with a rich set of features and active community. Since Cl Calc only extends MathJS with some additional features, everything that is available in MathJS is available in Cl Calc.

You can find exhaustive MathJS Documentation and Examples on the MathJS website. Below is just a brief overview of its main features.

Data Types

The data types suported by MathJS are:

Number/BigNumber:

Try It!42!1405006117752879898543142606244511569936384000000000

Complex:

Try It!(2+3i)^2-5 + 12i

Fraction:

Try It!fraction("2/3") + fraction("3/4")17/12

Boolean:

Try It!3 > 2 and 3 < 5true

String:

Try It!concat("Hello, ", "World")"Hello, World"

Array (vector):

Try It![1, 2, 3] + 10[11, 12, 13]

Matrix:

Try It!det([[1, 0], [0, 1]])1

Unit:

Try It!10 km to ft32808.3989501312335958005249343832020997375328083989501312335958 ft

Built-In Constants

Below are some of the built-in constants. For the complete list please refer to MathJS Documentation.

ConstantDescription
PI/piThe number pi
tau2 * pi
E/eEuler's number
phiThe golden ratio
iImaginary unit
SQRT2Square root of 2
LN2Natural logarithm of 2
LN10Natural logarithm of 10
LOG2EBase-2 logarithm of e
LOG10EBase-10 logarithm of e

Built-In Functions

Below are some of the most common built-in functions. For the complete list please refer to MathJS Documentation.

FunctionDescriptionExamples
sin, cos, tan, cot, asin, acos, atan, atan2, acot, ...Basic trigonometry functionssin(pi) + cos(90 deg)
absCalculate the absolute value of a numberabs(-3)
logCalculate the logarithm of a valuelog(16, 2)
sqrtCalculate the square root of a valuesqrt(16)
simplifySimplify an expressionsimplify("2x^2+(5-3x)*x+x-x^2")
and, or, not, xorBasic logical functionstrue and false or true and (not false)
detCalculate determinant of a matrixdet([[1, 0], [0, 1]])
invCalculate inverse of a square matrixinv([[1, 0], [0, 1]])
combinations(n, k)Calculate number of ways of picking k unordered outcomes from n possibilitiescombinations(10, 2)
min, maxCalculate minimum/maximum value of a matrix or a listmin([1, -3, 5, 7])
meanCalculate mean value of matrix or a listmean([1, -3, 5, 7])
varCalculate variance of a matrix or a listvar([1, -3, 5, 7])
stdCalculate standard deviation of a matrix or a liststd([1, -3, 5, 7])
sumCalculate sum of a matrix or a listsum([1, -3, 5, 7])
distance, intersectBasic geometry functionsdistance([0, 0, 0], [1, 2, 3]), intersect([0, 0], [10, 10], [10, 0], [0, 10])

Operators

Below are some of the MathJS operators, for the complete list please see MathJS Documentation.

OperatorDescriptionExample
+, -, *, /Basic arithmetic operations1 + 2 - 3 * 4 / 5
^Power2 ^ 3
.*, ./, .^Element-wise multiplication, division and power[1, 2] .* [3, 4]
%, modModulus10 % 3
'Transpose[[1, 2], [3, 4]]'
!Factorial5!
&, |, ~, ^|Bitwise operations: and, or, not, xor(7 & ~5) | 8 ^| 1
<<, >>Arithmetic shifts: left, right8 >> 2
>>>Logical shift right-1 >>> 1
and, or, not, xorLogical operatorstrue and not false
==, !=, <, <=, >, >=Comparison operators1 > 2
to, inUnit conversion operators6 ft to cm
:Range1:10
? :Ternary operator (conditional expression)1 > 2 ? 0 : -1
;Statement separatorx = 1; y = 2

Variables

Variables can be defined using assignment operator and used in the same way as constants:

Try It!x = 4242myvar = [1, 2, 3][1, 2, 3]x * myvar[42, 84, 126]

Variable value can also be changed using the assignment operator:

Try It!myvar = "Hello""Hello"myvar = "World""World"myvar"World"

Please note that inside a function, function arguments take precedence over global variables:

Try It!x = "global""global"f(x) = concat("I am ", x)f("argument")"I am argument"

Functions

Functions can be defined using following format:

name(arg1, arg2, ..., argN) = body

where

  • name is the name of the function
  • arg1, arg2, ..., argN are the function arguments
  • body is an expression used to calculate the result of the function for given arguments

For example:

Try It!f(x) = x^2 + sin(x)f(pi)9.869604401089358618834490999876151135313699407240790626413349374

All global variables (including user-defined variables), functions and constants can be accessed in the function body:

Try It!density = 4242sphere_weight(radius) = density * (4 / 3 * pi * radius ^ 3)sphere_weight(5)21991.14857512855266923850368295652018938018579562574074682461214density = 1.51.5sphere_weight(5)785.3981633974483096156608458198757210492923498437764552437361479

Help

You can use built-in function help(...) to get information about built-in functions and constants. For examply, try:

help(sin)

help(phi)

Linear Interpolation

To calculate linear interpolation between two values (a, b) for a parameter (t) in the closed unit interval [0, 1] you can use lerp(...) function:

lerp(a, b, t)

Resulting value is calculated as

(1 - t) * a + t * b

a, b - can be numbers or colors, while t must always be a number:

Try It!lerp(1, 5, 0.25)2lerp(#000000, #ffffff, 0.5)#FF808080

Please note that ARGB representation is used for colors interpolation. If this is note desirable, you can define your own color interpolation based on HSV or HSL color representation. Please refer to Colors section for more information.

Hex/Bin Numbers

Cl Calc supports hexadecimal and binary numeric literals and provides functions to convert numbers to hexadecimal or binary representation.

Hexadecimal literals are prefixed with '0x' or '0X' and binary literals are prefixed with '0b' or '0B'. Both can include decimal point. On evaluation hex and binary literals are automatically converted to decimal numbers. For example:

Try It!0xFF2550xab.cd171.800781250b1101.1113.75

To convert numbers to hexadecimal or binary representation you can use hex(...) and bin(...) functions respectively. These functions accept either individual numbers or lists and matrices:

Try It!hex(255)"0xff"bin(13.75)"0b1101.11"hex([16, 200, 128])["0x10", "0xc8", "0x80"]

Hexadecimal and binary numbers maintain the same precision as regular numbers (i.e. they are BigNumbers as well):

Try It!hex(42! + 0.5)"0x3c1581d491b28f523c23abdf35b689c908000000000.8"bin(pi)"0b11.00100100001111110110101010001000100001011010001100001000110101"

Unicode

Cl Calc provides a few functions to work with Unicode encoding.

To convert a string to list of Unicode code points corresponding to each character use toCodePoints(...) function:

Try It!toCodePoints("I ♡ Cl Calc")[73, 32, 9825, 32, 67, 108, 32, 67, 97, 108, 99]

To convert back from code points list to string use fromCodePoints(...) function:

Try It!fromCodePoints([73, 32, 9825, 32, 67, 108, 32, 67, 97, 108, 99])"I ♡ Cl Calc"

Similarly, to convert a string to UTF8 you can use toUtf8(...) function:

Try It!toUtf8("I ♡ Cl Calc")[73, 32, 226, 153, 161, 32, 67, 108, 32, 67, 97, 108, 99]

To convert from UTF8 to string use fromUtf8(...) function:

Try It!fromUtf8([73, 32, 226, 153, 161, 32, 67, 108, 32, 67, 97, 108, 99])"I ♡ Cl Calc"

Base64 Encoding

To encode array of bytes to string using Base64 Encoding use base64Encode(...) function:

Try It!base64Encode([ 1, 2, 3, 4, 5 ])"AQIDBAU="

To decode Base64 encoded string back to byte array use base64Decode(...) function:

Try It!base64Decode("AQIDBAU=")[1, 2, 3, 4, 5]

You can encode string to Base64 by encoding it to UTF8 first:

Try It!base64Encode(toUtf8("Hello World"))"SGVsbG8gV29ybGQ="fromUtf8(base64Decode("SGVsbG8gV29ybGQ="))"Hello World"

Hash Functions

Cl Calc supports the following hash functions: md5, sha1, sha224, sha256. Each hash function accepts an array of bytes as its argument and produces hash string as the result. Below are some examples:

Try It!data = toUtf8("Hello World")[72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100]md5(data)"b10a8db164e0754105b7a99be72e3fe5"sha1(data)"0a4d55a8d778e5022fab701977c5d840bbc486d0"sha224(data)"c4890faffdb0105d991a461e668e276685401b02eab1ef4372795047"sha256(data)"a591a6d40bf420404a011733cfb7b190d62c65bf0bcda32b57b277d9ad9f146e"

Colors

Cl Calc supports some operations with colors by introducing Color data type, color literals and some functions that work with colors.

There are few ways to construct a Color. The easiest one is to use color literals. Color literals resemble web color format, where literal starts with hash character '#' followed by three bytes in hex representing RGB components, or four bytes in hex representing ARGB components (where A is an alpha component). If RGB format is used, alpha component is set to 255 (i.e. opaque). For example:

Try It!#ABCDEF#FFABCDEF#80FF00FF#80FF00FF

As you may notice, when Cl Calc outputs color as a result of an expression, it is displayed as a color preview square. You can click it to see color value. Checkerboard background used to preview opacity.

You can also construct color explicitly by calling any of the following functions:

ConstructorDescriptionExample
color4b(a, r, g, b)Construct color from ARGB bytes.color4b(255, 0, 0, 255)
color3b(r, g, b)Construct color from RGB bytes. Alpha is set to 255.color3b(0, 0, 255)
color4f(a, r, g, b)Construct color from four floating-point numbers in a range [0, 1].color4f(1, 0, 0, 1)
colorU32(number)Construct color from unsigned 32-bit integer where each byte (higher to lower) represents ARGB components.colorU32(0xFF0000FF)
colorHsv(h, s, v)Construct color from HSV (hue, saturation, value) components.colorHsv(240, 100, 100)
colorHsl(h, s, l)Construct color from HSL (hue, saturation, lightness) components.colorHsl(240, 100, 50)

You can add, subtract, divide and multiply colors in the same way as regular numbers. For these operations ARGB representation is used, and operation performed on each component individually:

Try It!#FF0000 + #00FF00#FFFFFF00#ffffff - #0080ff00#FF7F00FF

You can also multiply and divide colors by a number:

Try It!#808080 * 2#FFFFFFFF#808080 / 2#80404040

You can convert color to different representations using following functions:

FunctionDescriptionExample
rgb(color)Convert color to RGB bytes.rgb(#0000FF)
argb(color)Convert color to ARGB bytes.argb(#0000FF)
argbf(color)Convert color to ARGB components, where each component is represented by a floating-point number in a range [0, 1].argbf(#0000FF)
hsv(color)Convert color to HSV representation.hsv(#0000FF)
hsl(color)Convert color to HSL representation.hsl(#0000FF)

Function Plot

Cl Calc can plot a two-dimensional graph of a function with plot2d(...) function.

plot2d(expression, variable, [ from, to ])

where

  • expression an expression of a function to plot
  • variable variable name that will be used as an 'x' variable (horizontal axis)
  • from, to numbers representing begin and end of the interval that will be plotted

For example:

Try It!plot2d(sin(1/x), x, [-5, 5])

You can zoom function graph with a mouse wheel and drag it by holding left mouse button.

Please note that in its current implementation plot2d function doesn't detect vertical asymptotes. If you are plotting function that has vertical asymptotes you may see vertical lines appear where the function value "jumps".

Precision

Cl Calc is configured to use big numbers when possible with the default precision of 64 significant digits (you can read more about underlying long arithmetics library on Decimal.js GitHub Page). The precision can be adjusted in Settings.

While most of the MathJS functions and operators support big numbers, there are still some that don't and will degrade precision to 64-bit floating-point numbers. For example, complex numbers do not support long arithmetics and will degrade precision:

Try It!abs(30!)265252859812191058636308480000000abs(30! + 0i)2.6525285981219107e+32

Aliases

For convenience, Cl Calc introduces some function aliases to built-in MathJS functions:

AliasMathJS Function
nCrcombinations
nPrpermutations

Last Evaluated Expression

For convenience, Cl Calc stores value of the last evaluated expression in the built-in variable '$'. It can then be used in the same manner as a regular variable. However, it doesn't work with a function assignment.

Try It!2+35$+27$7

Be extra careful with '$' variable, since its value updated after every expression evaluation. It is nothing more then just a shortcut to quickly access the value of the previous expression.

About

Cl Calc is licensed under MIT license and its source code is available on the GitLab.

For contributing, reporting bugs or suggesting features please visit issues page.

Current version: 1.0.0.

Third party libraries: