Floating-point calculation in shell scripts

Source: Internet
Author: User

Bash Digital Computing--Digital processing power is critical for any programming language, but for bash shell scripts, this ability is poor, even if the form of Expr command and $[] can only be used for integer operations, the operation of floating-point numbers cannot be completed. To break this limit, a bash floating point solution was introduced, which is built-in bash calculator (BC)

First, BC introduction

BC is actually a programming language that allows you to enter a floating-point expression at the command line and then execute it.

BC can recognize (floating-point) numbers, variables, expressions, programming statements, functions.

You can use the BC command to access bc,quit from the shell prompt to exit BC:

[Email protected] ~]# BCBC 1.06.95Copyright 1991-1994, 1997, 1998, $, 2004, 2006 free software Foundation, inc.this Free software with absolutely NO WARRANTY. For details type ' warranty '. 1.25*33.75quit

The scale variable sets the number of decimal digits , the default scale=0, and when scale=4, displays the answer to the 4-bit decimal digit:

[Email protected] ~]# Bc-q5/41scale=45/41.2500quit

Second, the use of BC in the script

Run the BC command using the inverse quotation marks

Var= ' echo ' options;expression ' |BC '

#!/bin/bashvar= ' echo ' Scale=4;5/4 "|BC ' echo $var

built-in input redirection run BC command

var= ' BC << EOF

Options

Expression

EOF '

#!/bin/bashvar= ' BC << eofscale=4a=5b=4b/aeof ' echo $var

Note: The variables in the BC are only valid in the Bash calculator and cannot be used outside of the BC



Floating-point calculation in shell scripts

Related Article

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.