calc(3)calc - Calculator
insert-calc - Calculator with answer insertion
calc-op - Low level calculator command
calc "string"
insert-calc "string"
calc-op "string"
calc can perform floating point, simple integer and logic based calculations given by "string", where the "string" takes the following form:-
"[b]<s>"
Where 'b' is an optional letter setting the required output base/format which can be one of the following:
f - Floating point
b - Binary
o - Octal
d - Decimal
x - Hexadecimal
Default when omitted is a hybrid of 'f' (floating point) and 'd' (decimal) whereby the calculation is performed using both floating point and integer maths and if the results differ then both are returned (floating point first with integer in brackets after). "s" is the sum to be calculated, which typically takes the form of a number followed by 1 or more operator & number pairs.
Calc supports numbers in the following formats:
0b## - Binary number where # can be 0-1
0## - Octal number where # can be 0-7
## - Decimal number where # can be 0-9, note it should not be prefixed with a 0
0x## - Hexadecimal number where # can be 0-9, A-F or a-f
#.# - Floating point number where # can be 0-9
#e# - Floating point number with exponent where # can be 0-9
#.#e# - Floating point number with exponent where # can be 0-9
Ans - Last calculation answer (case insensitive)
$xxx - MicroEmacs system variable
%xxx - MicroEmacs global variable
.xxx - MicroEmacs command variable
"xx" - String value (for logical evaluations)
Following is a list of supported operators in order of precedence (which is in compliance with the C programming language):
(..) - Parentheses (contents calculated first)
~ ! - Bitwise-not, Logical-not (number prefix)
+ - - Positive/Negative number (number prefix)
* / % - Multiply, Divide, Modulus
+ - - Addition, Subtraction
<< >> - Bitwise rotate left/right
< > <= >= - Logical Less-than, Greater-than, Less-or-equal, Greater-or-equal
== != - Logical Equals, Not-equals
& - Bitwise and
^ - Bitwise xor
| - Bitwise or
&& - Logical and
|| - Logical or
The answer of the last successful calculation is stored in .calc.answer(5), "Ans" in the following sum is replaced with this value. The result of the each calculation is stored in .calc.result(5), this can be the same as .calc.answer, the hybrid values when floating point and integer maths produce different answers and the required format is not specified, or, if there is an error in the string a MicroEmacs list with the first element being the character offset and the second being the error message.
The argument n is a bitwise flag where:
0x01
0x02
When omitted the default argument is 1.
insert-calc is a simple macro for executing calc with flag 2 set of the numerical argument so the answer is inserted.
calc-op is the low-level macro used by calc to perform the calculation, it is similar to executing calc with a numeric argument of 0 except that even errors are not printed to the message-line. This macro can be used by other macros to silently perform any computation, retrieve the result from variable .calc.answer(5).
To calculate the number of hours in a year:
calc "365*24"
To then calculate the number of seconds in the year:
calc "Ans*60*60"
calc is a macro defined in calc.emf.
The command throws an error if an operator is given an incompatible argument, for example a floating point number cannot be directly given to a bitwise or boolean logic operator such as >> or ||, it can be given to a comparison operator, such as >=, the result of which is an integer boolean value (1 or 0) which can then be used by logical operators.
calc-op is used by c-hash-eval(3) to evaluate the state of any #if and #ifdef lines.
(c) Copyright JASSPA 2025
Last Modified: 2024/05/08
Generated On: 2025/09/29