String to Integer cast issue, possible bug?
|
|
Slade Christensen
Liquid Heat CTO
Join date: 25 Dec 2005
Posts: 31
|
11-17-2008 17:45
Hi all, Paste the following code into a prim to see what I am referring to in this thread: string testnum="9999"; default { state_entry() { if((integer)testnum < 10000) { llSay(0, "Test number is under 10,000"  ; }else{ llSay(0,"Test number is greater than or equal to 10,000"  ; } } } This if/then block works fine with testnum = 9999 and 10,000 *but* if you make testnum a large number such as 234823872344 it will fail and say that it's below 10,000. Any ideas as to why and how I can work around or fix this issue? Many thanks
|
|
Jesse Barnett
500,000 scoville units
Join date: 21 May 2006
Posts: 4,160
|
11-17-2008 18:04
234823872344 is not a valid integer:
"an integer is a signed, 32-bit value with valid range from -2147483648 to 2147483647"
and directly impacting your problem:
"Beware, a integer variable assigned a value larger/smaller than the valid range is instead assigned a value of -1."
234823872344 = -1
_____________________
I (who is a she not a he) reserve the right to exercise selective comprehension of the OP's question at anytime. From: someone I am still around, just no longer here. See you across the aisle. Hope LL burns in hell for archiving this forum
|
|
Slade Christensen
Liquid Heat CTO
Join date: 25 Dec 2005
Posts: 31
|
11-17-2008 23:26
Thanks for the reply, I assumed it was a problem with integer bounds but wasn't sure. Since integer wont work in this case for comparison sake (I cant do an IF to see if it's greater than that #), what would you suggest?
|
|
Shifting Dreamscape
Always questioning ...
Join date: 12 Dec 2007
Posts: 266
|
11-18-2008 01:16
You could check the length of the string to see if it has more than 4 digits, then you would know if it was less than 10,000:
string testnum="9999"; default { state_entry() { if(llStringLength(testnum) < 5) { llSay(0, "Test number is under 10,000"); }else{ llSay(0,"Test number is greater than or equal to 10,000"); } } }
|
|
Void Singer
Int vSelf = Sing(void);
Join date: 24 Sep 2005
Posts: 6,973
|
11-18-2008 05:39
just remember to check for the presence of bad data, or possible variations 1.99999999999 <-- need to cut the string after the '.' 15lindens <-- need to discard invalid characters 0xFFFFF <-- too big for integer range, requires special processing (notice it's 5 'f's)
_____________________
| | . "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... | - 
|
|
Slade Christensen
Liquid Heat CTO
Join date: 25 Dec 2005
Posts: 31
|
11-19-2008 09:59
I suppose I could also do the following: if((integer)testnum < 10000 && (integer)testnum != -1)
|
|
Void Singer
Int vSelf = Sing(void);
Join date: 24 Sep 2005
Posts: 6,973
|
11-19-2008 11:18
that's great, unless they actually put in -1 =)
_____________________
| | . "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... | - 
|