Skip to main content

"qcalc" is a powerful, cross-platform calculator specifically designed for embedded systems programmers. The calculator accepts whole expressions in the C-syntax and displays results simultaneously in decimal, hexadecimal, and binary without the need to explicitly convert the result to these bases.

General Requirements

The "qcalc" package requires Python 3, which is included in the QTools distribution for Windows and is typically included with other operating systems, such as Linux and MacOS.

Installation

The qcalc.py script can be used standalone, without any installation (see Using "qcalc" below).

Alternatively, you can install qcalc.py with pip from PyPi by executing the following command:

pip install qcalc

Or directly from the sources directory (e.g., /qp/qtools/qcalc):

python setup.py install --install-dir=/qp/qtools/qcalc

Using "qcalc"

If you are using qcalc as a standalone Python script, you invoke it from a console as follows:

python /path-to-qcalc-script/qcalc.py [expression]

Alternatively, if you've installed qcalc with pip, you invoke it from a console as follows:

qcalc [expression]

Batch mode

If you provide the optional [expression] argument, qcalc will evaluate the expression, print the result and terminate.

Interactive mode

Otherwise, if no [expression] argument is provided, qcalc will start in the interactive mode, where you can enter expressions via your keyboard.

Features

The most important feature of "qcalc" is that it accepts expressions in the C-syntax -- with the same operands and precedence rules as in the C or C++ source code. Among others, the expressions can contain all bit-wise operators (<<, >>, |, &, ^, ~) as well as mixed decimal, hexadecimal and binary constants. "qcalc" is also a powerful floating-point scientific calculator and supports all mathematical functions (sin(), cos(), tan(), exp(), ln(), ...). Some examples of acceptable expressions are:

  • ((0xBEEF << 16) | 1280) & ~0xFF -- binary operators, mixed hex and decimal numbers
  • ($1011 << 24) | (1280 >> 8) ^ 0xFFF0 -- mixed @ref qcalc_bin "binary", dec and hex numbers
  • (1234 % 55) + 4321//33 -- remainder, integer division (note the // integer division operator
  • pi/6 -- pi-constant
  • pow(sin(ans),2) + pow(cos(ans),2) -- scientific floating-point calculations, ans-variable

NOTE "qcalc" internally uses the Python command eval to evaluate the expressions. Please refer to the documentation of the Python math expressions for more details of supported syntax and features.

Automatic Conversion to Hexadecimal and Binary

If the result of expression evaluation is integer (as opposed to floating point), "qcalc" automatically displays the result in hexadecimal and binary formats (see "qcalc" screenshot above). For better readability the hex display shows an apostrophe between the two 16-bit half-words (e.g., 0xDEAD'BEEF). Similarly, the binary output shows an apostrophe between the four 8-bit bytes (e.g., 0b11011110'10101101'10111110'11101111).

Hexadecimal and Binary Numbers

As the extension to the C-syntax, QCalc supports both hexadecimal numbers and binary numbers. These numbers are represented as 0x... and0b..., respectively, and can be mixed into expressions. Here are a few examples of such expressions:

(0b0110011 << 14) & 0xDEADBEEF
(0b0010 | 0b10000) * 123

History of Inputs

As a console application "qcalc" "remembers" the history of the recently entered expressions. You can recall and navigate the history of previously entered expressions by pressing the "Up" / "Down" keys.

The ans Variable

"qcalc" stores the result of the last computation in the ans variable. Here are some examples of expressions with the ans variable:

  • 1/ans -- find the inverse of the last computation@n
  • log(ans)/log(2) -- find log-base-2 of the last computation@n

64-bit Range

"qcalc" supports the 64-bit range and switches to 64-bit arithmetic automatically when an integer result of a computation exceeds the 32-bit range. Here are some examples of the 64-bit output:

> 0xDEADBEEF << 27
= 501427843159293952 | 0x06F5'6DF7'7800'0000
= 0b00000110'11110101'01101101'11110111'01111000'00000000'00000000'00000000
> 0xDEADBEEF << 24
= 62678480394911744 | 0x00DE'ADBE'EF00'0000
= 0b00000000'11011110'10101101'10111110'11101111'00000000'00000000'00000000
> 0xDEADBEEF << 34
! out of range
>

Error Handling

Expressions that you enter into "qcalc" might have all kinds of errors: syntax errors, computation errors (e.g., division by zero), etc. In all these cases, "qcalc" responds with the Error message and the explanation of the error:

> (2*4) + )
Traceback (most recent call last):
  File "C:\qp\qtools\qcalc\qcalc.py", line 54, in _main
    result = eval(expr)
  File "", line 1
    (2*4) + )
            ^
SyntaxError: unmatched ')'
>

More Information

More information about "qcalc" is available online at:

More information about the QTools collection is available online at:

Metadata

Release files for qcalc 7.3.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for qcalc 7.3.3
File Size Uploaded
qcalc-7.3.3.tar.gz 5.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for qcalc 7.3.3
File Interpreter ABI Platform
qcalc-7.3.3-py3-none-any.whl Python 3 none any Details

Total release size: 11.8 kB

Release files / qcalc-7.3.3.tar.gz

Download URL qcalc-7.3.3.tar.gz
Size 5.9 kB
Tags Source
SHA-256 checksum
How to use checksums
362dcf7e434ab7cb90922a028bebf7f3ee780235cddc99c9fa462c555fe99bd3
BLAKE2b-256 checksum
How to use checksums
9e194544f9cbe50e9bd32f9dce74d8d894cd7357361d702b417d2db4973dd0e0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.12.1

Release files / qcalc-7.3.3-py3-none-any.whl

Download URL qcalc-7.3.3-py3-none-any.whl
Size 5.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
65b3246a5914e3efcc7f06aec8f7561125cfe68725ea309498607aaf15c4e280
BLAKE2b-256 checksum
How to use checksums
3827dcb6e4d586a8d08123fda9e76d74d7e55c15c3fe495260f06a01d4d16326
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.12.1

Release history Release notifications | RSS feed

This release

7.3.3 This release

2 release files

7.3.2

2 release files

7.0.0

2 release files

6.9.4

2 release files

6.9.3

2 release files

6.9.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page