如何获得BC科学(又名指数)表示法来处理数字? [英] How to get bc to handle numbers in scientific (aka exponential) notation?

查看:173
本文介绍了如何获得BC科学(又名指数)表示法来处理数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

BC 不喜欢数字EX $ P $科学记数法pssed(又名指数表示法)。

bc doesn't like numbers expressed in scientific notation (aka exponential notation).

$ echo "3.1e1*2" | bc -l
(standard_in) 1: parse error

但我需要用它来处理在这个符号pssed前$ P $几个记录。有没有办法让 BC 了解指数符号?如果不是这样,我有什么办法将其转化为一种格式, BC 会明白?

but I need to use it to handle a few records that are expressed in this notation. Is there a way to get bc to understand exponential notation? If not, what can I do to translate them into a format that bc will understand?

推荐答案

不幸的是, BC 不支持科学记数法。

Unfortunately, bc doesn't support scientific notation.

但是,它可以转化为一种格式, BC 可以处理,使用 SED :结果
通过使用

However, it can be translated into a format that bc can handle, using sed:
By using

value=`echo ${value} | sed -e 's/[eE]+*/\\*10\\^/'`

您可以取代E(或E +,如果指数为正)以* 10 ^,其中 BC 将及时了解。这适用即使指数为负,或者如果数目随后通过另一个电源相乘,并允许跟踪的显著位

you can replace the "e" (or "e+", if the exponent is positive) with "*10^", which bc will promptly understand. This works even if the exponent is negative or if the number is subsequently multiplied by another power, and allows keeping track of significant digits.

编辑:感谢jwpat7和保罗汤布林澄清sed的语法方面

thanks to jwpat7 and Paul Tomblin for clarifying aspects of sed's syntax.

编辑:

正如ormaaj指出的那样,这也可以在纯的bash完成。只需使用

As ormaaj pointed out, this can also be done in pure bash. Just use

value=${value/[eE]+*/*10^}

来实现完全与上述相同。

to accomplish exactly the same as above.

这篇关于如何获得BC科学(又名指数)表示法来处理数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆