[Linux] mathematical computation in the command line

Source: Internet
Author: User

In the command line, sometimes a simple manual mathematical computation is required. The built-in "BC" command is the front end of an "arbitrary precision computing language". If you are a person who is working and learning mathematics, it is very meaningful to check how it works in man. Therefore, you can perform some daily computing work on the command line.
Use BC, enter "BC" in the command prompt, and then use "+", "-", "*" (multiplication), and "/" (Division). For example, if the calculation result is 200 multiplied by 133, enter "200*133" and press Enter.
By default, the BC calculation result has no decimal places. You can enter "scale = 8", so that the calculation result will be accurate to 8 digits after the decimal place.
After the calculation is complete, press Ctrl + D to exit.
The BC switching mode may be cumbersome for simple computation, so you can create a shell script to directly perform computation.
Open gedit and enter the following content:
#! /Bin/bash
# Run input through BC for simple math purposes
Scale = 'scale = 8; '# No of decimal places for result
Math =$ {scale }$ @
Echo $ math | BC
Keep exiting. Change the File Permission to "executable" and copy it to the "/usr/bin" directory.
$ Chmod + x calc
$ Sudo MV calc/usr/bin/
Now this command can be directly executed, for example, input:
$ Calc 203 + 99/16
After the execution is completed, the result "209.1875" is output ".

 

In addition, you can also use echo "1 + 1" | BC-l command

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.