| Author: | Christophe Delord |
|---|---|
| Web site: | http://www.cdsoft.fr/calc |
| Date: | Wednesday 15 May 2013 |
| License: | This software is released under the GPL license. |
| Download: | http://www.cdsoft.fr/calc/calc-v1.2.0.tgz |
Table of Contents
This software started as a Simple Parser example. As it is growing bigger and bigger, it is now packaged apart.
Calc is a calculator designed for my own programmer's needs, especially dealing with floating point numbers, their IEEE representation and binary and hexadecimal calculus.
Calc requires Python 2.6 or later (Python 3.1 is also supported).
Calc accepts some command line parameters:
If Calc runs in a terminal and no command is entered on the command line, Calc enter an interactive session. The user can type in some expressions and read the result in the terminal:
$ calc.py +---------------------------------------------------------------------+ | C A L C | |---------------------------------------------------------------------| | Modes: | Numbers: | | num flt32 flt64 rat | binary: b... or ...b | | int int8 int16 int32 int64 | octal : o... or ...o | |---------------------------------| hexa : h... or ...h or 0x... | | Variable and function: | float : 1.2e-3 | | variable = expression |-----------------------------------| | function(x, y) = expression | Operators: | |---------------------------------| or xor and not | | Builtin functions: | < <= > >= == != | | fact, fib, rev, ... | cond?expr:expr | | type help to list all functions | + - | ^ * / % & >> << ~ ** | |---------------------------------------------------------------------| | Commands: ? help bye | http://www.cdsoft.fr/calc | +---------------------------------------------------------------------+ (num) fact(42) = 1405006117752879898543142606244511569936384000000000 (num)
If some text is available in the standard input it is evaluated:
$ echo "fact(10)" | calc.py (num) fact(10) = 3628800
Calc can use several numeric modes:
this mode uses the Python generic numbers. Integers are unlimited and floating point is used when necessary:
(int) num = Number mode (num) 10**42 = 1000000000000000000000000000000000000000000 (num) 1/2 = 0.5
these modes use Python floats on 32 or 64 bits. The IEEE representation of the results is also displayed:
(num) flt32
= 32 bit Float mode
(flt32) cos(pi/4)
= 0.707388269167
ieee: 0x3F351766
this mode uses the Python rational numbers. It is useful in some cases to perform exact computations:
(flt32) rat = Rational mode (rat) 0.1*3 + 1/4 = 11/20
this mode uses Python integers. As for num, integers are unlimited. Every intermediate computations are made using integers (i.e. are truncated):
(rat) int = Integer mode (int) 10/3 = 3 (int) 10**42 = 1 000 000 000 000 000 000 000 000 000 000 000 000 000 000
these modes use Python integers but every intermediate computations are made using 8, 16, 32 or 64 bit integers. For 32 and 64 bit mode, the corresponding IEEE floating point is also displayed:
(int) int32
= 32 bit Integer mode
(int32) 0xFF << 24 | 128
= 4 278 190 208
hex: FF00 0080
bin: 1111 1111 0000 0000 0000 0000 1000 0000
flt: -1.70143779609e+38
(int32) 0x40100000
= 1 074 790 400
hex: 4010 0000
bin: 0100 0000 0001 0000 0000 0000 0000 0000
flt: 2.25
Type num, flt32, flt64, rat, int, int8, int16, int32 or int64 to change the current mode.
Calc can use floating point, integer, binary, octal, hexadecimal and rational numbers.
Logical operators deal with boolean expressions. Booleans are numbers. Null values are false, other values are true.
Logical operators are or, xor, and, not.
or and and use short-circuit evaluation (the second operand is evaluated only when necessary):
(num) 3 or 4 = 3 (num) 3 and 4 = 4 (num) 1 or 0/0 = 1 (num) 0 or 0/0 ZeroDivisionError: int division or modulo by zero
Relational operators are used to compare numbers.
Relational operators are <, <=, >, >=, ==, !=.
The ternary operator (... ? ... : ...) is also available. If the first expression is not null, it evaluates the second. Otherwise the third one is evaluated:
(num) (2 == 1+1) ? 42 : 0/0 = 42 (num) (2 > 1+1) ? 0/0 : 42 = 42
Calc can manipulate bits with bitwise operators (|, ^, &, ~):
(int8) 1 | 2
= 3
hex: 03
oct: 03
bin: 0000 0011
(int8) 3 ^ 1
= 2
hex: 02
oct: 02
bin: 0000 0010
(int8) 3 & 1
= 1
hex: 01
oct: 01
bin: 0000 0001
(int8) ~3
= 252
hex: FC
oct: 374
bin: 1111 1100
Bit shift operators (<< and >>) left and right shift bits:
(int16) 42 << 8
= 10 752
hex: 2A00
bin: 0010 1010 0000 0000
(int16) 0xFF00 >> 4
= 4 080
hex: 0FF0
bin: 0000 1111 1111 0000
Finally, classical arithmetic operators are implemented. These operators are +, -, *, /, % (modulo), ** (exponentiation), ! (factorial) and |...| (absolute value).
Variables and functions can be defined using the equality operator:
variable = expression
or:
function(x,y) = expression containing x and y and global variables
Names found in expressions are considered as either local parameters or global variables.
This example defines a constant and the factorial function:
(num) fact_of_0 = 1 = 1 (num) fact(n) = (n==0) ? fact_of_0 : n*fact(n-1) = fact(n) (num) fact(5) = 120
Note
Expressions defining variables and functions are stored uncomputed. This means that when the mode is changed, the value of a variable is recomputed and may be different. pi defined as pi = 3.14 is 3.14 in num mode, 3 in int mode and 157/50 in rat mode.
For further information, read the documentation of the math module.
From highest to lowest precedence:
| Operator family | Syntax |
|---|---|
| Precedence overloading | (...) |
| Absolute value | |...| |
| Function evaluation | f(...) |
| Factorial | x! |
| Exponentiation | x**y |
| Unary operators | +x, -y, ~z |
| Multiplicative operators | * / % & << >> |
| Additive operators | + - | ^ |
| Ternary operator | x ? y : z |
| Relational operators | < <= > >= == != |
| Logical not | not x |
| Logical and | and |
| Logical or | or xor |
| Assignement | x = y |
Calc is intended to run in a terminal. It works everywhere Python runs.
Just execute calc.py. sp.py shall be in the same directory.
Calc also contains a standalone executable for Windows. calc.exe contains calc.py, sp.py and a Python interpretor. It is packaged as a single executable with AutoHotkey. When calc.exe is run for the first time, it is unpacked (it may be slow). Then it uses AutoHotkey to add two shortcuts:
I'm not a Windows user but I need my calculator at work.
Not everybody has yet understood that some OS are definitely not made for software engineers. So I'm using a small but powerful MinGW/MSYS environment and Calc in a Windows console at work.
Support
If you find these softwares useful, you are free to donate something to support their future evolutions. Thanks for your support.
You can use Flattr or PayPal to support these softwares.
| Flattr | PayPal |
|---|---|