module division the % sign

This is a discussion on module division the % sign within the C++ Programming forums, part of the General Programming Boards category; hey this isnt an exact C++ question but i wasnt sure where to put it im actually doing something for ...

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    255

    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.
    hooch

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    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.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    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.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  4. #4
    Registered User
    Join Date
    Nov 2001
    Posts
    255
    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.
    hooch

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    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.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    Registered User
    Join Date
    Nov 2001
    Posts
    255
    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
    hooch

  7. #7
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    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.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  8. #8
    Registered User
    Join Date
    Nov 2001
    Posts
    255
    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?
    Last edited by ssjnamek; 09-18-2006 at 04:30 PM.
    hooch

  9. #9
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    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).
    Last edited by Cat; 09-18-2006 at 04:46 PM.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  10. #10
    Registered User
    Join Date
    Nov 2001
    Posts
    255
    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?
    Last edited by ssjnamek; 09-18-2006 at 04:58 PM.
    hooch

  11. #11
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Since apparently you have no idea what the modulo operation is ...

    http://en.wikipedia.org/wiki/Modulo_operation
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  12. #12
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    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.
    Last edited by Cat; 09-18-2006 at 05:05 PM.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  13. #13
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Actually, in VB / is always floating point division. \ is integer division.

    I think this thread ought to end or be moved to tech.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  14. #14
    Registered User
    Join Date
    Nov 2001
    Posts
    255
    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
    hooch

  15. #15
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    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;
    }

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Find Injected DLLs In A Process?
    By pobri19 in forum Windows Programming
    Replies: 35
    Last Post: 02-06-2010, 08:53 AM
  2. Merge Module Problems
    By mercury529 in forum Windows Programming
    Replies: 0
    Last Post: 11-29-2006, 02:30 PM
  3. My own itoa()
    By maxorator in forum C++ Programming
    Replies: 18
    Last Post: 10-15-2006, 11:49 AM
  4. Function basics
    By sjleonard in forum C++ Programming
    Replies: 15
    Last Post: 11-21-2001, 11:02 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21