免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 5116 | 回复: 11
打印 上一主题 下一主题

关于python浮点mod运算的解释。 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-05-24 12:39 |只看该作者 |倒序浏览
问题来自http://bbs.chinaunix.net/thread-1708133-1-1.html 这个帖子
但是我感觉帖子里对python浮点 % 运算并没有彻底讨论清楚,因此这里说一下我个人的理解。
关于python浮点mod运算的结果 <<core python>>里提到这样一个公式,x-(math.floor(x/y)*y)
这实际上是说python对浮点商是向下取整的,这个可以通过float_divmod的实现得到求证。
......
mod = fmod(vx, wx);
        /* fmod is typically exact, so vx-mod is *mathematically* an
           exact multiple of wx.  But this is fp arithmetic, and fp
           vx - mod is an approximation; the result is that div may
           not be an exact integral value after the division, although
           it will always be very close to one.
        */
        div = (vx - mod) / wx;  //注意这里
       
        if (mod) {
                /* ensure the remainder has the same sign as the denominator */
                if ((wx < 0) != (mod < 0)) {
                        mod += wx;
                        div -= 1.0;
                }
        }
......

论坛徽章:
0
2 [报告]
发表于 2010-05-24 12:49 |只看该作者
其实是,关键要理解地板除的含义。

论坛徽章:
0
3 [报告]
发表于 2010-05-24 13:27 |只看该作者
我测试了下确实是,学习了

论坛徽章:
0
4 [报告]
发表于 2010-05-24 14:02 |只看该作者
嗯,感谢楼主分享,以前每次用之前先自己试一遍,汗……

论坛徽章:
0
5 [报告]
发表于 2010-05-24 21:36 |只看该作者
math.fmod(x, y)
Return fmod(x, y), as defined by the platform C library. Note that the Python expression x % y may not return the same result. The intent of the C standard is that fmod(x, y) be exactly (mathematically; to infinite precision) equal to x - n*y for some integer n such that the result has the same sign as x and magnitude less than abs(y). Python’s x % y returns a result with the sign of y instead, and may not be exactly computable for float arguments. For example, fmod(-1e-100, 1e100) is -1e-100, but the result of Python’s -1e-100 % 1e100 is 1e100-1e-100, which cannot be represented exactly as a float, and rounds to the surprising 1e100. For this reason, function fmod() is generally preferred when working with floats, while Python’s x % y is preferred when working with integers.

看下fmod的定义以及%的定义会更清楚些:
http://python.org/doc/current/ref/binary.html#l2h-411

论坛徽章:
0
6 [报告]
发表于 2010-05-24 21:50 |只看该作者
回复 5# 可可熊
从文档上看还真是按绝对值来的,我理解的有偏差。不过实际效果还是向下取整数。

论坛徽章:
0
7 [报告]
发表于 2010-05-25 11:33 |只看该作者
回复 6# luffy.deng


    。。我还是想多问句,有中文手册吗?

论坛徽章:
0
8 [报告]
发表于 2010-05-25 11:38 |只看该作者
凡事google下先

论坛徽章:
0
9 [报告]
发表于 2010-05-25 18:26 |只看该作者
回复  luffy.deng


    。。我还是想多问句,有中文手册吗?
zhuzhu316 发表于 2010-05-25 11:33



    有也都是很旧的,而且手册这种东西,一般翻译了也会晦涩难懂吧……往往看起来比英文难得多。

论坛徽章:
0
10 [报告]
发表于 2010-05-25 19:55 |只看该作者
其实可以这么看,我们就是要确定k令:0 <= a + k * b < b
原来a==-25.5,b==2.25
当k==12时-25.5 + 12 * 2.25 == 1.5
这个值就是我们要求的
其他的也是一样的…………之所以会有地板除,是在找k的时候方便
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP