Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Testing for integer result

Reply
Thread Tools

Testing for integer result

 
 
DavidW
Guest
Posts: n/a
 
      01-04-2011
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.


 
Reply With Quote
 
 
 
 
Vaclav Haisman
Guest
Posts: n/a
 
      01-04-2011
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
 
Reply With Quote
 
 
 
 
DavidW
Guest
Posts: n/a
 
      01-04-2011
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*


 
Reply With Quote
 
DavidW
Guest
Posts: n/a
 
      01-04-2011
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/Countdo...#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.


 
Reply With Quote
 
DavidW
Guest
Posts: n/a
 
      01-06-2011
Paavo Helde wrote:
> "DavidW" <(E-Mail Removed)> wrote in
> news:4d23a28f$0$18555$(E-Mail Removed):
>> Thanks. It's for a solver for a game show:
>> http://en.wikipedia.org/wiki/Countdo...#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.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
i = 10; result = ++i - --i; How result become ZERO Lakshmi Sreekanth C Programming 52 09-23-2010 07:41 AM
Re: i = 10; result = ++i - --i; How result become ZERO Mr. Buffoon C Programming 4 09-23-2010 03:01 AM
Is the result of valid dynamic cast always equal to the result ofcorrespondent static cast? Pavel C++ 7 09-18-2010 11:35 PM
simulation result is correct but synthesis result is not correct J.Ram VHDL 7 12-03-2008 01:26 PM
1. Ruby result: 101 seconds , 2. Java result:9.8 seconds, 3. Perl result:62 seconds Michael Tan Ruby 32 07-21-2005 03:23 PM



Advertisments