[main]  

calc

For many years, this program was a one-liner to call perl, until support for binary, octal, decimal and hexadecimal conversion and the help text with examples were added.

Documentation

Description

    Small, simple command line based calculator with history (provided 
    by bash), complex expression evaluation (provided by perl)
    and optional bin/oct/dec/hex formatting (provided by perl).

Usage

    calc [ bin | oct | dec | hex ] EXPRESSION

Notes

  1 Precision is higher if no (bin/oct/dec/hex) formatting is used. 
    Also, formatting implies non-fractional output.

  2 The 'x' character is an alternative for '*' (asterisk) 
    to do a multiplication. The advantage over using '*' is that 
    no escaping is required in a shell like 'bash'
    - Note that escaping parentheses '(' and ')' will be still 
      required.

  3 The Perl interpreter is used to do actual calculation, 
    available operators are described in 'man perlop'. 

Examples

    Normal calculation: quoting is used to avoid interpretation 
    of parentheses by the shell: 
      $ calc '(2.4 + 1) x 3'
          10.2 = (2.4 + 1) * 3

    Add decimal 3 and hexadecimal 0x10 and octal 010 and binary 0b10, 
    and output result in binary format:
      $ calc bin 3 + 0x10 + 010 + 0b10
          0b11101 = 3 + 0x10 + 010 + 0b10
    All in decimals this is: 
      $ calc dec 3 + 16 + 8 + 2
          29 = 3 + 16 + 8 + 2

    Note the difference between with or without formatting argument:
      $ calc dec 12345678 x 12.12
          149629617 = 12345678 * 12.12
      $ calc  12345678 x 12.12
          149629617.36 = 12345678 * 12.12

    Simple conversions:
      $ calc hex 65535
          0xffff = 65535
      $ calc dec 0xffff
          65535 = 0xffff

Prerequisites

    - bash (version 3.1.16(1) used for testing, but any should work)
    - perl (version v5.8.8 used for testing, but any >=5 should work)

See Also

    man perlop

Related

    - expr, man expr (may help out if 'perl' is not available) 
    - GUI-based calculators: xcalc, kcalc (just to name a few)
    - other command line based calculators 
    - Gentoo-Linux category sci-calculators

Version

    0.04.0001 (2007-03-30)

Source Code

License: The code is in the public domain.There is no warranty, and no liability is assumed.
  [main]  
DaicasWeb v.1.50.0102  //   Daniel Käps  //   March 30, 2007  //   Impressum / Imprint 
http://www.daicas.net/programming/calc.html