• Stars
    star
    3,147
  • Rank 14,288 (Top 0.3 %)
  • Language PureScript
  • License
    MIT License
  • Created over 7 years ago
  • Updated 9 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

High precision scientific calculator with support for physical units

insect

A high precision scientific calculator with full support for physical units.

Try the web version here: https://insect.sh

Contents

Documentation

  • Evaluate mathematical expressions:

    1920/16*9
    2^32
    sqrt(1.4^2 + 1.5^2) * cos(pi/3)^2
    
    • Operators: addition (+), subtraction (-), multiplication (*, ·, ×), division (/, ÷, per), exponentiation (^, **). Full list: see Reference below.

    • Mathematical functions: abs, acos, acosh, acot/arccotangent, acoth/archypcotangent, acsc/arccosecant, acsch/archypcosecant, arcsecant, asech/archypsecant, asin, asinh, atan2, atan, atanh, ceil, cos, cosh, cot/cotangent, coth/hypcotangent, csc/cosecant, csch/hypcosecant, exp, floor, fromCelsius, fromFahrenheit, gamma, ln, log10, log, maximum, mean, minimum, round, secant, sech/hypsecant, sin, sinh, sqrt, tan, tanh, toCelsius, toFahrenheit.

    • High-precision numeric type with 30 significant digits that can handle very large (or small) exponents like 10^(10^10).

    • Exponential notation: 6.022e23.

    • Hexadecimal, octal and binary number input:

      0xFFFF
      0b1011
      0o32
      0x2.F
      0o5p3
      
  • Physical units: parsing and handling, including metric prefixes:

    2 min + 30 s
    40 kg * 9.8 m/s^2 * 150 cm
    sin(30°)
    
    • Supported units: see Reference section below.

    • Implicit conversions: 15 km/h * 30 min evaluates to 7.5 km.

    • Useful error messages:

      > 2 watts + 4 newton meter
      
      Conversion error:
        Cannot convert unit N·m (base units: kg·m²·s⁻²)
                    to unit W (base units: kg·m²·s⁻³)
      
  • Explicit unit conversions: the -> conversion operator (aliases: , , to):

    60 mph -> m/s
    500 km/day -> km/h
    1 mrad -> degree
    52 weeks -> days
    5 in + 2 ft -> cm
    atan(30 cm / 2 m) -> degree
    6 Mbit/s * 1.5 h -> GB
    
  • Variable assignments:

    Example: mass of the earth

    r = 6000km
    vol = 4/3 * pi * r^3
    density = 5 g/cm^3
    vol * density -> kg
    

    Example: oscillation period of a pendulum

    len = 20 cm
    2pi*sqrt(len/g0) -> ms
    
    • Predefined constants (type list to see them all): speed of light (c), Planck's constant (h_bar), electron mass (electronMass), elementary charge (elementaryCharge), magnetic constant (µ0), electric constant (eps0), Bohr magneton (µ_B), Avogadro's constant (N_A), Boltzmann constant (k_B), gravitational acceleration (g0), ideal gas constant (R), ...

    • Last result: you can use ans (answer) or _ to refer to the result of the last calculation.

  • User-defined functions:

    Example: kinetic energy

    kineticEnergy(mass, speed) = 0.5 * mass * speed^2 -> kJ
    
    kineticEnergy(800 kg, 120 km/h)
    

    Example: barometric formula

    P0 = 1 atm
    T0 = fromCelsius(15)
    tempGradient = 0.65 K / 100 m
    
    pressure(height) = P0 * (1 - tempGradient * height / T0)^5.255 -> hPa
    
    pressure(1500 m)
    
  • Sums and products:

    Syntax:

    sum(<expression>, <index-variable>, <from>, <to>)
    product(<expression>, <index-variable>, <from>, <to>)
    

    Examples:

    # sum of the first ten squares
    sum(k^2, k, 1, 10)
    
    # the factorial of n as the product 1 × 2 × ... × n
    myFactorial(n) = product(k, k, 1, n)
    
  • Unicode support:

    λ = 2 × 300 µm
    ν = c/λ → GHz
    
  • And more: tab completion, command history (arrow keys, Ctrl+R), pretty printing, syntax highlighting, ...

Reference

Pros and cons

Reasons to use Insect

  • Insect is open source.
  • There is a web version that requires no installation.
  • With both browser and terminal versions available, insect is truly cross-platform.
  • Insect has first-class support for physical units, including metric and binary prefixes. While evaluating your calculation, Insect ensures that you did not accidentally make any mistakes in combining the physical quantities.
  • Insect supports an interactive style with its readline-based interface. There is a saved history that can be browsed by pressing up- and down keys. The history is also searchable via Ctrl-R.
  • The syntax of Insect is rather strict. The parser does not try to be "smart" on syntactically incorrect input, so there shouldn't be any surprises - and you can trust the result of your calculation. The parsed user input is always pretty-printed for a quick double-check.
  • Insect is written in PureScript and therefore benefits from all the safety-guarantees that a strictly-typed functional programming language gives you.
  • The source code of purescript-quantities (the underlying library for physical units) as well as the code of Insect itself is extensively tested.

Reasons to choose an alternative

  • Insect is a scientific calculator. It's not a computer algebra system that solves differential equations or computes integrals. Try WolframAlpha instead.
  • There is no graphical user interface with buttons for each action (, 1/x, DEG/RAD, etc.). Qalculate! is a fantastic tool that supports both text- as well as graphical input.
  • Insect supports a huge range of physical units: all SI units, all units that are accepted by SI as well as most units of the imperial and US customary systems (and many more). However, if you need something even more comprehensive, try GNU units.
  • Insect is not a general-purpose programming language. You could try Frink.
  • Insect does not have a special mode for hexadecimal or binary numbers (yet).

FAQ

  • Why are Celsius and Fahrenheit not supported?

    Compared to the SI unit Kelvin and in contrast to all other units, Celsius and Fahrenheit require an additive offset when converting into and from other temperature units. This additive offset leads to all kinds of ambiguities when performing calculations in these units. Adding two temperatures in Celsius, for example, is only meaningful if one of them is seen as an offset value (rather than an absolute temperature). Insect is primarily a scientific calculator (as opposed to a unit conversion tool) and therefore focuses on getting physical calculations right.

    Even though °C and °F are not supported as built-in units, there are helper functions to convert to and from Celsius (and Fahrenheit):

    • fromCelsius takes a scalar value that represents a temperature in Celsius and returns a corresponding temperature in Kelvin:

      > fromCelsius(0)
      
         = 273.15 K
      
      > k_B * fromCelsius(23) to meV
      
         = 25.5202 meV
      
    • toCelsius takes a temperature in Kelvin and returns a scalar value that represents the corresponding temperature in Celsius:

      > toCelsius(70 K)
      
         = -203.15
      
      > toCelsius(25 meV / k_B)
      
         = 16.963
      
  • Why is 1/2 x parsed as 1/(2x)?

    Implicit multiplication (without an explicit multiplication sign) has a higher precedence than division (see operator precedence rules). This is by design, in order to parse inputs like 50 cm / 2 m as (50 cm) / (2 m). If you meant ½ · x, write 1/2 * x.

  • What is the internal numerical precision?

    By default, Insect shows 6 significant digits in the result of the calculation. However, the internal numerical precision is much higher (30 digits).

  • How does the conversion operator work?

    The conversion operator -> attempts to convert the physical quantity on its left hand side to the unit of the expression on its right hand side. This means that you can write an arbitrary expression on the right hand side (but only the unit part will be extracted). For example:

    # simple unit conversion:
    > 120 km/h -> mph
    
      = 74.5645 mi/h
    
    # expression on the right hand side:
    > 120 m^3 -> km * m^2
    
      = 0.12 m²·km
    
    # convert x1 to the same unit as x2:
    > x1 = 50 km / h
    > x2 = 3 m/s -> x1
    
      x2 = 10.8 km/h
    
  • What is the relation between the units RPM, rad/s, deg/s and Hz?

    The unit RPM (revolutions per minute) is defined via 1 RPM = 1 / minute where the 1 on the right hand side symbolizes "1 revolution".

    As the base unit is the same (1 / second), RPM can be converted to rad / s, deg / s or Hz. Note, however, that 1 RPM does not equal 2π rad / min or 360° / min or 1 Hz, as some might expect. If you interested in computing the traversed angle of something that rotates with a given number of revolutions per minute, you need to multiply by 2π rad or 360 ° because:

    1 RPM · (360°/revolution) = (1 revolution / minute) · (360° / revolution) = 360° / minute
    

Terminal version

In addition to the web interface, there is also a command line version (supporting Node 10 and later) which can by installed via npm:

npm install -g insect

Note that this might fail if you run it with sudo. Instead, set up a prefix directory and call npm install as a user.

For Arch Linux, there is a package on AUR:

yaourt -S insect

For macOS, there is a Homebrew package:

brew install insect

For Android, install Termux from F-Droid. Install Node.js in Termux and then install insect.

pkg install nodejs-lts
npm install -g insect

Development

Insect is written in PureScript (see Getting Started guide). You can install all dependencies and build the whole project by running:

npm install
npm start

Open web/index.html in your browser.

Insect comes with a comprehensive set of unit tests. You can run them by calling

npm test

Note that Node 12 and above is required to work on/build Insect (despite Insect itself requiring only Node 10 or later to run). If you don't have or want to install Node 12 or later, you can use the following Dockerfile to build or run Insect on Node 18:

FROM node:18

WORKDIR /usr/src/insect

COPY . .

RUN npm install && \
    npm run build

CMD ["node", "index.cjs"]

After creating the image (docker build -t sharkdp/insect .), you can create the container and copy out the build artifacts:

docker create sharkdp/insect:latest
# copy SHA (e.g. 71f0797703e8)
docker cp 71f0797703e8:/usr/src/insect/index.cjs .
docker cp -r 71f0797703e8:/usr/src/insect/node_modules .

To directly run insect inside the Docker (paying a heavy startup time penalty), you can use

docker run -it --rm -v ~/.local/share/insect-history:/root/.local/share/insect-history sharkdp/insect:latest

Maintainers

More Repositories

1

bat

A cat(1) clone with wings.
Rust
48,695
star
2

fd

A simple, fast and user-friendly alternative to 'find'
Rust
33,228
star
3

hyperfine

A command-line benchmarking tool
Rust
18,256
star
4

hexyl

A command-line hex viewer
Rust
8,565
star
5

pastel

A command-line tool to generate, analyze, convert and manipulate colors
Rust
4,746
star
6

dbg-macro

A dbg(…) macro for C++
C++
2,719
star
7

cube-composer

A puzzle game inspired by functional programming
PureScript
1,967
star
8

vivid

A themeable LS_COLORS generator with a rich filetype datebase
Rust
1,479
star
9

shell-functools

Functional programming tools for the shell
Python
1,133
star
10

binocle

a graphical tool to visualize binary data
Rust
964
star
11

diskus

A minimal, fast alternative to 'du -sh'
Rust
936
star
12

numbat

A statically typed programming language for scientific computations with first class support for physical dimensions and units
Rust
734
star
13

great-puzzles

A curated list of great puzzles
484
star
14

purescript-flare

A special-purpose UI library for Purescript
PureScript
286
star
15

lscolors

A Rust library and tool to colorize paths using LS_COLORS
Rust
235
star
16

trigger

Run a user-defined command on file changes
Shell
181
star
17

bugreport

Rust library to collect system and environment information for bug reports
Rust
131
star
18

quizzity

A fast-paced geography quiz
JavaScript
125
star
19

curiosity

How far does your curiosity take you?
HTML
99
star
20

lucid

A simple mock-application for programs that work with child processes
Rust
86
star
21

parachuting-robots

An interactive version of a classic puzzle
PureScript
78
star
22

purescript-sparkle

Infer user interfaces from type signatures
PureScript
75
star
23

stack-inspector

A gdb command to inspect the size of objects on the stack
Python
62
star
24

painless

Painless parameter handling for easy exploration
C++
60
star
25

yinsh

A web-based version of the board game Yinsh
Haskell
59
star
26

purescript-isometric

Isometric rendering for PureScript
PureScript
58
star
27

pysh

Python-enhanced bash scripts
Python
58
star
28

purescript-quantities

Physical quantities and units
PureScript
53
star
29

minicpp

A REPL-style development environment for small C++ snippets
Shell
48
star
30

purescript-ctprelude

A Prelude with names from category theory
PureScript
48
star
31

qrnet

A neural network that decrypts QR codes
Python
43
star
32

riffle

A simple terminal pager (as a library)
Rust
41
star
33

config-files

A set of configuration files for vim, zsh, i3, ...
Python
37
star
34

content_inspector

Fast inspection of binary buffers to guess/determine the type of content
Rust
32
star
35

factorization-diagrams

Interactive factorization diagrams
HTML
27
star
36

argmax

Rust library to deal with "argument too long" errors
Rust
20
star
37

vacuum-robot-simulator

Simulation environment for an autonomous robot
Rust
19
star
38

sub

A command-line tool for string substitutions (RustLab 2019 workshop)
Rust
16
star
39

purescript-format

Type-safe, printf-style formatting for PureScript
PureScript
15
star
40

PaperLocator

Citation parsing to quickly access publications
JavaScript
14
star
41

purescript-decimals

Arbitrary precision numbers for PureScript
PureScript
13
star
42

flare-example

Quick start project for purescript-flare
PureScript
11
star
43

fd-benchmarks

Benchmarks for 'fd'
Shell
10
star
44

tinytag

tinytag is a "URL" shortener for your filesystem
Python
10
star
45

purescript-flaredoc

Interactive documentation with Flare
PureScript
8
star
46

phd-thesis

LaTeX sources for my PhD thesis
TeX
7
star
47

level11

7
star
48

GPE-Solver

Split-step solver for the 3D (dipolar) Gross-Pitaevskii equation
C++
7
star
49

Pack-Zack

A tag-based manager for packing lists
CSS
7
star
50

latex-report

A clean and simple LaTeX template for reports
TeX
7
star
51

dp

Personal website about some of my open source projects, board games and mountainbiking.
HTML
5
star
52

cs231n

My solutions to the CS231n assignments
Jupyter Notebook
4
star
53

imdb-ratings

Statistical analysis of my IMDB movie ratings
Jupyter Notebook
3
star
54

purescript-pairs

Datatype and functions for pairs of values
PureScript
3
star
55

zehntausend

A solver for the dice game Zehntausend
Haskell
3
star
56

gnuplot-mm

GnuPlot bindings for Mathematica
Mathematica
2
star
57

talk-moderne-programmiersprachen

JavaScript
2
star
58

rock-paper-scissors

Interactive regex puzzle
HTML
2
star
59

purescript-gametree

Algorithms for two-player zero-sum games
PureScript
2
star
60

snake-factory

A work-in-progress game with monadic parsers and snakes
PureScript
1
star
61

gomoku

Five in a row
PureScript
1
star