所以我试图用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
如所想.