如何阻止 bc 分割线?
我正在使用 bash 脚本中的 bc 来做一些快速而肮脏的 BigInteger 数学,但是当我增大比例时,它开始在我身上分割线:
pax> echo 'scale=200 ; 1 / 4' | bc
.2500000000000000000000000000000000000000000000000000000000000000000\
00000000000000000000000000000000000000000000000000000000000000000000\
00000000000000000000000000000000000000000000000000000000000000000
pax> num="$(echo 'scale=200 ; 1 / 4' | bc )" ; echo $num
.2500000000000000000000000000000000000000000000000000000000000000000\ 00000 ...
我如何阻止这种情况发生,以便我可以直接得到数字而不进行任何分割吗?手册页记录了这种行为,但似乎没有提供任何更改它的选项。
实际上,我会退一步告诉您请求的来源,以防有人有更好的解决方案。我需要一个相当于值 2-n 的 C 字符串数组,大致如下:
static char *str[] = {
"1.00000000 ... 000", // 1/1 to 150 fractional places.
"0.50000000 ... 000", // 1/2
"0.25000000 ... 000", // 1/4
"0.12500000 ... 000", // 1/8
: : :
"0.00000000 ... 004", // 1/(2^256)
};
我不关心什么语言生成该数组,我只是要获取输出并插入它进入我的C代码。然而我确实需要准确性。
I'm using bc
from a bash script to do some quick and dirty BigInteger math but, when I bump up the scale, it starts splitting lines on me:
pax> echo 'scale=200 ; 1 / 4' | bc
.2500000000000000000000000000000000000000000000000000000000000000000\
00000000000000000000000000000000000000000000000000000000000000000000\
00000000000000000000000000000000000000000000000000000000000000000
pax> num="$(echo 'scale=200 ; 1 / 4' | bc )" ; echo $num
.2500000000000000000000000000000000000000000000000000000000000000000\ 00000 ...
How do I stop this from happening so that I can just get the number without any splits? The man page documents this behaviour but doesn't seem to give any options for changing it.
Actually, I'll step back and tell you the source of the request in case anyone has a better solution. I need an array of strings in C equivalent to the values 2-n, along the lines of:
static char *str[] = {
"1.00000000 ... 000", // 1/1 to 150 fractional places.
"0.50000000 ... 000", // 1/2
"0.25000000 ... 000", // 1/4
"0.12500000 ... 000", // 1/8
: : :
"0.00000000 ... 004", // 1/(2^256)
};
I don't care what language generates the array, I'm just going to take the output and plug it into my C code. I do need the accuracy however.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
至少在我的系统上,BC_LINE_LENGTH 环境变量控制输出行的长度:
At least on my system, the BC_LINE_LENGTH environment variable controls how long the output lines are:
这就是原始值。
如果您想创建正确的 C 语法,请使用类似的内容。
这可能会做你想做的事。
That's the raw values.
If you want to create proper C syntax, use something like this.
That might do what you want.