bc is ignoring scale option

2019-03-23 07:52发布

I can't figure out why bc tool sometimes ignores the scale option.

Here is an example:

> echo 'scale=2; 2.777 - 1.4744' | bc
1.3026

Expected result is:

1.30

Additional information:

> bash --version
GNU bash, version 2.05b.0(1)-release (x86_64-suse-linux)
Copyright (C) 2002 Free Software Foundation, Inc.
> bc --version
bc 1.06
Copyright 1991-1994, 1997, 1998, 2000 Free Software Foundation, Inc.

标签: linux bash bc
2条回答
Explosion°爆炸
2楼-- · 2019-03-23 08:05

From the bc(1) man page:

Unless specifically mentioned the scale of the result is the maximum scale of the expressions involved.

1.4744 has scale 4, so that's what happens to your expression.

查看更多
不美不萌又怎样
3楼-- · 2019-03-23 08:20

as Carl pointed out, if you check man page, you can find that line. it is about expression explanations. subtraction won't read scale variable. If you want to get the expected result (1.30), you could:

kent$  echo 'scale=2; (2.777 - 1.4744)/1' | bc 
1.30

/ operation will read scale variable.

查看更多
登录 后发表回答