Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: command-line mathematics

  1. #1
    Join Date
    Apr 2006
    Beans
    313

    command-line mathematics

    I am curious if there is anything standard in the bash shell or the terminal that allows you to perform simple mathematics from the command line.

    I am currently writing some c programs that will allow me to evaluate equations using syntax like:

    Code:
    add 10 12.34
    22.34
    
    sub 30.20 16 4 
    10.20
    I am also creating modules for division, multiplication and percent finding.

    I guess I have a couple questions:

    Is there anything like this already available?
    Would there be interest in having this available?

    I have already compiled and am running the addition module, but I am pretty new to C programming, so if anyone is interested in looking at the source and giving some input, I can make it available.

    Thanx

  2. #2
    Join Date
    Apr 2006
    Location
    Canada
    Beans
    104

    Re: command-line mathematics

    Quote Originally Posted by munkyeetr View Post
    I am curious if there is anything standard in the bash shell or the terminal that allows you to perform simple mathematics from the command line.

    I am currently writing some c programs that will allow me to evaluate equations using syntax like:

    Code:
    add 10 12.34
    22.34
    
    sub 30.20 16 4 
    10.20
    I am also creating modules for division, multiplication and percent finding.

    I guess I have a couple questions:

    Is there anything like this already available?
    Would there be interest in having this available?

    I have already compiled and am running the addition module, but I am pretty new to C programming, so if anyone is interested in looking at the source and giving some input, I can make it available.

    Thanx
    if you're doing this for a learning exercise in c then good stuff, that's a neat idea.
    if you actually just want to be able to do simple arithmetic in bash though, the syntax is like so:
    echo $(( 5+4 ))

    good luck

  3. #3
    Join Date
    Apr 2007
    Beans
    6

    Re: command-line mathematics

    i use the dc command for quick arithmetic. uses reverse polish notation. there's also a command bc.
    Last edited by allyn; May 5th, 2007 at 07:16 PM.

  4. #4
    Join Date
    Apr 2006
    Beans
    313

    Re: command-line mathematics

    Nice. thank for that; echo works well.

    I like to screw around, so I'll probably keep writing the c modules, and may even write a wrapper script for echo to do math.

    Cheers.

  5. #5
    Join Date
    Apr 2006
    Location
    California
    Beans
    154
    Distro
    Ubuntu 7.10 Gutsy Gibbon

    Re: command-line mathematics

    Yea a little wrapper script would be nice, then you can just put it in your PATH and use 'add' and 'sub' commands like you mentioned above.

    something like this (for add or sub)
    Code:
    #!/bin/bash
    
    if [ $# -lt 1 ]; then
            echo "usage: $0 <value1> <value2> [<valueN>]"
            exit 0
    fi
    
    sum=0
    for arg in $@
    do
            sum=$(($sum+$arg))
    done
    
    echo $sum
    but this wont do decimals.. hmmm

  6. #6
    jfinkels is offline Iced Almond Soy Ubuntu, No Foam
    Join Date
    Nov 2006
    Beans
    1,187

    Re: command-line mathematics

    The interactive python interpreter does math pretty well.

  7. #7
    Join Date
    Sep 2005
    Location
    Denmark
    Beans
    202
    Distro
    Ubuntu Karmic Koala (testing)

    Re: command-line mathematics

    ~$ echo 10 + 12.34 | bc
    22.34

    Im not sure how to understand
    "sub 30.20 16 4
    10.20"

    does that mean "sub" takes arg1 and then subtracts all futher arguments (eg, arg2, arg3, etc.. )

    anyways

    ~$ echo 30.20 - 16 - 4 | bc
    10.20

    if you like to introduce:
    sum = arg1 + arg2 + arg3 + ... + argN
    and
    sub = arg1 - (arg2 + arg3 + ... + argN)


    then making with your favorite editor (vi of course)

    ~# vi /usr/local/bin/sum
    echo $@ | sed 's/ / + /g' | bc

    and

    ~# vi /usr/local/bin/sub
    echo $@ | sed 's/ / - /g' | bc
    --
    Søren Dideriksen | Ubuntu 9.10 | IBM T61p

  8. #8
    Join Date
    Sep 2006
    Location
    France.
    Beans
    Hidden!
    Distro
    Lubuntu Development Release

    Re: command-line mathematics

    Thread moved to "Programming Talk".
    | My old and mostly abandoned blog |
    Linux user #413984 ; Ubuntu user #178
    J'aime les fraises.
    Nighty night me lovelies!

    | Reinstalling Ubuntu ? Please check this bug first ! |
    | Using a ppa ? Please install ppa-purge from universe, you may need it should you want to revert packages back |
    | No support requests by PM, thanks |

  9. #9
    Join Date
    Jan 2007
    Beans
    235

    Re: command-line mathematics

    Quote Originally Posted by jfinkels View Post
    The interactive python interpreter does math pretty well.
    seconded. It's great for some quick calculations
    Disregard anything I post...

  10. #10
    Join Date
    Apr 2006
    Beans
    1,979
    Distro
    Ubuntu 8.10 Intrepid Ibex

    Re: command-line mathematics

    You could use the 'let' command:

    Code:
    let i=5+7 && echo $i

Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •