#!/bin/sh # The MIT License (MIT) # # Copyright © 2020-2025 pacman64 # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the “Software”), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. # ca [decimals...] [expression] # # # CAlculator is an easier-to-use version of `bc` (basic calculator) where # # - you give the expression as an argument, while `bc` reads it from stdin # - you don't need quoting when avoiding parentheses and spaces # - you can use either ** or ^ to raise powers # - you can use [ and ] or ( and ) interchangeably # - the number of decimals is 20 by default # - automatically includes the extended bc math library via option -l # - there are several extra predefined values and functions # handle help options case "$1" in -h|--h|-help|--help) awk '/^# +ca /, /^$/ { gsub(/^# ?/, ""); print }' "$0" exit 0 ;; esac # handle optional scale-override parameter scale=20 if [ $# -eq 2 ]; then scale="$1" shift fi # ensure output is all on 1 line, define several funcs and values, then # inject the expression given as this script's arguments, transformed # according to the rules described above { BC_LINE_LENGTH=0 bc -l << ENDOFSCRIPT scale = ${scale}; femto = 0.000000000000001; pico = 0.000000000001; nano = 0.000000001; micro = 0.000001; milli = 0.001; kilo = 1000; mega = 1000 * kilo; giga = 1000 * mega; tera = 1000 * giga; peta = 1000 * tera; exa = 1000 * peta; zetta = 1000 * exa; binkilo = 1024; binmega = 1024 * binkilo; bingiga = 1024 * binmega; bintera = 1024 * bingiga; binpeta = 1024 * bintera; binexa = 1024 * binpeta; binzetta = 1024 * binexa; kb = 1024; mb = 1024 * kb; gb = 1024 * mb; tb = 1024 * gb; pb = 1024 * tb; eb = 1024 * pb; zb = 1024 * eb; kib = 1024; mib = 1024 * kib; gib = 1024 * mib; tib = 1024 * gib; pib = 1024 * tib; zib = 1024 * pib; mol = 602214076000000000000000; mole = 602214076000000000000000; cup2l = 0.23658824; floz2l = 0.0295735295625; floz2ml = 29.5735295625; ft2m = 0.3048; gal2l = 3.785411784; in2cm = 2.54; lb2kg = 0.45359237; mi2km = 1.609344; mpg2kpl = 0.425143707; nmi2km = 1.852; oz2g = 28.349523125 psi2pa = 6894.757293168; ton2kg = 907.18474; yd2m = 0.9144; ga2l = gal2l; nm2km = nmi2km; tn2kg = ton2kg; hour = 3600; day = 24 * hour; week = 7 * day; hr = hour; wk = week; define ftin(f, i) { return (0.3048 * f + 0.0254 * i); } define lboz(l, o) { return (0.45359237 * l + 0.028349523 * o); } define eu() { return (e(1)); } define euler() { return (e(1)); } define pi() { return (4*a(1)); } define tau() { return (8*a(1)); } define deg(x) { return (180 * x / pi()); } define rad(x) { return (pi() * x / 180); } define abs(x) { if (x >= 0) return (x); return (-x); } define exp(x) { return (e(x)); } define j0(x) { return (j(0, x)); } define j1(x) { return (j(1, x)); } define ln(x) { return (l(x)); } define log(x) { return (l(x)); } define log2(x) { return (l(x) / l(2)); } define log10(x) { return (l(x) / l(10)); } define sin(x) { return (s(x)); } define cos(x) { return (c(x)); } define tan(x) { return (s(x) / c(x)); } define cot(x) { return (c(x) / s(x)); } define atan(x) { return (a(x)); } define sinh(x) { return ((e(x) - e(-x)) / 2); } define cosh(x) { return ((e(x) + e(-x)) / 2); } define tanh(x) { return ((e(x) - e(-x)) / (e(x) + e(-x))); } define coth(x) { return ((e(x) + e(-x)) / (e(x) - e(-x))); } define hypot(x, y) { return (sqrt(x*x + y*y)); } define sinc(x) { if (x == 0) return (1); return (s(x) / x); } define min(x, y) { if (x <= y) return (x); return (y); } define max(x, y) { if (x >= y) return (x); return (y); } define mod(x, y) { auto s, m; s = scale; scale = 0; m = x % y; scale = s; return (m); } define mod1(x) { return (mod(x, 1)); } define modf(x) { return (mod(x, 1)); } define round0(x) { auto i; i = x - mod(x, 1); if (x - i >= 0.5) { return (i + 1); } return (i); } define round(x, d) { auto k; k = 10^d; return (round0(x * k) / k); } define r(x, d) { return (round(x, d)); } define r0(x) { return (round0(x)); } define fac(x) { auto f, i; if (x < 0) return (0); f = 1; for (i = x; i >= 2; i--) { f *= i; } return (f); } define f(x) { return (fac(x)); } define fact(x) { return (fac(x)); } define factorial(x) { return (fac(x)); } define per(n, k) { auto p, i; if (n < k) return (0); p = 1; for (i = n; i >= n - k + 1; i--) { p *= i; } return (p); } define p(n, k) { return (per(n, k)); } define perm(n, k) { return (per(n, k)); } define com(n, k) { if (n < k) return (0); return (per(n, k) / fac(k)); } define comb(n, k) { return (com(n, k)); } define gcd(x, y) { return (x * y / lcm(x, y)); } define lcm(x, y) { auto a, b, z; /* the LCM is defined only for positive integers */ # if (mod(x, 1) != 0 || x < 1 || mod(y, 1) != 0 || y < 1) { return 0; } # if (mod(x, 1) != 0) return (0); if (x < 1) return (0); # if (mod(y, 1) != 0) return (0); if (y < 1) return (0); a = min(x, y); b = max(x, y); z = b; while (mod(z, a) != 0) { z += b; } return (z); } define sgn(x) { if (x > 0) return (1); if (x < 0) return (-1); return (0); } define sign(x) { return (sgn(x)); } define logistic(x) { return (1 / (1 + e(-x))); } $(echo "$@" | sed 's-^+--g; s-_--g; s-\*\*-^-g; s-\[-(-g; s-\]-)-g') ENDOFSCRIPT } | # ensure the result shows at least a zero before the decimal dot, then # rid the result of trailing zero decimals and/or trailing decimal dots sed -E 's-^\.-0.-; s/^-\./-0./; s-(\.[0-9]+[1-9]+)0+$-\1-; s-\.0*$--'