Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

Stupid inequality!!

Rolig Loon
Not as dumb as I look
Join date: 22 Mar 2007
Posts: 2,482
01-23-2010 14:28
Here's a really simple function that I wrote because I've been scaling a set of numbers for graphing.

CODE

float tempnum;

integer nexthighest(integer num)
{
integer i=1;
while(num/i >= 10)
{
i = i * 10;
tempnum = num/i;
}
return ((integer)(tempnum+1) * i);
}


If I feed it, say, the number 45, it spits back 50. If I give it 6789, it gives me 7000. That's what it's supposed to do. I decided to put in a failsafe, just in case I fed it a number too large to handle, so I modified the function to look like this .....

CODE

integer nexthighest(integer num)
{
if (num < 10000000000)
{
integer i=1;
while(num/i >= 10)
{
i = i * 10;
tempnum = num/i;
}
return ((integer)(tempnum+1) * i);
}
else
{
llSay(0,"Number is out of range.");
tempnum = 0;
return ((integer)tempnum);
}
}


Now, if I feed it 45, or 6789, or ANY positive integer, it gives me the error message, "Number is out of range." However, if I reverse the inequality and test for num > 10000000000, it works fine.

I feel so dumb. What am I missing here? :confused:
_____________________
It's hard to tell gender from names around here but if you care, Rolig = she. And I exist only in SL, so don't ask.... ;)

Look for my work in XStreetSL at
Dora Gustafson
Registered User
Join date: 13 Mar 2007
Posts: 779
01-23-2010 14:36
We only have 32 bits integers: (2^33)-1=8589934591<10000000000
actually the highest positive integer is smaller:2147483647
http://lslwiki.net/lslwiki/wakka.php?wakka=integer
_____________________
From Studio Dora
Rolig Loon
Not as dumb as I look
Join date: 22 Mar 2007
Posts: 2,482
01-23-2010 15:11
OK.... I had momentarily forgotten that. :o Yup... changing that by a couple of orders of magnitude makes it work like a charm. Thanks, Dora.
_____________________
It's hard to tell gender from names around here but if you care, Rolig = she. And I exist only in SL, so don't ask.... ;)

Look for my work in XStreetSL at
SuezanneC Baskerville
Forums Rock!
Join date: 22 Dec 2003
Posts: 14,229
01-23-2010 15:15
What is your function doing?
_____________________
-

So long to these forums, the vBulletin forums that used to be at forums.secondlife.com. I will miss them.

I can be found on the web by searching for "SuezanneC Baskerville", or go to

http://www.google.com/profiles/suezanne

-

http://lindenlab.tribe.net/ created on 11/19/03.

Members: Ben, Catherine, Colin, Cory, Dan, Doug, Jim, Philip, Phoenix, Richard,
Robin, and Ryan

-
Void Singer
Int vSelf = Sing(void);
Join date: 24 Sep 2005
Posts: 6,973
01-23-2010 15:20
return (integer)((num / 10.0) + 0.9375) * 10;

rounds up to the nearest factor of 10, -20 --> -1

you can multiply 0.9375 by (1 | num >> 31), if you want all numbers rounded away from zero, or multiply 10 by (num > 0) to fix all negatives to 0.
_____________________
|
| . "Cat-Like Typing Detected"
| . This post may contain errors in logic, spelling, and
| . grammar known to the SL populace to cause confusion
|
| - Please Use PHP tags when posting scripts/code, Thanks.
| - Can't See PHP or URL Tags Correctly? Check Out This Link...
| -
Rolig Loon
Not as dumb as I look
Join date: 22 Mar 2007
Posts: 2,482
01-23-2010 16:16
From: Void Singer
return (integer)((num / 10.0) + 0.9375) * 10;

rounds up to the nearest factor of 10, -20 --> -1

you can multiply 0.9375 by (1 | num >> 31), if you want all numbers rounded away from zero, or multiply 10 by (num > 0) to fix all negatives to 0.

Now THAT's cool to know. I wouldn't have thought of that. Thanks.
_____________________
It's hard to tell gender from names around here but if you care, Rolig = she. And I exist only in SL, so don't ask.... ;)

Look for my work in XStreetSL at
Rolig Loon
Not as dumb as I look
Join date: 22 Mar 2007
Posts: 2,482
01-23-2010 16:23
From: SuezanneC Baskerville
What is your function doing?

If I have a set of values to graph, I was annoyed by the fact that my graph always set the maximum on the axis equal to the largest value in the set instead of something nice and round. If the largest value was 78, for example, I wanted to set the maximum on the axis to 80 for graphing purposes. It makes it easier to get tick marks at places that don't look totally stupid. Hence, the function. Now that it works (Thank you again, Dora), it looks like this ....

CODE

float tempnum;

integer nexthighest(integer num)
{
if (num < 100000000 & num > 0)
{
if (num <= 10)
{
return (10);
}
else
{
integer i=1;
while(num/i >= 10)
{
i = i * 10;
tempnum = num/i;
}
return ((integer)(tempnum+1) * i);
}
}
else
{
llSay(0,"Number is out of range.");
tempnum = 0;
return ((integer)tempnum);
}
}
_____________________
It's hard to tell gender from names around here but if you care, Rolig = she. And I exist only in SL, so don't ask.... ;)

Look for my work in XStreetSL at
SuezanneC Baskerville
Forums Rock!
Join date: 22 Dec 2003
Posts: 14,229
01-23-2010 16:28
I had a feeling that there was some cool way to do what you wanted done such as the one Void suggested, not that I had the slightest idea what it was. I was thinking about logarithms and exponents and such.
_____________________
-

So long to these forums, the vBulletin forums that used to be at forums.secondlife.com. I will miss them.

I can be found on the web by searching for "SuezanneC Baskerville", or go to

http://www.google.com/profiles/suezanne

-

http://lindenlab.tribe.net/ created on 11/19/03.

Members: Ben, Catherine, Colin, Cory, Dan, Doug, Jim, Philip, Phoenix, Richard,
Robin, and Ryan

-
Rolig Loon
Not as dumb as I look
Join date: 22 Mar 2007
Posts: 2,482
01-23-2010 17:10
Yeah, that was my first inclination too, but my mind is too brute force for fiddling with exponents for a silly task like this that should only need simple arithmetic. I do like Void's method, though. I wish I could think of things like that. :p
_____________________
It's hard to tell gender from names around here but if you care, Rolig = she. And I exist only in SL, so don't ask.... ;)

Look for my work in XStreetSL at
Void Singer
Int vSelf = Sing(void);
Join date: 24 Sep 2005
Posts: 6,973
01-23-2010 18:31
From: Rolig Loon
Yeah, that was my first inclination too, but my mind is too brute force for fiddling with exponents for a silly task like this that should only need simple arithmetic. I do like Void's method, though. I wish I could think of things like that. :p

habit of seeing them so often... integer math is all about abusing numbers

simple way to look at it,take your normal range of values, what theyd be if you divided them by your segment normally, and then add just enough to bump them over the next value (or under the previous), and flatten it by converting to integer.

you can also do the same trick with modulus, get the portion you are over the last segment, subtract it from the segment, and add the result back

num + (10 - num % 10) //-- ps this is probably faster

or round down to your segment
num - num % 10

so how do you come up with these imaginative methods of pushing the numbers around?

don't look at the number itself, look at how much it needs to change by to get the result you want from it.
_____________________
|
| . "Cat-Like Typing Detected"
| . This post may contain errors in logic, spelling, and
| . grammar known to the SL populace to cause confusion
|
| - Please Use PHP tags when posting scripts/code, Thanks.
| - Can't See PHP or URL Tags Correctly? Check Out This Link...
| -
Indeterminate Schism
Registered User
Join date: 24 May 2008
Posts: 236
01-25-2010 18:59
I was going to pretend this wasn't just for the fun of obfuscation but, yeah, ok, I wouldn't lie to you, this is just for the fun of obfuscation:

return (integer) (((string) ((integer) llGetSubString((string) Thing, 0, 0)) + 1) + llGetSubString("000000000000", 0, llStringLength((string) Thing) - 1));
Void Singer
Int vSelf = Sing(void);
Join date: 24 Sep 2005
Posts: 6,973
01-25-2010 19:41
From: Indeterminate Schism
I was going to pretend this wasn't just for the fun of obfuscation but, yeah, ok, I wouldn't lie to you, this is just for the fun of obfuscation:

return (integer) (((string) ((integer) llGetSubString((string) Thing, 0, 0)) + 1) + llGetSubString("000000000000", 0, llStringLength((string) Thing) - 1));

cute, but doesn't that round up to the next power of 10 even if it's already at a power of ten?

ETA
oops no, just when it's at a 9 leading power of ten
ETA2
and fails on things under ten except 9
_____________________
|
| . "Cat-Like Typing Detected"
| . This post may contain errors in logic, spelling, and
| . grammar known to the SL populace to cause confusion
|
| - Please Use PHP tags when posting scripts/code, Thanks.
| - Can't See PHP or URL Tags Correctly? Check Out This Link...
| -
Ephraim Kappler
Reprobate
Join date: 9 Jul 2007
Posts: 1,946
01-25-2010 23:10
My ... brainnn ... hurtzszszssss.
Indeterminate Schism
Registered User
Join date: 24 May 2008
Posts: 236
01-26-2010 01:57
From: Void Singer
cute, but doesn't that round up to the next power of 10 even if it's already at a power of ten?

ETA
oops no, just when it's at a 9 leading power of ten
ETA2
and fails on things under ten except 9


Oh there's lots of things wrong with it, won't work for negatives or scientific notation either, but it's a fun way to "look at how much it needs to change" and was inspired by your quote. TO the extent that it works, it works as intended, but it wasn't intended for use.

(at least some of this and my previous post makes sense. Sorry, I'm in a funny mood)
Talarus Luan
Ancient Archaean Dragon
Join date: 18 Mar 2006
Posts: 4,831
01-26-2010 11:19
If you're looking for scaling factors:

fBase = llPow(10.0,llFloor(llLog10(fNumber)));
fNextDivision = llCeil(fNumber / fBase) * fBase;

If you're dealing with graphs/charts, the logarithm/exponential functions are very good things to know. :)
Rolig Loon
Not as dumb as I look
Join date: 22 Mar 2007
Posts: 2,482
01-26-2010 11:35
Thanks, Talarus. If I were dealing with values that range over several orders of magnitude, especially, it would make great sense to graph them logarithmically. It's good to remember that LSL has that capacity.

I LOVE it when a simple question yields so many cool options. :D
_____________________
It's hard to tell gender from names around here but if you care, Rolig = she. And I exist only in SL, so don't ask.... ;)

Look for my work in XStreetSL at