+ Reply to Thread
Results 1 to 7 of 7

Thread: Using bc (basic calculator)

  1. #1
    Join Date
    Sep 2009
    Location
    USA
    Posts
    3,400
    Blog Entries
    5
    Rep Power
    37

    Using bc (basic calculator)

    bc is a basic calculator in Linux. Even though it is very simple, it is also very powerful because it allows for arbitrary precision arithmetic.
    This tutorial is interactive, you are encouraged to type everything in while reading it!

    To start bc, type this into the shell:
    Code:
    bc
    Yes, it is that easy. You should receive a message like this:
    Code:
    bc 1.06.94
    Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006 Free Software Foundation, Inc.
    This is free software with ABSOLUTELY NO WARRANTY.
    For details type `warranty'.
    Now you can type in simple calculations. Try some of these:
    Code:
    1+1
    6*7
    8^3
    1+2*3
    (1+3)*3
    2/3
    You output should be like this:
    Code:
    2
    42
    512
    7
    12
    0
    bc does order of operations, just like you would expect.
    Everything looks fine, except 2/3 equals 0. Why did this happen?
    By default bc does not deal with decimal places. If you want bc to show decimal places, you need to set the scale. Type this into bc:
    Code:
    scale=10
    scale is a variable in bc. When you type that into bc, you make scale equal to ten. Now type this:
    Code:
    scale
    bc should output 10, because that is what scale equals now. Try this:
    Code:
    2/3
    Now you should get this as an output:
    Code:
    .6666666666
    bc calculated 2/3 correctly to ten decimal places.
    Now type this in:
    Code:
    sqrt(2)
    bc should output the square root of 2 on your computer:
    Code:
    1.4142135623
    sqrt() is a function in bc.
    Now quit bc as follows:
    Code:
    quit
    Now we will execute bc again, but with two flags.
    Code:
    bc -q -l
    The -q makes bc quiet, so you should get no welcome message.
    The -l starts bc with a math library, which gives you extra functions to work with. Try this again:
    Code:
    sqrt(2)
    bc should now compute the square root of 2 to 20 decimal places:
    Code:
    1.41421356237309504880
    When you start bc with -l it automatically sets the scale to 20.
    bc also has a few new functions. Try this:
    Code:
    e(1)
    bc just calculated Euler's Number to the first power.
    Code:
    2.71828182845904523536
    Type this in:
    Code:
    s(45)
    bc calculates the sine of 45 in radians.
    Code:
    .85090352453411842486
    To calculate cosine use c(), and to calculate arctangent use a()

    Now you should know how to use bc for some everyday calculations. What do we do now? If you have programming experience, programming in bc should be very easy. If you don't program, you don't need to read this section.
    While loops are like this:
    Code:
    while (condition) {
    calculations
    }
    Conditions are just like in other programming languages
    >, <, <=, >=, ==, !=, etc. will all work in bc
    If conditions are like this:
    Code:
    if (condition) {
    calculations
    } else {
    calculations
    }
    For a programmer, this should be straightforward and easy to use.
    To print things in bc use the print command
    Code:
    print 1+1
    prints 2 to the screen
    Code:
    print "Hello, CodeCall!\n"
    Will print Hello, CodeCall! to the screen followed by a newline.
    To read a number use the read() function:
    Code:
    number=read()
    Defining a function is like this:
    Code:
    define plusone(x) {
    x=x+1
    return x
    }
    This function will take x as a number and add one to it.
    If you want to run a program in bc, the command looks like this:
    Code:
    bc program.bc
    That's it! Feel free to post any questions, suggestions or comments. +rep is always appreciated.
    Root Beer == System Administrator's Beer
    Download the new operating system programming kit! (some assembly required)

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    Join Date
    Jul 2006
    Location
    Amherst, New York, United States
    Posts
    6,277
    Blog Entries
    26
    Rep Power
    20

    Re: Using bc (basic calculator)

    That is very cool! I always found it annoying I couldn't do math in my beloved terminal. I always assumed there was a way, but never cared enough to look into it. +rep

  4. #3
    Join Date
    Sep 2009
    Location
    USA
    Posts
    3,400
    Blog Entries
    5
    Rep Power
    37

    Re: Using bc (basic calculator)

    Thanks
    Root Beer == System Administrator's Beer
    Download the new operating system programming kit! (some assembly required)

  5. #4
    Jordan Guest

    Re: Using bc (basic calculator)

    I never knew about this either, very cool! +rep

  6. #5
    Join Date
    Jul 2006
    Posts
    16,491
    Blog Entries
    75
    Rep Power
    143

    Re: Using bc (basic calculator)

    Behold the power of the command-line! +rep
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  7. #6
    Join Date
    Aug 2009
    Location
    ~/
    Posts
    918
    Rep Power
    19

    Re: Using bc (basic calculator)

    Very Nice +rep

  8. #7
    Join Date
    Oct 2007
    Location
    /dev/null
    Posts
    4,513
    Blog Entries
    8
    Rep Power
    59

    Re: Using bc (basic calculator)

    I love Linux even more now. +rep!

    EDIT: Nevermind, it won't let me do it.
    sudo rm -rf /

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Calculator using visual basic
    By bgjyd834 in forum General Programming
    Replies: 0
    Last Post: 04-27-2011, 01:00 AM
  2. Basic Calculator
    By AfTriX in forum Visual Basic Tutorials
    Replies: 8
    Last Post: 03-23-2010, 03:55 AM
  3. PHP: Very Basic Calculator
    By Brandon W in forum Classes and Code Snippets
    Replies: 29
    Last Post: 10-11-2008, 03:05 PM
  4. Basic Calculator
    By Hydromethod in forum Visual Basic Programming
    Replies: 1
    Last Post: 09-19-2008, 12:55 AM
  5. help on basic calculator?pls?
    By exypnos in forum C and C++
    Replies: 3
    Last Post: 11-07-2007, 05:32 PM

Tags for this Thread

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