Testing for integer result

Discussion in 'C++' started by DavidW, Jan 4, 2011.

  1. DavidW

    DavidW Guest

    Hello,

    What is the most reliable way, or the standard way, of determining whether an
    integer division operation produces an integer result? I'm using this at
    present:

    long n1;
    long n2;

    // stuff
    ....

    // n1, n2 != 0
    if(n1/n2 == (double)n1/n2)
    {
    ....
    }

    I can't see how that can fail, excluding cases of overflow or underflow, but I
    thought I'd better ask. I'm using VC++ 6.0.
     
    DavidW, Jan 4, 2011
    #1
    1. Advertising

  2. DavidW wrote, On 4.1.2011 22:38:
    > Hello,
    >
    > What is the most reliable way, or the standard way, of determining whether an
    > integer division operation produces an integer result? I'm using this at
    > present:
    >
    > long n1;
    > long n2;
    >
    > // stuff
    > ...
    >
    > // n1, n2 != 0
    > if(n1/n2 == (double)n1/n2)
    > {
    > ...
    > }
    >
    > I can't see how that can fail, excluding cases of overflow or underflow, but I
    > thought I'd better ask. I'm using VC++ 6.0.

    Try std::fmod(), e.g. 'if (std::fmod (n1, n2) == 0)'. It is the floating
    point version of operator %.

    --
    VH
     
    Vaclav Haisman, Jan 4, 2011
    #2
    1. Advertising

  3. DavidW

    DavidW Guest

    Pete Becker wrote:
    > On 2011-01-04 16:38:24 -0500, DavidW said:
    >
    >> Hello,
    >>
    >> What is the most reliable way, or the standard way, of determining
    >> whether an integer division operation produces an integer result?

    >
    > Integer division operations always produce integer results.


    I meant a division of one integer value by another, in mathematics.

    >> I'm using this at present:
    >>
    >> long n1;
    >> long n2;
    >>
    >> // stuff
    >> ...
    >>
    >> // n1, n2 != 0
    >> if(n1/n2 == (double)n1/n2)
    >> {
    >> ...
    >> }

    >
    > If n1%n2 is zero, then n1 is an exact multiple of n2.


    Of course, and I've used that plenty of times before. It's been ages since I've
    needed to do it. Thanks.

    *embarrassed*
     
    DavidW, Jan 4, 2011
    #3
  4. DavidW

    DavidW Guest

    Vaclav Haisman wrote:
    > DavidW wrote, On 4.1.2011 22:38:
    >> Hello,
    >>
    >> What is the most reliable way, or the standard way, of determining
    >> whether an integer division operation produces an integer result?
    >> I'm using this at present:
    >>
    >> long n1;
    >> long n2;
    >>
    >> // stuff
    >> ...
    >>
    >> // n1, n2 != 0
    >> if(n1/n2 == (double)n1/n2)
    >> {
    >> ...
    >> }
    >>
    >> I can't see how that can fail, excluding cases of overflow or
    >> underflow, but I thought I'd better ask. I'm using VC++ 6.0.

    > Try std::fmod(), e.g. 'if (std::fmod (n1, n2) == 0)'. It is the
    > floating point version of operator %.


    Thanks. It's for a solver for a game show:
    http://en.wikipedia.org/wiki/Countdown_(game_show)#Numbers_round

    I think longs should have sufficient capacity for the actual solutions, but
    overflows could occur in trying all the possibilities, so I'll think about
    whether to use doubles everywhere.
     
    DavidW, Jan 4, 2011
    #4
  5. DavidW

    DavidW Guest

    Paavo Helde wrote:
    > "DavidW" <> wrote in
    > news:4d23a28f$0$18555$:
    >> Thanks. It's for a solver for a game show:
    >> http://en.wikipedia.org/wiki/Countdown_(game_show)#Numbers_round
    >>
    >> I think longs should have sufficient capacity for the actual
    >> solutions, but overflows could occur in trying all the possibilities,
    >> so I'll think about whether to use doubles everywhere.

    >
    > The long datatype is 32 bits with VC++. If this is not sufficient a
    > logical step would be to use a 64-bit integer type (called __int64 in
    > VC++6 IIRC). Modern compilers provide int64_t through <stdint.h>.
    > There are variants of stdint.h also usable with VC++6 (see the end of
    > http://en.wikipedia.org/wiki/Stdint.h).


    Yes, I've written it with a typedef to compile it with __int64. As it turns out,
    32-bit ints are enough. I didn't bother to calculate it out before. The maximum
    value with the restrictions imposed by the rules is 100*75*50*25*10*10 =
    937,500,000, which is less than 2^31. But I might use __int64 anyway so
    experiments can be done without restrictions except on the maximum for an item
    value.
     
    DavidW, Jan 6, 2011
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. J.Ram
    Replies:
    7
    Views:
    682
  2. Pavel
    Replies:
    7
    Views:
    564
    Pavel
    Sep 19, 2010
  3. Lakshmi Sreekanth

    i = 10; result = ++i - --i; How result become ZERO

    Lakshmi Sreekanth, Sep 21, 2010, in forum: C Programming
    Replies:
    52
    Views:
    1,238
    Nick Keighley
    Sep 23, 2010
  4. Mr. Buffoon
    Replies:
    4
    Views:
    355
    Eric Sosman
    Sep 23, 2010
  5. Michael Tan
    Replies:
    32
    Views:
    1,082
    Ara.T.Howard
    Jul 21, 2005
Loading...

Share This Page