Tag Info

Hot answers tagged

144

There are lots of options!!! Summary $ echo $((20.0/7)) $ zcalc $ bc <<< 20+5/2 $ bc <<< 'scale=4;20+5/2' $ expr 20 + 5 $ calc 2 + 4 $ node -pe 20+5/2 # Uses the power of JavaScript, e.g. : node -pe 20+5/Math.PI $ echo 20 5 2 / + p | dc $ echo 4 k 20 5 2 / + p | dc $ perl -E "say 20+5/2" $ python -c "print 20+5/2" $ python -c "print ...


25

There are many ways to calculate. For simple expressions you can use bash itself: echo $((20+5)) or expr: expr 20 + 5 And for complex cases there is great tool bc: echo "20+5" | bc Btw, bc can calculate even very complex expression with roots, logarithms, cos, sin and so on.


18

The mentioned solutions are fine for very simple calculations, but very error-prone. Examples: # without spaces 20+5 literally produces 20+5 expr 20+5 → 20+5 # bc's result does't give the fractional part by default bc <<< 9.0/2.0 → 4 # expr does only integer expr 9 / 2 → 4 # same for echo echo $((9/2)) → 4 # echo chokes on floats echo ...


12

echo 'ibase=16;C06D1000-C06A5E78' | bc 176520 if you need answer in hex too (but note that only upper case hex digits are supported as lower case ones would conflict with function and variable names, which is why you got 0 in your example (var1 - var2)): echo 'obase=16;ibase=16;C06D1000-C06A5E78' | bc 2B188 ps. FYI scale isn't designed for conversion ...


11

You could use bc. E.g., $ echo "25 + 5" | bc 30 Alternatively bc <<< 25+5 will also work. Or interactively, if you want to do more than just a single simple calculation: $ bc bc 1.06.95 Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006 Free Software Foundation, Inc. This is free software with ABSOLUTELY NO WARRANTY. For details type `warranty'. ...


10

You can use calc: If you just enter calc with no other arguments it enters an interactive mode where you can just keep doing math. You exit this by typing exit: C-style arbitrary precision calculator (version 2.12.3.3) Calc is open software. For license details type: help copyright [Type "exit" to exit, or "help" for help.] ; 2+4 6 ; 3+5 8 ; 3.4+5 8.4 ; ...


9

I like to fire up Python and use it as an interactive calculator (but then again, I'm a Python programmer).


8

Nobody has mentioned awk yet? Using bash functions, and awk math power, just define this (one line) function: calc(){ awk "BEGIN { print "$*"}"; } Then just execute things like calc 1+1 or calc 5/2 Note: To make the function always available, add it to ~/.bashrc Of course, a little script named "calc" with the following contents: #!/bin/bash awk ...


7

The substring inside the ` ` must be a valid command itself: rownum=`echo $nextnum+1 | bc` But is preferable to use $( ) instead of ` `: rownum=$(echo $nextnum+1 | bc) But there is no need for bc, the shell is able to do integer arithmetic: rownum=$((nextnum+1)) Or even simpler in bash and ksh: ((rownum=nextnum+1))


7

bc supports the natural logarithm if invoked with the -l flag. You can calculate the base-10 or base-2 log with it: $ bc -l ... l(100)/l(10) 2.00000000000000000000 l(256)/l(2) 8.00000000000000000007 I don't think there's a built-in factorial, but that's easy enough to write yourself: $ bc ... define fact_rec (n) { if (n < 0) { print "oops"; ...


7

Since no-one else has mentioned it, and though it's not strictly a calculator (but neither are all these general-purpose scripting languages), I'd like to mention units: $ units "1 + 1" Definition: 2 $ units "1 lb" "kg" * 0.45359237 / 2.2046226 Or, for less output so you can get just the number to use in $() to assign to ...


6

$> ghc -e '20 + 5' 25 it :: Integer Also ghci, that is the Glasgow-Haskell Compiler in interactive mode (ghc --interactive, as opposed to it evaluating an expression with -e), makes for a fascinating "calculator": $>ghci GHCi, version 7.8.3: http://www.haskell.org/ghc/ :? for help Loading package ghc-prim ... linking ... done. Loading package ...


6

dc is a very archaic tool and somewhat older than bc. To quote the Wikipedia page: It is one of the oldest Unix utilities, predating even the invention of the C programming language; like other utilities of that vintage, it has a powerful set of features but an extremely terse syntax. The syntax is a reverse polish notation, which basically means that ...


5

It is interpreting C and A as units. (Coulombs and Amperes.) The button where you selected “Hexadecimal” only applies when displaying the result. It doesn't affect the way the expression that you enter is parsed. Try 0xC - 0xA.


3

There is no reliable way to implement that. The issue is the comma is used to separate arguments when more that one is passed to a function. If you aren't going to use such functions, I guess the simplest way would be something like: calc () { echo "scale=6;" "$@" | tr , . | bc -l | tr . , } That gives: $ calc 1 + 1 2 $ calc 1 / 3 ,333333 $ calc ...


3

I have found a solution. calc () { awk ' function asin(x) { return atan2(x, sqrt(1-x*x)) } function acos(x) { return atan2(sqrt(1-x*x), x) } function atan(x) { return atan2(x,1) } function tan(x) { return sin(x)/cos(x) } BEGIN { pi=atan(1)*4; print '"$(echo "$@" | tr , .)}" | tr . , } This one accepts numbers as 5,2 or 5.2 (i.e. ...


3

I use a little python script that will evaluate a python expression and print the result, then I can run something like $ pc '[i ** 2 for i in range(10)]' [0, 1, 4, 9, 16, 25, 36, 49, 64, 81] the script is: #!/usr/local/bin/python3 import sys import traceback from codeop import CommandCompiler compile = CommandCompiler() filename = "<input>" ...


2

For console calculations, I use concalc. (sudo aptitude install concalc) After that, just type concalc and hit enter. It won't supply a prompt, but just type in the your calculation (no spaces) and hit enter, and on the next line, it'll give you the numeric value.


2

There is TiLP. I've used it with my TI-84+ SE: it allows you to manage applications, programmes and variables, and IIRC it also has a "view screen" feature — either it has a screenshot function, or you can just use import or something else to grab a screenshot of TiLP showing the screen.


2

I can't believe to read "the power of JavaScript" (but I had to upvote the answer for the other parts, except perl of course. Practically, for the simple cases where integer arithmetic is sufficient, I use the buildin $((...)) and recommend it. Else, in almost all cases echo "..." | bc is sufficient. For some arithmetic operations like statistics, matrix ...


2

SQLite: echo 'select 1 + 1;' | sqlite3 MySQL: mysql -e 'select 1 + 1 from dual;' PostgreSQL: psql -c 'select 1 + 1 as sum;'


2

While not technically an answer to your question, you might want to consider moo(1) instead of bc(1).


2

You can control the scale that bc outputs with the scale=<#> argument. $ echo "scale=10; 5.1234 * 5.5678" | bc 28.52606652 $ echo "scale=5; 5.1234 * 5.5678" | bc 28.52606 Using your example: $ bc <<< 'scale=2; 1.5 * 1.5' 2.25 You can also use the -l switch (thanks to @manatwork) which will initialize the scale to 20 instead of the ...


2

dc is a calculator whereas bc is an actual language. See their man pages. dc dc is a reverse-polish desk calculator which supports unlimited precision arithmetic. It also allows you to define and call macros. Normally dc reads from the standard input; if any command arguments are given to it, they are filenames, and dc reads and executes the ...


2

You can also use built in arithmetic in bash rownum=$((nextnum+1)) which would be slightly faster


2

bash and GNU date #grab today's date in YYYYMMDD format today=$(date +%Y%m%d) #grab date as of 3 months ago in YYYYMMDD format three_months_ago=$(date +%Y%m%d --date='3 months ago') #now convert dates to "seconds since epoch" format, and then divide the difference by 60*60*24 to convert from seconds to days printf '%d\n' $(( ($(date --date=$today +%s) - \ ...


1

Try this : $ bc scale=10 1.5 * 1.5 2.25 Search bc's man for scale variable


1

You first need Ndless- Do you have it? If not, Google how to install it. Second, launch the LinuxLoader. Now load the INITRD and the KERNEL with the commands. After that, use BOOT Source: I did it myself on a TI Nspire CX.


1

Absolutely right and complete the suggested solutions, just to mention the way it has to be done in former times when only the Bourne-Shell was available, that's the way it likes it: rownum=`expr $nextnum + 1`


1

I would use (as was mentioned before) rownum=$((nextnum+1)) or ((rownum=nextnum+1)) but if you prefer an standard command you can use the let command, like let rownum=$nextnum+1



Only top voted, non community-wiki answers of a minimum length are eligible