473,372 Members | 926 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,372 software developers and data experts.

fmod and %


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


what is going wrong? Any suggestions

Thanks in advance

Zunbeltz Izaola
Jul 18 '05 #1
2 4595
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.

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.]

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.


Jul 18 '05 #2

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


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


And this is 1.0 - .32768

Terry J. Reedy

Jul 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

by: seia0106 | last post by:
Hello, I have a C++ program , which has following two lines of code z=atan2(x,y); z=(float)(fmod(2*pi+z, 2*pi); The comments written by the other programmer says that second line is used to...
by: Lonnie Princehouse | last post by:
I've been trying to debug this for two days now, and it's a longshot but I'm hoping that someone here might recognize a solution. I've got a C extension which calls a function in a C library,...
by: stau | last post by:
Hi! I'm reading a C book, and it says that fmod() returns the remainder of the exact division of it's arguments. Well, in a exact division, the remainder shall always be 0 (zero), so this don't...
by: Gintautas | last post by:
I'm trying to play a part of wav file. FSOUND_Sample_Load (0,"T01.wav",FSOUND_NORMAL, 0,0); plays all file FSOUND_Sample_Load (0,"T01.wav",FSOUND_NORMAL, 0,90000); plays file until 90000 sample...
by: joseph.p.doyle | last post by:
This code, compiled with visual studio .NET 2003, double a = 95.022, b = 0.01; printf ("%lf - floor(%lf / %lf) * %lf = %.17lf\n", a, a, b, b, a - floor(a / b) * b); a = 95.021, b = 0.01;...
by: Aaron Gray | last post by:
Does anyone have a good fmod() function written in Javascript ? Many thanks in advance, Aaron
by: bummerland | last post by:
Hi, I have a problem with the function fmod. Why is fmod(5.7, 0.1) = 0.1 ?? Why is it not 0? tia bummerland
by: bsabiston | last post by:
Hi, I'm trying to get the fractional part of a floating point number. I've tried fmod() and modf(), and while both do work, they also occasionally return 1.0 for the fractional part of the number...
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.