[20170622]bc与取模运算.txt

[20170622]bc与取模运算.txt

--//前一阵子在使用bc做取模运算.发现一个奇怪的问题.开始以为是使用mod(受oracle的影响).
--//查手册才发现%.

--//例子如下:
$ bc -v
bc 1.06
Copyright 1991-1994, 1997, 1998, 2000 Free Software Foundation, Inc.

$ bc -q
9%4
1
9%5
4

--//很明显以上结果是正确的,但是如果加参数-l.
-l, --mathlib =>  Define the standard math library.

$ bc -lq
9%4
0
9%5
0
scale=12
9%5
0
9%4
0
--//很明显这个时候就不对了.

$ bc -lq
12312324%1232
.00000000000000001168

$ bc -q
12312324%1232
948

--//为什么呢?也就是如果使用-l参数,使用standard math library. %不再是取模运算.到底做什么运算呢?
$ man bc
--//里面一段话如下:
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.

# bc -l
bc 1.06
Copyright 1991-1994, 1997, 1998, 2000 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'.
1/3
.33333333333333333333
--//可以看出使用math 库,缺省scale=20.
--//而如果不使用-l参数scale=0.

--//这样如果在scale=20的情况,取模使用是这样.

12312324/1232
9993.76948051948051948051

12312324-9993.76948051948051948051*1232
.00000000000000001168

scale=2
12312324%1232
11.68

12312324/1232
9993.76
--//bc竟然没做四舍五入.

12312324-9993.76*1232
11.68

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/267265/viewspace-2141137/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/267265/viewspace-2141137/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值