Programming Forums
User Name Password Register
 

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

Reply
 
Thread Tools Display Modes
Old Jan 11th, 2005, 11:34 AM   #1
HRahman
Newbie
 
Join Date: Jan 2005
Posts: 3
Rep Power: 0 HRahman is on a distinguished road
Modulus

Hello,

I'm trying to use the % for modulus but this works purely as a remainder operator. This does not work properly when you have non whole numbers, i.e. it won't calculate 25/4 % 7 correctly. Is there another operator which deals with this properly?

Thanks!

Hammamur.
HRahman is offline   Reply With Quote
Old Jan 11th, 2005, 11:51 AM   #2
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
Have you tried it with numbers larger than the modulo? I believe even something like 24/4 % 7 will return 0, because 6 is less than 7.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Jan 11th, 2005, 12:31 PM   #3
EdSalamander
Programmer
 
EdSalamander's Avatar
 
Join Date: Dec 2004
Location: Tucson, AZ, USA
Posts: 80
Rep Power: 4 EdSalamander is on a distinguished road
Send a message via AIM to EdSalamander
Actually, 25/4 % 7 should return 6. Because all the numbers you used here are int's, the dividing and the mod will automatically round down. So, 25/4 = 6 and 6%7 = 6. (6/7 = 0R6). If you're looking for a decimal answer, you need to change all your numbers to doubles, like so: 25.0/4.0 % 7.0 which will return 6.25. If you were looking for something else entirely, then you might have your formula set up wrong (or mod isn't what you need ). What result were you looking for exactly?
__________________
I can pick my friends. And I can pick my nose. So, why can't I pick my friend's nose?
EdSalamander is offline   Reply With Quote
Old Jan 11th, 2005, 2:23 PM   #4
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
Damnit... what was I thinking about...
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Jan 11th, 2005, 2:59 PM   #5
Mjordan2nd
The Supreme Ruler
 
Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6 Mjordan2nd is on a distinguished road
I believe when using modulus you must have a whole number.
__________________
"Every gun that is made, every warship launched, every rocket signifies, in the final sense, a theft from those who hunger and are not fed, from those who are cold and are not clothed. The world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children." - Dwight D. Eisenhower
Mjordan2nd is offline   Reply With Quote
Old Jan 11th, 2005, 4:03 PM   #6
EdSalamander
Programmer
 
EdSalamander's Avatar
 
Join Date: Dec 2004
Location: Tucson, AZ, USA
Posts: 80
Rep Power: 4 EdSalamander is on a distinguished road
Send a message via AIM to EdSalamander
Quote:
Originally Posted by Mjordan2nd
I believe when using modulus you must have a whole number.
Suprisingly no; not in Java at least. I just tried it with 23.1/7.2%2.3 and got 0.9083333333333337, so apparently it works with decimals.
__________________
I can pick my friends. And I can pick my nose. So, why can't I pick my friend's nose?
EdSalamander is offline   Reply With Quote
Old Jan 11th, 2005, 4:06 PM   #7
Mjordan2nd
The Supreme Ruler
 
Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6 Mjordan2nd is on a distinguished road
Heh. That's pretty cool. I had no idea you could do that with decimals.
__________________
"Every gun that is made, every warship launched, every rocket signifies, in the final sense, a theft from those who hunger and are not fed, from those who are cold and are not clothed. The world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children." - Dwight D. Eisenhower
Mjordan2nd is offline   Reply With Quote
Old Jan 11th, 2005, 4:15 PM   #8
EdSalamander
Programmer
 
EdSalamander's Avatar
 
Join Date: Dec 2004
Location: Tucson, AZ, USA
Posts: 80
Rep Power: 4 EdSalamander is on a distinguished road
Send a message via AIM to EdSalamander
Yeah, me neither, I'd was just guessing it would work 'till I tried it. :p

But anyway, the question remains, what values were you originally expecting, HRahman?
__________________
I can pick my friends. And I can pick my nose. So, why can't I pick my friend's nose?
EdSalamander is offline   Reply With Quote
Old Jan 11th, 2005, 5:40 PM   #9
HRahman
Newbie
 
Join Date: Jan 2005
Posts: 3
Rep Power: 0 HRahman is on a distinguished road
Hello,

Yes, should'nt be able to use non-whole number with mod (it doesn't make sense to). BUT yet Java allows you which is mathematically incorrect. What needs to happen is for the example for 25/4:

Convert this to 25 * 1/4

Now the inverse of 1/4 is 2, so therefore:

25 * 2 = 50 = 1 (mod 7)

Now, what I was hoping that Java provided a more surfisticated modulus operator to do all the above.

So, anyone no of one.

Thanks alot for all the replies!!!!

Hammamur.
HRahman is offline   Reply With Quote
Old Jan 11th, 2005, 8:01 PM   #10
EdSalamander
Programmer
 
EdSalamander's Avatar
 
Join Date: Dec 2004
Location: Tucson, AZ, USA
Posts: 80
Rep Power: 4 EdSalamander is on a distinguished road
Send a message via AIM to EdSalamander
Oh, I see, you want the real, mathematical modulus. Well, I think you're outta luck then. I don't know of any other operators that work with modulus.

Also, to be honest, I still can't really tell what you're trying to evaluate. Are you trying to show that 25 = 4 (mod 7) (and get a boolean result)? Or are you trying to find that the modulus is 7? Or are you tring to find the remainder that makes them congruent (4)? Outside of programming, mod isn't an operator at all, it's simply a label. Oh, and isn't 4 the inverse of 1/4 not 2?
__________________
I can pick my friends. And I can pick my nose. So, why can't I pick my friend's nose?
EdSalamander 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




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 11:00 PM.

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