亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費(fèi)注冊(cè) 查看新帖 |

Chinaunix

  平臺(tái) 論壇 博客 文庫(kù)
12下一頁(yè)
最近訪問(wèn)板塊 發(fā)新帖
查看: 5229 | 回復(fù): 11
打印 上一主題 下一主題

關(guān)于python浮點(diǎn)mod運(yùn)算的解釋。 [復(fù)制鏈接]

論壇徽章:
0
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報(bào)告]
發(fā)表于 2010-05-24 12:39 |只看該作者 |倒序?yàn)g覽
問(wèn)題來(lái)自http://www.72891.cn/thread-1708133-1-1.html 這個(gè)帖子
但是我感覺(jué)帖子里對(duì)python浮點(diǎn) % 運(yùn)算并沒(méi)有徹底討論清楚,因此這里說(shuō)一下我個(gè)人的理解。
關(guān)于python浮點(diǎn)mod運(yùn)算的結(jié)果 <<core python>>里提到這樣一個(gè)公式,x-(math.floor(x/y)*y)
這實(shí)際上是說(shuō)python對(duì)浮點(diǎn)商是向下取整的,這個(gè)可以通過(guò)float_divmod的實(shí)現(xiàn)得到求證。
......
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 [報(bào)告]
發(fā)表于 2010-05-24 12:49 |只看該作者
其實(shí)是,關(guān)鍵要理解地板除的含義。

論壇徽章:
0
3 [報(bào)告]
發(fā)表于 2010-05-24 13:27 |只看該作者
我測(cè)試了下確實(shí)是,學(xué)習(xí)了

論壇徽章:
0
4 [報(bào)告]
發(fā)表于 2010-05-24 14:02 |只看該作者
嗯,感謝樓主分享,以前每次用之前先自己試一遍,汗……

論壇徽章:
0
5 [報(bào)告]
發(fā)表于 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的定義以及%的定義會(huì)更清楚些:
http://python.org/doc/current/ref/binary.html#l2h-411

論壇徽章:
0
6 [報(bào)告]
發(fā)表于 2010-05-24 21:50 |只看該作者
回復(fù) 5# 可可熊
從文檔上看還真是按絕對(duì)值來(lái)的,我理解的有偏差。不過(guò)實(shí)際效果還是向下取整數(shù)。

論壇徽章:
0
7 [報(bào)告]
發(fā)表于 2010-05-25 11:33 |只看該作者
回復(fù) 6# luffy.deng


    。。我還是想多問(wèn)句,有中文手冊(cè)嗎?

論壇徽章:
0
8 [報(bào)告]
發(fā)表于 2010-05-25 11:38 |只看該作者
凡事google下先

論壇徽章:
0
9 [報(bào)告]
發(fā)表于 2010-05-25 18:26 |只看該作者
回復(fù)  luffy.deng


    。。我還是想多問(wèn)句,有中文手冊(cè)嗎?
zhuzhu316 發(fā)表于 2010-05-25 11:33



    有也都是很舊的,而且手冊(cè)這種東西,一般翻譯了也會(huì)晦澀難懂吧……往往看起來(lái)比英文難得多。

論壇徽章:
0
10 [報(bào)告]
發(fā)表于 2010-05-25 19:55 |只看該作者
其實(shí)可以這么看,我們就是要確定k令:0 <= a + k * b < b
原來(lái)a==-25.5,b==2.25
當(dāng)k==12時(shí)-25.5 + 12 * 2.25 == 1.5
這個(gè)值就是我們要求的
其他的也是一樣的…………之所以會(huì)有地板除,是在找k的時(shí)候方便
您需要登錄后才可以回帖 登錄 | 注冊(cè)

本版積分規(guī)則 發(fā)表回復(fù)

  

北京盛拓優(yōu)訊信息技術(shù)有限公司. 版權(quán)所有 京ICP備16024965號(hào)-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號(hào):11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報(bào)專區(qū)
中國(guó)互聯(lián)網(wǎng)協(xié)會(huì)會(huì)員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關(guān)心和支持過(guò)ChinaUnix的朋友們 轉(zhuǎn)載本站內(nèi)容請(qǐng)注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP