Thread: modulo a size_t

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    108

    modulo a size_t

    Hi, a curious question. Why is the value of an int % a size_t strange?


    Code:
    #include <iostream>
    #include <math.h>
    #include <vector>
    using namespace std;
    
    
    int main(){
    
       vector<int> somevec;
       somevec.push_back(0);
       somevec.push_back(0);
       somevec.push_back(0);
       somevec.push_back(0);
       somevec.push_back(0);
       somevec.push_back(0);
    
       int moo = -1 % 6;
       int boo = -1 % somevec.size();
       int zoo = -1 % (int)(somevec.size());
       
       
       cout << "mm : " << moo << endl;
       cout << "m2 : " << boo << endl;
       cout << "m3 : " << zoo << endl;
    
       
    }
    They should be the same.. right?

    Cheers

  2. #2
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    I get
    Code:
    mm : -1
    m2 : 3
    m3 : -1
    For those of you who don't want to run the code. size() is unsigned, in contrast to the other types shown. That'll all I've got.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  3. #3
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    When you operate on a signed and an unsigned, the signed argument is converted to unsigned. So -1 is converted to unsigned int, value 4294967295.

    4294967295 % 6 == 3.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    When you do arithmetic with mixed types, one value is cast to some other type according to some rules. If one operand is signed and the other unsigned, the signed value is cast to unsigned.

    And a hint:

    Code:
       vector<int> somevec(6);
       //somevec.push_back(0);
       //somevec.push_back(0);
       //somevec.push_back(0);
       //somevec.push_back(0);
       //somevec.push_back(0);
       //somevec.push_back(0);
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    108
    ahh, I see. Didn't think of that. Thanks

    p.s I was thinking of putting vector.resize() but I guess I'd still miss that constructor thing.. heh

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with modulo calculation
    By manutdfan in forum C Programming
    Replies: 6
    Last Post: 01-12-2009, 03:37 PM
  2. Replies: 7
    Last Post: 06-01-2008, 07:47 AM
  3. modulo 2 modulus
    By breik in forum C Programming
    Replies: 5
    Last Post: 03-24-2006, 02:30 PM
  4. Modulo on decimal numbers
    By (Slith++) in forum C++ Programming
    Replies: 3
    Last Post: 12-17-2005, 03:29 AM
  5. modulo 2 division question help
    By shaq8u in forum C Programming
    Replies: 9
    Last Post: 08-20-2003, 08:37 AM