By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,766 Members | 1,435 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,766 IT Pros & Developers. It's quick & easy.

fmod and %

P: n/a

Hi,

I have a problem with % operation. It returns a incorrect value in
this case:
-2.77555756156e-17%1 1.0

where the returned value should be -2.77555756156e-17.

The documentation says that there is same errors due to roundoff and
suggest to use fmod from math module.
fmod( -2.77555756156e-17,1) -2.77555756156e-17

Ok, but when i try the following:
fmod(-0.32768000000000003,1) -0.32768000000000003

but i want to get the result % gives
-0.32768000000000003%1

0.67232000000000003

what is going wrong? Any suggestions

Thanks in advance

--
Zunbeltz Izaola
Jul 18 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
fmod and % have a different rule about the sign of the returned value.
This is documented both in the language reference and in the library
reference, in the appropriate spots.

http://python.org/doc/current/ref/binary.html#l2h-411
The % (modulo) operator yields the remainder from the division of
the first argument by the second. The numeric arguments are first
converted to a common type. A zero right argument raises the
ZeroDivisionError exception. The arguments may be floating point
numbers, e.g., 3.14%0.7 equals 0.34 (since 3.14 equals 4*0.7 + 0.34.)
The modulo operator always yields a result with the same sign as its
second operand (or zero); the absolute value of the result is strictly
smaller than the absolute value of the second operand

[Footnote: While abs(x%y) < abs(y) is true mathematically, for
floats it may not be true numerically due to roundoff. For example,
and assuming a platform on which a Python float is an IEEE 754
double-precision number, in order that -1e-100 % 1e100 have the
same sign as 1e100, the computed result is -1e-100 + 1e100, which
is numerically exactly equal to 1e100. Function fmod() in the math
module returns a result whose sign matches the sign of the first
argument instead, and so returns -1e-100 in this case. Which approach
is more appropriate depends on the application.]

http://python.org/doc/current/lib/mo....html#l2h-1105
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.

Jeff

Jul 18 '05 #2

P: n/a

"Zunbeltz Izaola" <zu******@wm.lc.ehu.es.XXX> wrote in message
news:ct*************@lcpxdf.wm.lc.ehu.es...

Hi,

I have a problem with % operation. It returns a incorrect value in
this case:
-2.77555756156e-17%1 1.0

where the returned value should be -2.77555756156e-17.


Wrong. As per the docs, x % positive number is defined as being positive
and <= that positive number. So above should be about 1.0 - 2.8e17, which,
for 32 bit machines, is 1.0, which is exactly what you got
1 - 2.8e-17 1.0.

as with 1 - 5.5e-17 1.0

whereas 1 - 5.6e-17 0.99999999999999989
-0.32768000000000003%1

0.67232000000000003


And this is 1.0 - .32768

Terry J. Reedy


Jul 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.