


Thread Tools  Display Modes 
Jan 1st, 2009, 7:42 PM  #1 
Professional Programmer
Join Date: Nov 2008
Posts: 336
Rep Power: 8

Using the modulus operator with type double
I want to use the modulus operator with a double type variable. Trying to use it with just the modulus operator results in an error. I read on the web that you can use modf (http://msdn.microsoft.com/enus/library/bk4c380c.aspx) to accomplish modulus operations with double. There are two questions I have. 1) Do you know if this works or if there is a better way. 2) How exactly would you use modf to achieve modulus functionality with double type variables?

Jan 1st, 2009, 8:17 PM  #2  
Programming Guru
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,329
Rep Power: 13

Re: Using the modulus operator with type double
If fmod() provides the result you seek, then it works, by definition. If you are seeking a different result, when a "better way" would be to use a method that produces the result you seek.
Quote:
result = fmod(x, y); .If you want to somehow be able to type result = x % y ; where x and y are both of type double, then you have no option.If either x or y are of class types, then you can overload operator%() to accept arguments of that class. Such overloading is not allowed if x and y are both basic floating point types (float, double). 

Jan 1st, 2009, 8:28 PM  #3  
C/C++ Developer
Join Date: Sep 2008
Location: Florida, USA
Posts: 130
Rep Power: 8

Re: Using the modulus operator with type double
Quote:
I may be overlooking its usefulness, as I don't see what benefit modulus has on two floats/doubles, but can you explain where and how you want to use this  (a) to be able to help you out, and (b) perhaps I can learn something here as well?
__________________
 William 

Jan 2nd, 2009, 12:17 AM  #4 
Professional Programmer
Join Date: Nov 2008
Posts: 336
Rep Power: 8

Re: Using the modulus operator with type double
I have an iterator that can be very large. I know that traditionally, iterators are supposed to be counting numbers, but doubles can hold the largest values. The only reason I want to use the modulus operator is because whenever the iterator divided by a certain number has a remainder of 0, I want to output the newline character. I'm thinking I could write a function to calculate the values at which such cases would happen, and have an if structure to output the newline and increase the value to the next step.

Jan 2nd, 2009, 1:33 AM  #5  
Programming Guru
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,329
Rep Power: 13

Re: Using the modulus operator with type double
Quote:
A double variable can hold a larger value than an integral type, but that's because it sacrifices precision. Integral types guarantee they can hold every integral value from their minimum to their maximum, meaning it is possible to iterate from the minimum to the maximum value. Floating point types do not offer a similar guarantee. If you have a for loop of the form for (double x = 0.0; x < some_very_large_value; x += 1.0) { // whatever } x for which x + 1.0 yields the original value x . The values tend to be large, but exist for all realworld floating point types. Such is the joy of rounding error.For this reason, using floating point variables to control loops is generally considered an extremely poor idea (except using specific techniques in specialised applications like numerical analysis, but you're not doing that). This is also, incidentally, the reason for the "traditional" approach of using iterators. If you want to get large values, you work out some system to do so. For example, have an outer loop that controls outputs of lines (let's say each line is 80 characters) and the inner loop output the 80 characters within each line. All you need to do is output the '\n' after completion of the inner loop. You then output 80 times the number of iterations of the outer loop (not including your newlines), but you do not run into problems due to limits of floating point precision. Deeper levels of nesting can be used if needed. 

Jan 2nd, 2009, 7:53 AM  #6 
Professional Programmer
Join Date: Nov 2008
Posts: 336
Rep Power: 8

Re: Using the modulus operator with type double
Oh. Thank you for that.

Bookmarks 
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)  
Thread Tools  
Display Modes  


Similar Threads  
Thread  Thread Starter  Forum  Replies  Last Post 
Difference between double and Double in C#  jonyzz  C#  15  Jul 29th, 2009 6:31 AM 
Incompatible pointer type (double pointer in a function)  Gabriel Margarido  C  16  Nov 23rd, 2007 3:04 AM 
Pointer return type,.. ???  Mack1982  C++  28  May 26th, 2006 8:02 PM 
bitwise operators and type double  ionexchange  C++  14  Feb 12th, 2006 9:27 AM 
How do i use a ^ for a double variable?  dvu  C#  4  Feb 3rd, 2006 9:27 AM 