# module division the % sign

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 09-18-2006
ssjnamek
module division the % sign
hey this isnt an exact C++ question but i wasnt sure where to put it im actually doing something for visual basic for school but the principle should be the same

im trying to get the remainder of something im dividing and i thought it was the use of the % sign or MOD in vb but ive noticed MOD isnt giving back the remainder

i noticed google's calculter doesnt do it either

so how in programming would i get back the remainder i thought it was the module division operator?

250000 % 507 gives back 49 but if you divide it by 507 is 493.0966 etc. why is this? since mod or % should work the same in all langauges i figure its related C++ enough and this board gets the most looks so sorry if its not about C++ directly maybe but it could be.
• 09-18-2006
CornedBee
Huh?

Integer division: 250000 / 507 = 493, 49 remaining.
Floating point division: 250000 / 507 = 493.0966..., with some fractional remainder that cannot be represented in limited precision.
• 09-18-2006
Cat
250,000 / 507 = 493.blah

250,000 - (493*507) = 49

So 250,000 / 507 = 493 remainder 49, which is what your tools are telling you.
• 09-18-2006
ssjnamek
well i thought mod should give you 493 back not 49 i have no clue where it got 49 from

so how would i get the 493 back and no decimal points? integer division i thought but that rounded it so it was inaccurate.
• 09-18-2006
CornedBee
The point of integer division is that it rounds.

If the question is really about VB as it somewhat seems (what on earth are you doing on cboard then? Go to www.vbforums.com ), you need to use \ instead of / for proper integer division.
• 09-18-2006
ssjnamek
ah well ill check that out thanks just google gives back every and anything on visual basic and this is the only good programming board i know period
• 09-18-2006
Cat
In C++ or VB, (250,000 / 507) gives 493, the quotient.
(250,000 % 507) gives 49, the remainder.

That is, if you had 250,000 objects and you put them in piles of 507, you'd have 493 piles and you'd have 49 objects left over.
• 09-18-2006
ssjnamek
but 250 000 isnt an even divide into 49

so why doesnt it give a decimal return it does on a normal calculator?

oh you were talking about integer division ya thats not what i want

but that 49 where does that come from i guess i dont understand mod i thought i did.

so theres no way to get that 493 without fear of it rounding up by using integer division?
• 09-18-2006
Cat
Quote:

Originally Posted by ssjnamek
but 250 000 isnt an even divide into 49

so why doesnt it give a decimal return it does on a normal calculator?

oh you were talking about integer division ya thats not what i want

but that 49 where does that come from i guess i dont understand mod i thought i did.

so theres no way to get that 493 without fear of it rounding up by using integer division?

Integer division NEVER rounds up. Only down. In fact, you should not consider it rounding -- it truncates the result.

9/10 = 0 in C++.

Here is how to think of remainder and quotient:

Imagine you are going to feed 200 guests. You are going to give them ice cream after dinner. You know that the ice cream you bought is enough for 750 scoops.

So, you will give each person 3 scoops. That is the quotient; that is how much each person gets.

Now, you've given 600 total scoops of ice cream out. That means you still have 150 scoops left over, because you started with 750 and you gave away 600. This is the remainder; this is how much is left over after the division is done.

In C++:

750 / 200 = 3; (this gives you the quotient)
750 % 200 = 150 (this gives you the remainder).

750.0 / 200.0 = 3.75 (this is the decimal division).
• 09-18-2006
ssjnamek
ah i see ill be sure to have fun with the module division later it looks to be interesting. thanks

so how can i get just the 3 without fear of any rounding

because 250000 / 507 rounds down just fine cause its 493.0blah

but 9000/507 is 17.75blah and that rounds to 18

is there a way to knock that down to 17 and get just the whole number without rounding

this is for school and our program has to get how many gallons of paint we need so its taking the area and dividing it by 507 in this case since each gallon covers 507. so i obviously do not need the decimal points.

wait never mind found out .net has a math.ceiling and match.floor function and realized wait a minute i do need to round up. slight overlook. though is there a similar function to this in c++ non .net?
• 09-18-2006
CornedBee
Since apparently you have no idea what the modulo operation is ...

http://en.wikipedia.org/wiki/Modulo_operation
• 09-18-2006
Cat
Quote:

Originally Posted by ssjnamek
but 9000/507 is 17.75blah and that rounds to 18

Not in C++, and it shouldn't in VB either. Although to do integer division in VB you'd type 9000 \ 507 (the slash goes the other way for integer division).

In C++, integer math DOES NOT ROUND. If decimal division would produce1.995, integer division would produce 1.
• 09-18-2006
CornedBee
Actually, in VB / is always floating point division. \ is integer division.

I think this thread ought to end or be moved to tech.
• 09-18-2006
ssjnamek
oh i always thought the math worked same from langauge to langauge since well its math it cant vary much or thatd be one too many headaches in the development world. much less newcomers or just imagine the chaos is countries did math differently its already bad enough with metric and non metric which i always found stupid to begin with.

but ill look at that link while i eat dinner hamburgers yummy

thanks
• 09-18-2006
swoopy
Quote:

wait never mind found out .net has a math.ceiling and match.floor function and realized wait a minute i do need to round up. slight overlook. though is there a similar function to this in c++ non .net?
Yes, ceil and floor. To use these include <cmath>. For example:
Code:

```#include <iostream> #include <cmath> using namespace std; int main() {   cout << (double) 9000/507 << endl;   cout << ceil( (double) 9000/507 ) << endl;   cout << floor( (double) 9000/507 ) << endl; }```
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last