Thread: dividing a multi-digit into single-digits

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    div() and ldiv() from <cstdlib> can be used to explicitly compute the quotient and remainder simultaneously (at least if the processor is capable of it, anyway).

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by robatino View Post
    div() and ldiv() from <cstdlib> can be used to explicitly compute the quotient and remainder simultaneously (at least if the processor is capable of it, anyway).
    Good point. I'm not aware of any integer divide-capable processor that doesn't also give the remainder as a side-effect - it is sort of trivial to calculate that when you do the divide - otherwise one would have to implement a separate instruction "mod" that does that, and it's not any more efficient. The same applies, by the way, to the more common versions of integer divide algorithms used on processors without hardware for divide.

    I tested with Visual Studio .Net, and in my test, the compiler did only one div instruction for both the divide and the modulo at once. I believe GCC does the same, which covers most code produced... :-)


    --
    Mats

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    Quote Originally Posted by robatino View Post
    div() and ldiv() from <cstdlib> can be used to explicitly compute the quotient and remainder simultaneously (at least if the processor is capable of it, anyway).
    There is the downside that there are versions of this function available only for certain types - div(), ldiv(), lldiv(), imaxdiv() for int, long, long long, and intmax_t, resp. (only the first two being standard C++). This means that if one wants to compute i/j and i%j where i and j are std::size_t, say, it might be better to just use i/j and i%j in close proximity and hope that it gets optimized. Is there a reason why there isn't a templated version in C++, similar to the way that functions from <cmath> such as sqrt() are templated in C++ for different floating-point types, even though in C they're only available for double?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. five digit number with different digits
    By khdani in forum C Programming
    Replies: 11
    Last Post: 05-03-2009, 11:33 AM
  2. Looking for constructive criticism
    By wd_kendrick in forum C Programming
    Replies: 16
    Last Post: 05-28-2008, 09:42 AM
  3. newbie programmer - needs help bad.
    By hortonheat in forum C Programming
    Replies: 17
    Last Post: 10-20-2004, 05:31 PM
  4. concatenating single chars to multi char arrays
    By TJJ in forum C Programming
    Replies: 7
    Last Post: 11-20-2003, 04:09 AM
  5. leading zero on single digit int
    By confuted in forum C++ Programming
    Replies: 2
    Last Post: 07-31-2003, 04:48 PM