如何阻止 bc 分割线?

发布于 2024-10-04 02:59:17 字数 971 浏览 0 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

兔小萌 2024-10-11 02:59:17

至少在我的系统上,BC_LINE_LENGTH 环境变量控制输出行的长度:

$ echo 'scale=200; 1/4' | BC_LINE_LENGTH=9999 bc
.25000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

At least on my system, the BC_LINE_LENGTH environment variable controls how long the output lines are:

$ echo 'scale=200; 1/4' | BC_LINE_LENGTH=9999 bc
.25000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
遥远的她 2024-10-11 02:59:17
localhost:~ slott$ python -c 'print "{0:.200f}".format( 1./4. )'
0.25000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000


localhost:~ slott$ python -c 'import decimal; print 1/decimal.Decimal( 2**256 )'
8.636168555094444625386351863E-78

import decimal
for i in range(256):
    print "{0:.150f}".format(1/decimal.Decimal(2**i))

这就是原始值。

如果您想创建正确的 C 语法,请使用类似的内容。

def long_string_iter():
    for i in range(256):
        yield i, "{0:.150f}".format(1/decimal.Decimal(2**i))

def c_syntax( string_iter ):
   print "static char *str[] = {"
   for i, value in string_iter():
       print '    "{0}", // 1/{1}'.format( value, i )
   print "};"

这可能会做你想做的事。

localhost:~ slott$ python -c 'print "{0:.200f}".format( 1./4. )'
0.25000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000


localhost:~ slott$ python -c 'import decimal; print 1/decimal.Decimal( 2**256 )'
8.636168555094444625386351863E-78

import decimal
for i in range(256):
    print "{0:.150f}".format(1/decimal.Decimal(2**i))

That's the raw values.

If you want to create proper C syntax, use something like this.

def long_string_iter():
    for i in range(256):
        yield i, "{0:.150f}".format(1/decimal.Decimal(2**i))

def c_syntax( string_iter ):
   print "static char *str[] = {"
   for i, value in string_iter():
       print '    "{0}", // 1/{1}'.format( value, i )
   print "};"

That might do what you want.

苏璃陌 2024-10-11 02:59:17
% echo 'scale=200; 1/4' | bc | tr -d '\n' | tr -d '\\'
.25000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
% echo 'scale=200; 1/4' | bc | tr -d '\n' | tr -d '\\'
.25000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文