Game Development Community

Using the % modulo in math in script

by J Sears · in Torque Game Builder · 04/18/2007 (11:18 am) · 5 replies

Basically I have this code set up

%result = $a / $b;
%leftOver = $a % $b;
%resultTwo = %result - %leftOver;

now I have echos after each of these and to help explain what I'm doing (it's part of my work in progress AI for playing cards) %a is the round, each round more cards get dealt out, %b is the number of players so it stays constant for a whole game is just defined at the start of a new game. To help in my calculating of how much the AI should bid on his hand I want the average amount per player.

So when a and b are both 3 the result is 1, leftover is 0 and resultTwo is 1. That part makes sense to me and is how I expected it to work

when a is 4 and b is 3, result is 1.3333333 which is fine but leftOver is 1 making resultTwo .333333

I though % was supposed to result in the remainder so leftOver would be .33333 so that resultTwo would be 1.

Now I believe (my c/c++ coding is limited) that in C/C++ you can force variables to be INT, I'm not sure if that works in script or not but going to give it a shot. But even if I get it to work trying forcing of INT I'm still confused as to why leftOver is 1.

#1
04/18/2007 (12:01 pm)
4 % 3 == 1 because 3 goes into 4 once with 1 left over. Much like 7 % 3 would also equal 1 because 3 goes into seven twice with 1 left over. Or, 21 % 3 == 0 because 3 goes into 21 seven times leaving no remainder.
#2
04/18/2007 (12:32 pm)
I was certainly thinking about % the wrong way then. Since the remainder ends up as the decimal portion of the number I figured it just grabbed the decimal.

then is there a command for getting the decimal part, I am trying to end up with whole numbers.
#3
04/18/2007 (12:41 pm)
Well I just did it this way, thanks for the correction on how % worked gary

%result = $a / $b;
%leftOver = $a % $b;
%decimal = %leftover / $b
%resultTwo = %result - %decimal

and that does what I want in a round about way.
#4
04/19/2007 (12:06 pm)
If you want to do something like 4/3 = 1 instead of 1.333 you can do mFloor(4/3), which gives you the highest integer thats below or equal the number you passed as a parameter.

If you want to do it at hand, I guess the easiest way is:

%leftOver = %a % %b;
%result = (%a - %leftOver) / %b;
#5
04/19/2007 (12:29 pm)
MFloor looks promising for what I want it to do I never would have found that. I will try that out