Linux bc 忽略比例选项
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13963265/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
bc is ignoring scale option
提问by dabest1
I can't figure out why bc tool sometimes ignores the scale option.
我不明白为什么 bc 工具有时会忽略缩放选项。
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.
采纳答案by Kent
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:
正如卡尔指出的那样,如果您查看手册页,您可以找到该行。它是关于表达解释的。减法不会读取scale
变量。如果你想得到预期的结果(1.30),你可以:
kent$ echo 'scale=2; (2.777 - 1.4744)/1' | bc
1.30
/
operation will read scale
variable.
/
操作将读取scale
变量。
回答by Carl Norum
From the bc(1)
man page:
从bc(1)
手册页:
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.
1.4744
有 scale 4
,所以这就是你的表达方式。