Programming Forums
User Name Password Register
 

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

Reply
 
Thread Tools Display Modes
Old Nov 30th, 2005, 3:17 AM   #11
bl00dninja
Programming Guru
 
bl00dninja's Avatar
 
Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 6 bl00dninja is on a distinguished road
you can use integer division and modulus to break numbers apart. a classic example would be to convert decimal to binary. another example would be if you wanted to "make change". a solution rather easy for the human brain...how many coins of each type would you give to someone who needed 41 cents back in change? for those of us who've "worked a register" we all know it's one of each. one quarter, one dime, one nickel, and one penny. besides being the key to data encryption...modulus can help you divide numbers like you divide strings. in review...it's the REMAINDER after integer division. if you divide 1 by 2...2 goes into 1 zero times...leaving a remainder of two. it's a rather magical function. learn to use it.

as to "what can be evenly divided by what" let's restate, the syntax is: (for C/C++)

#include <iostream>
using namespace std;

int main(){
    
    int x, y;
    
    cout<<"enter 2 numbers"<<endl;
    cin>>x>>y;

    if (x % y ==0)
    {
        cout<<"yay!  this shit is true!!!"<<endl;
    }

     if (x % y != 0)
    {
        cout<<"crap!!! it's false!!!"<<endl;
    }
    
    system("pause");
}
__________________
i put on my robe and wizard hat...

Have you ever heard of Plato, Aristotle, Socrates?...Morons.
bl00dninja is offline   Reply With Quote
Old Nov 30th, 2005, 4:27 AM   #12
kurifu
Expert Programmer
 
kurifu's Avatar
 
Join Date: Jul 2004
Location: Halifax, Nova Scotia (Canada)
Posts: 784
Rep Power: 5 kurifu is on a distinguished road
Send a message via ICQ to kurifu Send a message via MSN to kurifu
Most easily said, modulus return the remainder after division. Remember how you used to do division in grade 3 and 4, they wouldn't make you calculate all those decimals, instead you just specific the remainder... so yup, it is that simple.
__________________
Clifford Matthew Roche &lt;geek@cliffordroche.com&gt;
Web Hosting: http://www.crd-hosting.com
Consulting: http://www.crdev-consulting.com
kurifu is offline   Reply With Quote
Old Nov 30th, 2005, 5:03 AM   #13
SittingDuck
Programmer
 
SittingDuck's Avatar
 
Join Date: Nov 2005
Location: Moseley, Birmingham, England, Earth
Posts: 51
Rep Power: 4 SittingDuck is on a distinguished road
Quote:
Originally Posted by coldDeath
Kilo, my favourite usage for the modolus is when finding put if a number is even or odd.

Just do a quick divide by 2, then if it returns 0, its an even number
Division can take up to 44 ticks. And can take up to three. Just check the least significant bit, it's much quicker.
SittingDuck is offline   Reply With Quote
Old Nov 30th, 2005, 9:10 AM   #14
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Let's see...post 12 repeats posts 2 and 5 and post 13 repeats post 9. Next?
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Nov 30th, 2005, 11:33 AM   #15
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
Post 15 repeats post 14. HAH you didn't see that one coming, did you?
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for."
-- Socrates
nnxion is offline   Reply With Quote
Old Nov 30th, 2005, 12:29 PM   #16
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
We finally introduced recursion into the thread .
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Nov 30th, 2005, 2:57 PM   #17
Munsta
Newbie
 
Join Date: Nov 2005
Posts: 9
Rep Power: 0 Munsta is on a distinguished road
:p
Quote:
Today 12:29 PM
DaWei We finally introduced recursion into the thread .
Today 11:33 AM
nnxion Post 15 repeats post 14. HAH you didn't see that one coming, did you?
Today 09:10 AM
DaWei Let's see...post 12 repeats posts 2 and 5 and post 13 repeats post 9. Next?
Today 05:03 AM
SittingDuck
Quote:
Originally Posted by coldDeath
Kilo, my favourite usage for the modolus is when finding put if a number is even or odd.

Just do a quick divide by 2, then if it returns 0, its an even number


Division can take up to 44 ticks. And can take up to three. Just check the least significant bit, it's much quicker.
Today 04:27 AM
kurifu Most easily said, modulus return the remainder after division. Remember how you used to do division in grade 3 and 4, they wouldn't make you calculate all those decimals, instead you just specific the remainder... so yup, it is that simple.
Today 03:17 AM
bl00dninja you can use integer division and modulus to break numbers apart. a classic example would be to convert decimal to binary. another example would be if you wanted to "make change". a solution rather easy for the human brain...how many coins of each type would you give to someone who needed 41 cents back in change? for those of us who've "worked a register" we all know it's one of each. one quarter, one dime, one nickel, and one penny. besides being the key to data encryption...modulus can help you divide numbers like you divide strings. in review...it's the REMAINDER after integer division. if you divide 1 by 2...2 goes into 1 zero times...leaving a remainder of two. it's a rather magical function. learn to use it.

as to "what can be evenly divided by what" let's restate, the syntax is: (for C/C++)

Code:

#include <iostream> using namespace std; int main(){ int x, y; cout<<"enter 2 numbers"<<endl; cin>>x>>y; if (x % y ==0) { cout<<"yay! this shit is true!!!"<<endl; } if (x % y != 0) { cout<<"crap!!! it's false!!!"<<endl; } system("pause"); }

Today 02:32 AM
coldDeath Good point, lectricpharaoh, i forgot all about that.

But if i'm correct, the best what in Python is using the modolus. But maybe i'm wrong.
Today 02:21 AM
lectricpharaoh
Quote:
Originally Posted by coldDeath
Kilo, my favourite usage for the modolus is when finding put if a number is even or odd.

Just do a quick divide by 2, then if it returns 0, its an even number
I prefer value & 1 for this, especially in tight loops (for example, it is the test condition).

@Kilo: Remember that modulus is a component of integer division. When using numbers in floating-point form (actually, any number capable of representing fractional amounts), the idea of a remainder doesn't make sense. As such, using it for float and double values will cause a compile-time error.
Today 02:18 AM
coldDeath Kilo, my favourite usage for the modolus is when finding put if a number is even or odd.

Just do a quick divide by 2, then if it returns 0, its an even number
Yesterday 09:14 PM
DaWei Here is example usage from a recent thread.
Yesterday 08:33 PM
Kilo thanks man!
Yesterday 08:29 PM
Jessehk modulus is simply the remainder.

*C/C++ syntax for modulus used (%)*

eg:

Code:

4 % 2 = 0



that is because 4 is evenly divided by 2, which gives a remainder of 0

Code:

20 % 3 = 2



3 * 6 = 18

20 - 18 = 2

the remainder is 2.
hope that helps
Yesterday 06:26 PM
DaWei The term is "evenly divisible", but the answer is "yes".
Yesterday 06:24 PM
Kilo so i would test... and if the remainder is 0? then my constant was divisible by the given number?
Yesterday 05:49 PM
DaWei Modulus version division. Both, successively, often required.
Yesterday 05:38 PM
Kilo can someone please explain to me how to use a modulus operater properly?

lets i have a number... and i want to divide it by another number to test if it divisible. I was told modulus would do that for me?


EDIT: disregard "recursion" being used in the topic
Munsta is offline   Reply With Quote
Old Nov 30th, 2005, 3:37 PM   #18
MBirchmeier
Hobbyist Programmer
 
Join Date: Oct 2005
Posts: 211
Rep Power: 4 MBirchmeier is on a distinguished road
Quote:
Originally Posted by MBirchmeier
Quote:
Originally Posted by MBirchmeier
Quote:
Originally Posted by MBirchmeier
Quote:
Originally Posted by MBirchmeier
Quote:
Originally Posted by MBirchmeier
Quote:
Originally Posted by MBirchmeier
Quote:
Originally Posted by MBirchmeier
Quote:
Originally Posted by MBirchmeier
Quote:
Originally Posted by MBirchmeier
Quote:
Originally Posted by MBirchmeier
Quote:
Originally Posted by MBirchmeier
True Recursion
-MBirchmeier
(my appologies to all)
MBirchmeier is offline   Reply With Quote
Old Nov 30th, 2005, 3:42 PM   #19
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
ROFL, it's pretty funny Mbirchmeier, and looks nice as well
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for."
-- Socrates
nnxion is offline   Reply With Quote
Old Nov 30th, 2005, 4:04 PM   #20
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Looks like one of the TO posts, MB .
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei 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 9:28 PM.

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