**Monday 16. December 2019**: Working at EasyMile for nearly 3 years. Critical real-time software in C, simulation and monitoring in Haskell ➡️ perfect combo! It’s efficient and funny ;-)

And **we are recruiting!** Contact if you are interested in Haskell or embedded softwares (or both).

**Sunday 15. December 2019**: Playing with Pandoc filters in Haskell. abp should make pp obsolete.

me CDSoft CV/Resume Haskell Haskell Handy Calc abp pp Lua BonaLuna calculadoira Python TPG Vim todo pwd A.I. tools in Prolog dialog Free Free softwares Essays Oldies

18 April 2012

- Author
Christophe Delord

- Contact
- Web site
- Date
- License
This software is released under the GPL license.

- Download

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:

`-l filename`

loads a file and evaluates its content. This may be used to import some user libraries before entering an interactive session.

`expression`

evaluates an expression. The result is printed on the standard output.

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:

`num`

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`

`flt32`

,`flt64`

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`

`rat`

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`

`int`

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`

`int8`

,`int16`

,`int32`

,`int64`

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.

- Integer numbers
`42`

- Binary numbers
`b0110`

or`0110b`

- Octal numbers
`o27`

or`27o`

- Hexadecimal numbers
`h1F`

,`1Fh`

or`0x1F`

- Floating point numbers
`1.2e-3`

- Rational numbers
`157/50`

- Undefined numbers
`undef`

is a special value that makes everytinhg return`undef`

. It represents an undefined value.

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.

`pi`

3.14159265359

`e`

2.71828182846

`fact`

computes the factorial of a positive integer.

`fib`

computes the fibonnacci number of a positive integer.

`rev`

reverses bit order in int8 to int64 modes.

- Number-theoretic and representation functions
`ceil`

,`sign`

,`abs`

,`floor`

,`isinf`

,`isnan`

,`trunc`

- Power and logarithmic functions
`exp`

,`log`

,`log1p`

,`log10`

,`pow`

,`sqr`

,`sqrt`

- Trigonometric functions
`acos`

,`asin`

,`atan`

,`atan2`

`cos`

,`sin`

,`tan`

,`hypot`

- Angular conversion
`degrees`

,`radians`

- Hyperbolic functions
`acosh`

,`asinh`

,`atanh`

,`cosh`

,`sinh`

,`tanh`

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` |

`?`

shows some help

`help`

shows a more detailled help

`bye`

quits the interactive session

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:

- Win+c: starts
`calc.py`

in a console - Win+Alt+c: edits
`calc.ini`

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.