Jump to content
  • Advertisement
Sign in to follow this  
Jazonxyz

Basic Math Problem

This topic is 3813 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

How can I tell if a certain number is a divisible of another number? for Example, How can I tell if 32 is a divisible of 4 and 33 isn´t? I´m using C++

Share this post


Link to post
Share on other sites
Advertisement
bool divisible(int dividend, int divisor) {
if (dividend%divisor==0) {
return true;
} else {
return false;
}
}


The modulus % operator returns the remainder of a division problem.

Share this post


Link to post
Share on other sites
Quote:
Original post by Lawl_Rock
bool divisible(int dividend, int divisor) {
if (dividend%divisor==0) {
return true;
} else {
return false;
}
}


The modulus % operator returns the remainder of a division problem.


Or just:
bool isDivisible(int dividend, int divisor)
{
return dividend%divisor == 0;
}


:)

Share this post


Link to post
Share on other sites
Quote:
Original post by Lawl_Rock
I love shorter ways to implement things.

Then prepare to have a crush on this:

bool isDivisible(int dividend, int divisor)
{
return !(dividend%divisor);
}

Share this post


Link to post
Share on other sites
But is 5 divisible by 0? Since 5/0 is undefined, I would say it is not.

bool isDivisible(int divident, int divisor)
{
return (divisor != 0) && (divident % divisor == 0);
}

or shorter and a bit more cryptic

bool isDivisible(int x, int y)
{
return y && !(x % y);
}

Share this post


Link to post
Share on other sites
Also,

if I devide an an int and I get a number with a decimal point what happens, does the compiler round up to the nearest whole number or does it just ignore the decimal point?

Share this post


Link to post
Share on other sites
Quote:
Original post by Jazonxyz
Also,

if I devide an an int and I get a number with a decimal point what happens, does the compiler round up to the nearest whole number or does it just ignore the decimal point?


With integer division, the part after the decimal point is ignored. For example, 8 / 5 == 1. No rounding is done.

Share this post


Link to post
Share on other sites

#include <iostream>
#include <iomanip>

void test(int x, int y)
{
std::cout << std::setw(4) << x << " / " << std::setw(4) << y << " = " << std::setw(4) << (x / y) << " --- " << std::setw(5) << (float(x) / float(y)) << std::endl;
}

int main()
{
test(9, 4);
test(-9, 4);
test(9, -4);
test(-9, -4);
}


prints

9 / 4 = 2 --- 2.25
-9 / 4 = -2 --- -2.25
9 / -4 = -2 --- -2.25
-9 / -4 = 2 --- 2.25

Meaning it drops the part after the decimal point when dividing integers.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!