Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

How to check if an integer is a multiple of a number?

Gattz Gilman
Banned from RealLife :/
Join date: 29 Feb 2004
Posts: 316
09-16-2004 12:24
i have the machine so that you pay 50L$ and i want to know how to check if the amount paid is an interval of 50L$?
this is what i have:
CODE

integer int;
if(amount / 50 == int)
{
//I am an interval of 50
}
if(amount / 50 != int)
{
//I am not an interval of 50
}


and i just tested it, and it does not work. can anyone help me?
_____________________
Ace Cassidy
Resident Bohemian
Join date: 5 Apr 2004
Posts: 1,228
09-16-2004 12:33
Use the modulus (%) operator. This returns the remainder after performing integer division.

So your code would be :

CODE

if ( (amount % 50) == 0 )
{
// amount IS a multiple of 50
}
else
{
// amount IS NOT a multiple of 50
}


- Ace
_____________________
"Free your mind, and your ass will follow" - George Clinton
Gattz Gilman
Banned from RealLife :/
Join date: 29 Feb 2004
Posts: 316
09-16-2004 12:40
ty :)
_____________________
Pat Murphy
The Wandering Wizard
Join date: 2 Dec 2002
Posts: 142
09-16-2004 12:56
you can't do type checking like that. For one, if you divide two integers you get an integer, it just drops the fraction. Second there are no type checking functions, so the best way to do it is to convert everything to floats, isolate the fraction part, and see if it is not 0, the version here creates a remainder, which isn't perfect because of precision errors, but you should get the idea.

CODE

integer CONST = 50;

integer Rem(integer a, integer b)
{
float q = ((float) a) / ((float) b);
llWhisper(0, "q = " + (string) q);
return (integer) ((q - ((integer) q)) * b);
}

default
{
state_entry()
{
llListen(0, "", NULL_KEY, "");
}

listen(integer chan, string name, key id, string message)
{
integer r = Rem((integer) message, CONST);
if(r == 0)
{
llSay(0, "Is a multiple of " + (string) CONST);
}
else
{
llSay(0, "Is not a multiple of " + (string) CONST + ". Remainder: " + (string) r);
}
}
}
_____________________
That's how they showed their respect for Paddy Murphy
That's how they showed their honour and their pride;
They said it was a sin and shame and they winked at one another
And every drink in the place was full the night Pat Murphy died.
-Great Big Sea
Pat Murphy
The Wandering Wizard
Join date: 2 Dec 2002
Posts: 142
09-16-2004 12:59
doh, I was too slow. I forgot we could do modulus :-p I was looking for a function to do it.
-Pat Murphy
_____________________
That's how they showed their respect for Paddy Murphy
That's how they showed their honour and their pride;
They said it was a sin and shame and they winked at one another
And every drink in the place was full the night Pat Murphy died.
-Great Big Sea