Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jan 1st, 2009, 7:42 PM   #1
lrh9
Professional Programmer
 
Join Date: Nov 2008
Posts: 336
Rep Power: 6 lrh9 is on a distinguished road
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/en-us/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?
lrh9 is offline   Reply With Quote
Old Jan 1st, 2009, 8:17 PM   #2
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,329
Rep Power: 11 grumpy will become famous soon enough
Re: Using the modulus operator with type double

Quote:
Originally Posted by lrh9 View Post
1) Do you know if this works or if there is a better way.
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:
Originally Posted by lrh9 View Post
2) How exactly would you use modf to achieve modulus functionality with double type variables?
The usual way would be to call it. 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).
grumpy is offline   Reply With Quote
Old Jan 1st, 2009, 8:28 PM   #3
MasterWill
C/C++ Developer
 
MasterWill's Avatar
 
Join Date: Sep 2008
Location: Florida, USA
Posts: 130
Rep Power: 7 MasterWill is on a distinguished road
Re: Using the modulus operator with type double

Quote:
Originally Posted by lrh9 View Post
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/en-us/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?
I guess the first question I have is: why are you looking to do this? The modulus operator is usually used with two integer types, and I have used it for a number of reasons - for example, to cause updates to the GUI every n number of iterations in a loop - but I have never heard of a use for it with floating point values. In theory, and someone correct me if I'm wrong, there is no remainder - the result of the division between two floating point values is another floating point value.

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
MasterWill is offline   Reply With Quote
Old Jan 2nd, 2009, 12:17 AM   #4
lrh9
Professional Programmer
 
Join Date: Nov 2008
Posts: 336
Rep Power: 6 lrh9 is on a distinguished road
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.
lrh9 is offline   Reply With Quote
Old Jan 2nd, 2009, 1:33 AM   #5
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,329
Rep Power: 11 grumpy will become famous soon enough
Re: Using the modulus operator with type double

Quote:
Originally Posted by lrh9 View Post
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.
Your logic is fatally flawed if you believe that floating point variables should be used to count.

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
}
the loop can be infinite. This occurs because the limited (finite) precision of floating point values means there are floating point values x for which x + 1.0 yields the original value x. The values tend to be large, but exist for all real-world 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.
grumpy is offline   Reply With Quote
Old Jan 2nd, 2009, 7:53 AM   #6
lrh9
Professional Programmer
 
Join Date: Nov 2008
Posts: 336
Rep Power: 6 lrh9 is on a distinguished road
Re: Using the modulus operator with type double

Oh. Thank you for that.
lrh9 is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

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

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 7:38 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC