How does the result of 1 % 2 = 1? I get that with 3 % 2, two goes in once and one is left over. But two, however, doesn't go into 1 once it goes in 0. Is the reminder the 1 it didn't divide into?
How does the result of 1 % 2 = 1? I get that with 3 % 2, two goes in once and one is left over. But two, however, doesn't go into 1 once it goes in 0. Is the reminder the 1 it didn't divide into?
"...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson
1/2=0.5 rounded up to 1
3/2=1.5 drop the 1 and .5 gets rounded up to 1
Last edited by stahta01; 12-03-2017 at 08:20 PM.
"...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson
point whatever is the marking of the remainder of division. 5 and up gets rounded up to the next highest number, that is basic math. but too the 5 can be argued to get rounded down too, that is left up to whomever, and you know as well as I do thier is no real editing of ones post after how long?
Code:int main() { printf("1 % 2 = %d\n", 1%2); printf("3 % 2 = %d\n", 3%2); printf("4 % 3 = %d\n", 4%3); printf("1 % 3 = %d\n", 1%3); return 0; }Code:$ ./term2 1 % 2 = 1 3 % 2 = 1 4 % 3 = 1 1 % 3 = 1same basic logic if it has a remainder it is round up to 1 or as they put it elevated to 1.would evaluate to 1 because 5 divided by 2 leaves a quotient of 2 and a remainder of 1, while "9 mod 3" would evaluate to 0 because the division of 9 by 3 has a quotient of 3 and leaves a remainder of 0;
Last edited by userxbw; 12-03-2017 at 08:47 PM.
That's not the correct logic to follow. It may work for 1%2, but what about 5%3 or 15%6. Using the way you rationalize it, 5%3 would be 1 because 5/3 is 1.666, drop the one, 0.666 gets rounded up to 1... But 5%3 is 2, not 1.
There is a way to make it work though, if you're interested. The remainder is formally described as (dividend - quotient*divisor). In programming terms( not C or C++ ):
Code:rem = a - floor(a/b)*b
Devoted my life to programming...
well the logic they use it looks to me that because it is above 5, because it is 6 so it gets elevated to 2, so on and so forth. Looks to me that this is just a made up system to deal with fractions. so their has to be some kind of logic behind it to eliminate the actual fractions, making them whole numbers instead.
I mean you're very correct that you won't have to do fractions with modulus, but I think it can be clearer than that.
It would probably help to mod by the same divisor so we start to see a pattern, and it feels less made up...
1 mod 5 = 1
6 mod 5 = 1
7 mod 5 = 2
8 mod 5 = 3
10 mod 5 = 0
Dividends less than divisor will result in the dividend as the result. Multiples of the divisor will be 0. After that, you basically can just divide and the result will be a remainder. A place where most people see modular arithmetic in the everyday is the 12 hour clock, because we go past the twelfth hour once every day and start over, so that might help it seem realistic as well.
Yet another way to look at it that should work in all situations, like with negative numbers: C99 demands that a/b * b + a%b must result in a. It makes sense logically, right? I mean as long as we remember that we're doing integer division all of our examples work.
1/5 * 5 + 1%5 = 0 * 5 + 1%5 = 0 + 1 = 1
6/5 * 5 + 6%5 = 1 * 5 + 6%5 = 5 + 1 = 6
7/5 * 5 + 7%5 = 1 * 5 + 7%5 = 5 + 2 = 7
8/5 * 5 + 8%5 = 1 * 5 + 8%5 = 5 + 3 = 8
10/5 * 5 + 10%5 = 2 * 5 + 10%5 = 10 + 0 = 10
Substitute numbers as you please.
Last edited by whiteflags; 12-04-2017 at 06:38 PM.