Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!

Console Calculators

Aug
2,831
149
In another thread,
@David Marcus noted the console calculator that he uses,
Console Calculator (CCalc) by ZoeSoft.

Ref: Console Calculator (CCalc) – ZoeSoft
Ref: start /wait not working in windows 10 in a batch file

I thought that we could share the console calculator that we each use.

I use two console calculators from TCC.

The first is CMath from the @Charles Dye EditKeys Plugin.
Code:
CMath — Evaluate a math expression at the command line.

To evaluate a math expression, type a colon followed by the expression. Then press Enter:

:1+2*3


The answer, 7, will be displayed below your expression. 
It will also be saved to the environment variable ANS. 
You can use this variable in your next expression:

:ans*2


Just remember that the value of ANS changes every time you use CMath. 
After the second line, ANS will be 14, not 7.


CMath calls the same function that @EVAL uses internally. 
You can use any operator or function that is legal in @EVAL:

:log(100)


set r=4:pi*r**2


:1492 =x

Another console calculator that I use is Qalculate!

QALC man page
Main Page: Qalculate! - the ultimate desktop calculator

Qalculate is also available as a GUI app.

Qalculate can be installed using the Microsoft console WinGet command.

Joe
 
While not TCC related, I've written an open source command-line RPN calculator (reverse polish notation) that's gotten a lot of use. If you are interested, check it out. It's java so you'll need a JRE installed. It's just one executable .Jar file that you run with "java -jar RPNCalc.jar"

Of course I have an a TCC executable extension setup for .jar...

If you try it, let me know what you think or if you have any suggestions.

RPNCalc

RPNCalc User Guide

Here is the help output
Code:
+--------------------------------------------------------------------------------+
+                            RPN Calculator  v5.10.9                            +
+                    Copyright (C) 2011-2025 by Michael Fross                    +
+--------------------------------------------------------------------------------+
         RPNCalc is the command line Reverse Polish Notation calculator         
                       https://github.com/frossm/rpncalc                       
                   https://frossm.github.io/RPNCalc-UserGuide                   

Command Line Options:
 -D       Start in debug mode. Same as using the 'debug' command
 -l       Load a saved named stack or create the stack if it doesn't exist
 -h | ?   Show this help information
 -v       Display version information as well as latest GitHub release
 -L       Display the program usage license. Same as the 'license' command
 -z       Disable colorized output

Operands:
 +    Addition:  Add last two stack elements
 -    Subtraction:  Subtract row 1 from row 2
 *    Multiplication:  Multiply last two stack items
 /    Division:  Divide line2 by line1
 ^    Power:  Calculate line2 to the power of line1

Calculator Commands:
 aa [keep]      Add all stack items. Adding 'keep' will retain existing items
 abs            Take the absolute value of line1
 avg [keep]     Replace stack items with average of values. 'keep' will retain stack
 c              Clear the screen and empty current stack
 cl[ean]        Clear screen and keep the stack values
 copy [#]       Copy line1 or the provided line number and add it to the stack
 d [#] [#-#]    Delete line1, the line number provided, or a range of lines provided
 dice XdY       Roll a Y sided die X times. Default is 1d6
 down           Shift the stack down moving the Line1 to the bottom of the stack
 echo message   Display the provided message prior to the next stack display
 fact           Take a factorial of line1. Decimals will be dropped
 f              Flip the sign of the element at line1
 int            Convert line1 to an integer. No rounding is performed
 lr [add] [x]   Linear Regression. 'add' puts next value on stk. 'x' shows y value at that x
 log | log10    Calculate the natural (base e) or base10 logarithm
 min | max      Add the minimum or maximum stack value to the stack
 median [keep]  Replace stack with median value. 'keep' will retain stack
 mod            Modulus. Perform a division and return the remainder
 sort a|d       Sorts the stack in an ascending or descending manner
 sqrt           Perform a square root of the line1 value
 rand [L] [H]   Random integer between L and H inclusive.  Default is 1-100
 repeat [Num]   Repeat the last command Num times.  Default is 1
 round [n]      Round to n decimal places. Default is 0 decimals
 s [#] [#]      Swap the last two elements in the stack or the lines provided
 sd [keep]      Standard deviation of stack items. 'keep' will retain stack
 u [STACK #]    Undo last action or back to the undo stack defined in 'list undo'
 up             Shift the stack up. Line1 becomes Line2, last stack items becomes Line1

Conversions:
 to%            Convert line1 from a number to a percent (0.715 -> 71.5%)
 from%          Convert line1 from a percent to a number (23.5% -> 0.235)
 frac [base]    Display as a fraction with min provided base. Default base is 64th
 deg2rad        Convert line1 from degrees to radians
 rad2deg        Convert line1 from radians to degrees
 in2mm          Convert line1 from inches into millimeters
 mm2in          Convert line1 from millimeters to inches
 in2ft          Convert line1 from inches into feet
 ft2in          Convert line1 from feet to inches
 gram2oz        Convert line1 from grams to US ounces
 oz2gram        Convert line1 from US ounces to grams
 kg2lb          Convert line1 from kilograms to US pounds
 lb2kg          Convert line1 from US pounds to kilograms

Trigonometry Functions:
 sin|cos|tan [rad]    Trig Functions: Angle is in degrees unless rad is provided
 asin|acos|atan [rad] Trig Functions: Result is in degrees unless rad is provided
 hypot                Returns the hypotenuse length using line1 and line2 as the legs

Memory Commands:
 mem [X] add       Add line1 to memory slot X. Default slot is 0
 mem [X] clr       Clear memory from slot X. Default slot0
 mem [X] copy      Copy number from memory slot X. Default slot is 0
 mem addall        Add all stack items to memory slots
 mem clearall      Clear all memory slots
 mem copyall       Copy all memory items onto the stack

Constants:
 eulersnum         Add Euler's exponential growth number (e)
 eulersconst       Add Euler's mathematical constant (y)
 phi               Add the Golden Ratio (phi)
 pi                Add Archimede's constant (PI)
 sol               Add the Speed of Light (c)

User Defined Functions:
 func del NAME     Delete named user defined function
 func delall       Delete all user defined functions
 func export       Export the currently defined functions to a file
 func import       Import functions from a file and append them to current ones
 record on         Turn on command recording
 record off        Disable recording

Configuration Options:
 reset             Resets configurations set with 'set' back to defaults
 set               Display current values of configurations you can set
 set align l|d|r   Set alignment of the stack display to 'l'eft, 'd'ecimal, or 'r'ight
 set browser FILE  Set the full path (using slashes) to the web browser
 set memslots NUM  Set the number of memory slots
 set width NUM     Set the width of the display to num

Operational Commands:
 debug             Toggle DEBUG mode on/off
 h|?               Show this help information.  Either key will work
 export FILE       Export the current stack to the provided file (one number per line)
 hp | homepage     Open up the configured browser to the RPNCalc Home Page
 import FILE       Replace current stack with file contents (one number per line)
 list func         Display the detailed user defined functions
 list mem          Display contents of the memory slots
 list stacks       Show the list of saved stacks
 list undo         Show the current undo stack
 list commands     Show the command history
 load NAME         Load (or create if needed) a named stack
 rev               Reverse the order of the items in the stack
 ss                Swap primary and secondary stack
 ug | userguide    Open up the configured browser to the RPNCalc User Guide
 license           Display the software usage license
 ver               Display current RPNCalc version & latest version on GitHub
 x | exit          Exit Calculator
 cx| clearexit     Clear the stack and then exit
 
Last edited:
Hi, I wrote a 100% console calculator that I use in Linux, Cywin, CMD, and TCC. I call it "exp", as it's one character shorter than "expr".

This is the help menu invoked by "exp --help"
Code:
exp - a high-precision command-line calculator (v2.12)

Syntax:   exp [-options] MATH_EXPRESSION
Options:
   -c     commify (add commas every 3 digits and wrap long lines)
   -C     also copy the answer to the Windows clipboard
   -h     issue this help message
   -p N   change precision to N decimal places (default: 5)
   -w N   change output width in wrapping long lines (default: 80)

Operators:
    ^      exponentiation             /    division
  * or x   multiplication             %    modulus (remainder)
  + or -   add, subtract             sqrt  square root

Operator precedence:   (...)   ^   * x   /   %   +   -   sqrt(N)

Requires a math statement on the command line that can be parsed
by "bc". Use quotes or backslashes if "*" or parens "(..)" occur
on the command line. Option switches to exp must appear before
the math expression. Commas in the expression are OK (ignored).
Remainder (modulus) is shown by "%", e.g., "19%4" returns 3.

Examples:
  exp 2^16                        # prints 65536
  exp -p 10 355 / 113             # prints 3.1415929203, better pi
  exp '123,456.7890123 * 99,999'  # prints 12345555444.4409877
  exp -c '123456.7890123 x 99999' # prints 12,345,555,444.4409877
  exp -c 5^5^6                    # prints 183 lines of data
  exp 'sqrt (41.5^2 + 12^2)'      # parens required for sqrt(n)

If running under CMD or TCC, the caret (^) is a quote character, so it
must be doubled (^^) to be seen as a single char, even if `backticks`
are used. Use "double quotes" to hide the caret.
  exp "sqrt (41.5^2 + 12^2)"      # double quote marks work best
  exp 'sqrt (41.5^^2 + 12^^2)'    # use 2 carets with backticks

The source code is on github at https:||github.com/epement/scripts/blob/main/bash/exp

As you can see "exp" is essentially a bash script. In my TCC setup, I have a file called "aliases.ini" where I keep all my aliases.

One of the lines reads like this:

exp %UNIXGIT%\usr\bin\bash.exe c:\tools\Linux\HOME-bin\exp %$

So when I run "exp" with a command tail (%$), it first invokes bash in the folder which contains Git for Windows, passing two parameters.

I don't often need to do high-precision calculations with exponents, but if I do, this is my tool that works well:

[ 2:37] > exp -c 86426^^120
24,964,886,420,497,693,103,940,377,873,887,859,803,447,154,574,681,968,050,571,
040,496,414,026,895,870,757,568,546,938,775,823,218,720,571,433,906,204,933,889,
692,361,106,372,039,050,187,993,026,878,092,659,951,738,139,169,423,172,478,120,
221,513,661,002,091,589,177,700,910,637,961,337,141,601,319,163,143,123,215,126,
778,222,204,738,270,404,283,604,338,483,410,989,923,647,312,039,440,392,048,290,
052,397,579,346,544,823,823,784,256,850,970,438,605,186,416,222,103,091,481,844,
596,309,700,721,495,626,045,932,233,164,094,253,706,581,553,612,493,715,002,135,
159,204,265,860,985,319,353,877,669,383,146,448,499,985,469,754,367,398,880,161,
511,534,435,998,704,837,440,781,765,608,191,203,268,054,491,113,996,425,064,759,
901,164,439,616,938,938,773,063,975,569,860,534,533,123,427,455,205,376


Hope someone finds it helpful!
 
I don't often need to do high-precision calculations with exponents, but if I do, this is my tool that works well:

[ 2:37] > exp -c 86426^^120
24,964,886,420,497,693,103,940,377,873,887,859,803,447,154,574,681,968,050,571,
040,496,414,026,895,870,757,568,546,938,775,823,218,720,571,433,906,204,933,889,
692,361,106,372,039,050,187,993,026,878,092,659,951,738,139,169,423,172,478,120,
221,513,661,002,091,589,177,700,910,637,961,337,141,601,319,163,143,123,215,126,
778,222,204,738,270,404,283,604,338,483,410,989,923,647,312,039,440,392,048,290,
052,397,579,346,544,823,823,784,256,850,970,438,605,186,416,222,103,091,481,844,
596,309,700,721,495,626,045,932,233,164,094,253,706,581,553,612,493,715,002,135,
159,204,265,860,985,319,353,877,669,383,146,448,499,985,469,754,367,398,880,161,
511,534,435,998,704,837,440,781,765,608,191,203,268,054,491,113,996,425,064,759,
901,164,439,616,938,938,773,063,975,569,860,534,533,123,427,455,205,376

You have to work a bit to do that with @EVAL. @EVAL is a little odd when it comes to exponentiation. I think this UDF (for integers only) is pretty robust.

Code:
d:\data\tcchistory> function ipow
%@eval[%1**%2=0.%@eval[%2*ceil(log10(%1))]]

d:\data\tcchistory> echo %@ipow[86426,120]
24964886420497693103940377873887859803447154574681968050571
040496414026895870757568546938775823218720571433906204933889
692361106372039050187993026878092659951738139169423172478120
221513661002091589177700910637961337141601319163143123215126
778222204738270404283604338483410989923647312039440392048290
052397579346544823823784256850970438605186416222103091481844
596309700721495626045932233164094253706581553612493715002135
159204265860985319353877669383146448499985469754367398880161
511534435998704837440781765608191203268054491113996425064759
901164439616938938773063975569860534533123427455205376
 
That's why all my libs are open source.
I don't like the thought of my work dying with me.

I'm not sure I understand your comment. My "exp" script (which does generic math calculations, not just exponents) relies on bc (the binary calculator) for its math calculations and uses perl for its formatting. I use the cygwin compilation of 'bc' under TCC. I do not need the cygwin dll's nor do I need Cygwin running to use bc.exe.

bc is open source, with a GNU free software license. You can get a compiled version with (or without) the source code from Cygwin's install tool, or you can download the source from the GNU project at https:⫽www.gnu.org/software/bc/

Note that according to the GNU page cited above, "bc is currently maintained by Phil Nelson."
 
Back
Top