bash bc 截断浮点数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20558710/
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 truncate floating point number
提问by Bilal Syed Hussain
How do I truncate a floating point number using bc
如何截断浮点数使用 bc
e.g if I do
例如,如果我这样做
echo '4.2-1.3' | bc
which outputs 2.9
how I get it to truncate/use floor to get 2
输出2.9
我如何让它截断/使用地板来获得2
回答by timrau
Use /
operator.
使用/
运算符。
echo '(4.2-1.3) / 1' | bc
回答by James Waldby - jwpat7
Dividing by 1 works ok if scale
is 0 (eg, if you start bc with bc
and don't change scale
) but fails if scale
is positive (eg, if you start bc with bc -l
or increase scale
). (See transcript below.) For a general solution, use a trunc
function like the following:define trunc(x) { auto s; s=scale; scale=0; x=x/1; scale=s; return x }
如果1分的作品确定scale
为0(例如,如果你开始与BCbc
不改scale
),但如果失败scale
(如果你开始BC与如为正bc -l
或增加scale
)。(请参阅下面的文字记录。)对于一般解决方案,请使用trunc
如下函数:define trunc(x) { auto s; s=scale; scale=0; x=x/1; scale=s; return x }
Transcript that illustrates how divide by 1 by itself fails in the bc -l
case, but how trunc
function works ok at truncating toward zero:
说明如何被 1 除以自身失败的脚本在这种bc -l
情况下失败,但trunc
函数如何在向零截断时正常工作:
> bc -l
bc 1.06.95
[etc...]
for (x=-4; x<4; x+=l(2)) { print x,"\t",x/1,"\n"}
-4 -4.00000000000000000000
-3.30685281944005469059 -3.30685281944005469059
-2.61370563888010938118 -2.61370563888010938118
-1.92055845832016407177 -1.92055845832016407177
-1.22741127776021876236 -1.22741127776021876236
-.53426409720027345295 -.53426409720027345295
.15888308335967185646 .15888308335967185646
.85203026391961716587 .85203026391961716587
1.54517744447956247528 1.54517744447956247528
2.23832462503950778469 2.23832462503950778469
2.93147180559945309410 2.93147180559945309410
3.62461898615939840351 3.62461898615939840351
define trunc(x) { auto s; s=scale; scale=0; x=x/1; scale=s; return x }
for (x=-4; x<4; x+=l(2)) { print x,"\t",trunc(x),"\n"}
-4 -4
-3.30685281944005469059 -3
-2.61370563888010938118 -2
-1.92055845832016407177 -1
-1.22741127776021876236 -1
-.53426409720027345295 0
.15888308335967185646 0
.85203026391961716587 0
1.54517744447956247528 1
2.23832462503950778469 2
2.93147180559945309410 2
3.62461898615939840351 3
回答by Michael Rybkin
Try the following solution. It will truncate anything after the decimal point without a problem:
尝试以下解决方案。它将截断小数点后的任何内容而不会出现问题:
echo 'x = 4.2 - 1.3; scale = 0; x / 1' | bc -l
echo 'x = l(101) / l(10); scale = 0; x / 1' | bc -l
You can make the code a tad shorter by performing calculations directly on the numbers:
您可以通过直接对数字执行计算来缩短代码:
echo 'scale = 0; (4.2 - 1.3) / 1' | bc -l
echo 'scale = 0; (l(101) / l(10)) / 1' | bc -l
In general, you can use this function to get only the integer part of a number:
通常,您可以使用此函数仅获取数字的整数部分:
define int(x) {
auto s;
s = scale;
scale = 0;
x /= 1; /* This will have the effect of truncating x to its integer value */
scale = s;
return (x);
}
Save that code into a file (let's call it int.bc) and run the following command:
将该代码保存到一个文件中(我们称之为int.bc)并运行以下命令:
echo 'int(4.2 - 1.3);' | bc -l int.bc
回答by Isaac
The variable governing the amount of decimals on divisionis scale.
控制除法小数位数的变量是scale。
So, if scale is 0
(the default), dividing by 1 would truncate to 0
decimals:
因此,如果 scale 是0
(默认值),除以 1 将截断为0
小数:
$ echo '(4.2-1.3) / 1 ' | bc
2
In other operations, the number of decimals is calculated from the scale (number of decimals) of each operand. In add, subtract and multiplication, for example, the resulting scale is the biggest of both:
在其他运算中,小数位数是根据每个操作数的小数位数(小数位数)计算得出的。例如,在加法、减法和乘法中,结果比例是两者中最大的:
$ echo ' 4.2 - 1.33333333 ' | bc
2.86666667
$ echo ' 4.2 - 1.333333333333333333 ' | bc
2.866666666666666667
$ echo ' 4.2000 * 1.33 ' | bc
5.5860
Instead, in division, the number of decimals is strictly equal to th evalue of the variable scale
:
相反,在除法中,小数位数严格等于变量的值scale
:
$ echo 'scale=0;4/3;scale=3;4/3;scale=10;4/3' | bc
1
1.333
1.3333333333
As the value of scale has to be restored, it is better to define a function (GNU syntax):
由于必须恢复 scale 的值,最好定义一个函数(GNU 语法):
$ echo ' define int(x){ os=scale;scale=0;x=x/1;scale=os;return(x) }
int( 4.2-1.3 )' | bc
2
Or in older POSIX language:
或使用较旧的 POSIX 语言:
$ echo ' define i(x){
o=scale;scale=0;x=x/1;scale=o;return(x)
}
i( 4.2-1.3 )' | bc
2