473,473 Members | 1,837 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

modulus operator "%" versus fmod

I'm trying to decide which of these two to use to calculate the
remainder between two floating point numbers. My gut instinct says to go
for the % operator - but I'm not sure if I am right. Which is the
correct one (i.e. more effecient) and why?

Thanks

Nov 14 '05 #1
3 7593
exquisitus wrote:
I'm trying to decide which of these two to use to calculate the
remainder between two floating point numbers. My gut instinct says to go
for the % operator - but I'm not sure if I am right. Which is the
correct one (i.e. more effecient) and why?


The % operator only works for integer arguments; moreover, as
% is defined in terms of / (i.e. a -(a/b)*b == a%b, if b!=0),
there is no sensible value, as we do not necessarily "lose"
something (which also could be recovered by a % operation)
by a "floating point" division.

So, fmod() is your way to go.
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Nov 14 '05 #2


Michael Mair wrote:
exquisitus wrote:
I'm trying to decide which of these two to use to calculate the
remainder between two floating point numbers. My gut instinct says to
go for the % operator - but I'm not sure if I am right. Which is the
correct one (i.e. more effecient) and why?

The % operator only works for integer arguments; moreover, as
% is defined in terms of / (i.e. a -(a/b)*b == a%b, if b!=0),
there is no sensible value, as we do not necessarily "lose"
something (which also could be recovered by a % operation)
by a "floating point" division.

So, fmod() is your way to go.
Cheers
Michael


Thanks Michael !

Nov 14 '05 #3
exquisitus wrote:
I'm trying to decide which of these two to use to calculate the
remainder between two floating point numbers. My gut instinct says to go
for the % operator - but I'm not sure if I am right. Which is the
correct one (i.e. more effecient) and why?

Thanks


Gut instinct can give you a tummy ache. You should be looking for
correctness, not efficiency. Better to read something. Do you have a C
book? "The % operator cannot be applied to float or double." is quite
prominent in my book.

--
Joe Wright mailto:jo********@comcast.net
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 14 '05 #4

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

Similar topics

34
by: Pmb | last post by:
I've been working on creating a Complex class for my own learning purpose (learn through doing etc.). I'm once again puzzled about something. I can't figure out how to overload the assignment...
81
by: Matt | last post by:
I have 2 questions: 1. strlen returns an unsigned (size_t) quantity. Why is an unsigned value more approprate than a signed value? Why is unsighned value less appropriate? 2. Would there...
43
by: markryde | last post by:
Hello, I saw in some open source projects a use of "!!" in "C" code; for example: in some header file #define event_pending(v) \ (!!(v)->vcpu_info->evtchn_upcall_pending & \...
2
by: John Goche | last post by:
Hello, Could anyone please provide with some information on the C++ overloaded cast operator and in which circumstances this might be useful? I have consulted several references but found no...
10
by: lovecreatesbea... | last post by:
Is it correct and safe to compare a string object with "", a pair of quotation marks quoted empty string?If the string object: s = ""; does s contain a single '\'? Is it better to use...
25
by: mdh | last post by:
Given the expression: while (isaspace(c = *s++)) x+1; (s is an array) Does the increment to 's' occur after "x+1" is evaluated, or after the content of s is assigned to c? Is there a...
1
by: manchin2 | last post by:
Hi, Can anybody please provide the information about "&quot" and its use, if possible please provide an example. ...
4
by: fran7 | last post by:
Hi, from help in the javascript forum I found the error in some code but need help. This bit of code works perfectly, trouble is I am writing it to a javascript function so the height needs to be in...
3
by: tvnaidu | last post by:
I compiled tinyxml files (.cpp files) and when I am linking to create final exe in Linux, I am getting lot of errors saying "undefiend reference" to "operator new" and "delete", any idea?. ...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.