linux bc 10 100m,linux calulator bc

Overview

bc command is a linux command line calculator, can do basic mathematical calculations, like Integer, floating calculations.

usage: bc [options] [file ...]

-h --help print this usage and exit

-i --interactive force interactive mode

-l --mathlib use the predefined math routines

-q --quiet don't print initial banner

-s --standard non-standard bc constructs are errors

-w --warn warn about non-standard bc constructs

-v --version print version information and exit

The bc command supports the following features:Arithmetic operators

Increment or Decrement operators

Assignment operators

Comparison or Relational operators

Logical or Boolean operators

Math functions

Conditional statements

Iterative statements

Usage

1. Arithmetic operators

$ a=2

$ b=10

$ echo "$a+$b" | bc

12

$ echo "$a-$b" | bc

-8

$ echo "$a*$b" | bc

20

$ echo "$a/$b" | bc

0

$ echo "$a^$b" | bc

1024

$ echo "sqrt(10)" | bc

3

$ echo "scale=2;sqrt(10)" | bc

3.16

bc 可以用 scale 保留小数位, only supports the /, sqrt, if you want to use it as follows:

$ echo "scale=2;3.333+4.444" | bc

7.777

$ echo "scale=2;(3.333+4.444)/1" | bc

7.77

From above, it not support math rounding, there is a workaround as follows:

$ echo "scale=2;(7.777+0.005)/1" | bc

7.78

2. Assignment Operatorsvar = value : Assign the vale to the variable

var += value : similar to var = var + value

var -= value : similar to var = var – value

var *= value : similar to var = var * value

var /= value : similar to var = var / value

var ^= value : similar to var = var ^ value

var %= value : similar to var = var % value

$ echo "var=10;var+=9;var" | bc

19

3. Increment and Decrement Operators

$ echo "var=10;var++" | bc

10

$ echo "var=10;++var" | bc

11

$ echo "var=10;var--" | bc

10

$ echo "var=10;--var" | bc

9

4. Comparison or Relational Operators

These are used to compare 2 numbers. If the comparison is true, then result is 1. Otherwise(false), returns 0.

Operators: ,>=,==,!=.

$ echo "10>=10.0" | bc

1

$ echo "10!=10.0" | bc

0

5. Logical or Boolean Operators

$ echo "10 && 5" | bc

1

$ echo "0 || 0" | bc

0

$ echo "!0" | bc

bash: !0: event not found

$ echo "! 0" | bc

1

6.Mathematical Functions

$ echo "length(1.2345)" | bc -l

5

$ echo "scale(1.2345)" | bc -l

4

进制转换

$ echo "ibase=10;obase=2;15" | bc -l

1111

$ echo "ibase=2;1111" | bc

15

7. Conditional Statements

$ echo 'n=8;m=10;if(n>m) print "n is greater" else print "m is greater" ' | bc -l

m is greater

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值