bash,bc modulo不能与-l标志一起使用

所以我试图用bc来计算一些对数,但我也需要用它来计算某些东西的模数.在制作我的剧本时,我启动了bc来测试它.

没有任何标志,bc<<< “3%5”当然会返回3. 但是使用bc -l(加载数学库以便我可以计算对数),%b的任何计算都返回0,其中a和b可以是任何数字,但是0. 发生了什么?

那是因为,从手册:
expr % expr
          The result of the expression is the "remainder" and it  is  com‐
          puted  in  the following way.  To compute a%b,first a/b is com‐
          puted to scale digits.  That result is used to compute a-(a/b)*b
          to  the scale of the maximum of scale+scale(b) and scale(a).  If
          scale is set to zero and  both  expressions  are  integers  this
          expression is the integer remainder function.

当您使用-l标志运行bc时,scale设置为20.要解决此问题:

bc -l <<< "oldscale=scale; scale=0; 3%5; scale=oldscale; l(2)"

我们首先在变量oldscale中保存scale,然后将scale设置为0以执行一些算术运算,并计算ln我们将scale设置回其旧值.这将输出

3
.69314718055994530941

如所想.

相关文章

用的openwrt路由器,家里宽带申请了动态公网ip,为了方便把2...
#!/bin/bashcommand1&command2&wait从Shell脚本并行...
1.先查出MAMP下面集成的PHP版本cd/Applications/MAMP/bin/ph...
1、先输入locale-a,查看一下现在已安装的语言2、若不存在如...
BashPerlTclsyntaxdiff1.进制数表示Languagebinaryoctalhexa...
正常安装了k8s后,使用kubect工具后接的命令不能直接tab补全...