Hi: i was wondering if it is possible to determine the type of a variable passed to a function...in other words, if this is my function function foo(var_arg) { } how can i determine if var_arg is a STRING, NUMBER, or ARRAY? Also, if it's a number is there any way to determine if it's greater than 32 bits? does flash even support numbers greater than 32 bits?
Hi, The typeof(var_arg) will return either "string","number","movieclip","object" or "undefined" , it won't return array as this is just a special type of object. as for Flash's ability with numbers... Number.MAX_VALUE : Constant representing the largest representable number (double-precision IEEE-754). This number is approximately 1.7976931348623158e+308. Hope that helps,
thank you both. mandingo, any advice on how to determine if a number is greater than 32 bits? 2 to the 32nd power is 4294967296 any sort of 1's or 2's complement stuff going on here or will a simple comparison do?
I am not a mathematician though there are a few on this forum and maybe they can tell you the best ways to do this. If what you say is correct and the largest 32 bit integer is 4294967296, then something like this perhaps? constant32Bit = 4294967296; conversion = function(var_arg){ switch(typeof(var_arg)){ case "number": if(var_arg > constant32Bit){ trace(var_arg + " is a type of " + typeof(var_arg) + " that is greater than 32 Bit"); } break; case "object": trace(var_arg + " is a type of " + typeof(var_arg)); break; case "string": trace(var_arg + " is a type of " + typeof(var_arg)); break; } } // test different values for comparison conversion("thisString"); conversion(4325); conversion(constant32Bit); conversion(constant32Bit+1); hope that helps, cheers,
thanks. it had occurred to me to do a check like that, but i have this sneaking suspicion it's not that simple.
I will leave you the domain of 'sneaky' suspicions and the mathematicians the domain of validating the methods used. The code provided does make the comparison and only returns on numbers greater... so... I will now bow out on this one. cheers,
There is our resident mathematician now!!! I guess the question that still remains kglad is "Is the suggestion of a comparison going to achieve the result sneakyimp wants?"
this rocks...thanks for the help guys...for your reference, i'm trying to convert this bit of PERL code which can serialize() an object in PERL so that PHP can unserialize() it. i'm thinking this will be super handy for communicating arrays and such with PHP. it's all kind of greek to me.... sub serialize_value { my ($value) = @_; my $s; #this is an integer # Thanks to Konrad Stepien <konrad@interdata.net.pl> # for pointing out correct handling of negative numbers. if ($value =~ /^\-?\d+$/) { # if larger than 32 bits, serialize it as a double if (abs($value) > 2**32) { $s = "d:$value;"; } else { $s = "i:$value;"; } } #this is a double # Thanks to Konrad Stepien <konrad@interdata.net.pl> # for pointing out correct handling of negative numbers. elsif ($value =~ /^\-?(\d+)\.(\d+)$/) { $s = "d:$value;"; } #this is a NULL value # # Only values of "\0" will be serialized as NULL # Empty strings are not NULL, they are simply empty. # Note: this differs from v0.7 where a string "NULL" would # be serialized as NULL. elsif ($value eq "\0") { ##print "NULL: \"$value\" \n"; $s = "N;"; } #this is a string else { my $vlen = length($value); $s = "s:$vlen:\"$value\";"; } return $s; }
maybe i should bitwise shift decimal value -1? this is from the documentation: The following example converts -1 to a 32-bit integer and shifts it 1 bit to the right. x = -1 >>> 1 The result of the above operation is as follows: x = 2147483647 This is because -1 decimal is 11111111111111111111111111111111 binary (thirty-two 1's), and when you shift right (unsigned) by 1 bit, the least significant (rightmost) bit is discarded, and the most significant (leftmost) bit is filled with a 0. The result is 01111111111111111111111111111111 binary, which represents the 32-bit integer 2147483647.
A bitwise shift? Is that a smart movement a little at a time? Now you have moved from all greek to all Hindu-Arabic and I am still out of my depth... give me simple arithmetic base 1010 any day... :)
x**y is x to the y power. and i'm not really sure what sneaky's trying to determine. if he wants to test if a given integer can be represented by a 32-bit binary number, then yes testing if the number in question is strictly less than 2**32 will yield the result he seeks.
I'm trying to duplicate certain aspects of the PHP function serialize() it requires that you specify whether a number is a DOUBLE or not. I have no idea exactly what this means...obviously certain small integers are not...certain large ones are. I think the term double is intended as a double in C or C++ but i honestly don't know. at any rate, I understand that negative numbers and decimal numbers must also be examined to determine their double status...it's not just a matter of magnitude, it's a matter of digits. for your reference, here is the PERL script I'm trying to conver to actionscript...i have no experience with PERL (see attached).
i've never heard the term "double number". it's not a term i encountered in mathematics and i have a far amount of experience in math (phd). i checked on google and i see various mention of a double number related to programming languages like c++ but still can't find a definition.
Interesting... I must admit I have learned much information that I will probably never need to know ever again in this post, but I said that about almost everything my high school math teacher taught me and look how wrong I was then. Just hypothesising here but could the reference to a 'double number' be due to the fact that any number is combined of a whole number (reportable as a value) and a fraction or decimal value (the remainder) also reportable as a value. As Jeckyl inferred, whole numbers (or the classic integers) do not need this second set of values to define the number so therefore form the unique subset of double numbers ... all along I was thinking it must have something to do with a floating point calculation that uses multiple 32bit numbers to extend the range. live and learn thanks all for enlightening me... hope sneakyimp gets his answer soon.
doubles are numbers with or without decimal places. Anything that is a 'Number' type in ActionScript is a double. integers are a smaller subset of the range of doubles. Its whole numbers (between limits derived from the number of bits in a value). In ActionScript you cannot directly tell if a number if truly an integer other than checking it is a whole number and within the range specified.
It does back to the ancient times of FORTRAN It relates to the number of bytes used to store floating point numbers. a 'float' (or single precision) uses 4 bytes a 'double' (or double precision) uses 8 bytes all numbers in actionscript are converted to doubles for calculations .. effectively you can treat them as all being double precision. see http://encyclopedia.thefreedictionary.com/Double%20Precision
yes jeckyl is write...single precision numbers are 4 byte (32 bits) and "double" precision numbers are 8 bytes. in C, you have all of these super low-level programming things you have to do...like specify how many bytes you need for a number or string, you have to allocate memory, etc. a bit of this stuff is coming back now...since an integer can be positive or negative, a 32-bit number (a "single" in c parlance) must use 1 bit to specify whether the number is positive or negative. you'd think that it would be as simple as picking one bit to represent the positiveness or negativeness of a number but that's not the case...why? it has something to do with how binary addition and subtraction are performed. if 1 is represented by 0001, negative 1 is not simply 1001...i think negative 1 is actually 1111. why? i have no clue...some engineer decision somewhere. i'll read that PDF. anyways, long story short...since you have to use the whole 32 bit range to represent positive AND negative numbers, you don't get the whole 4294967296 range for singles...you get roughly half that. i just want to find the exact rule so i can properly create my serialize() function.....this function could be super duper handy for folks trading variables with PHP.... for more info go here: http://www.php.net/manual/en/function.serialize.php
well i read that PDF and couldn't quite comprehend it well enough to get a definitive answer. i did, however, get this post at phpbuilder.com: This question is highly language-specific; some languages (or at least, some language implementations) don't store numbers as C-like ints or doubles at all. In PHP's case (on a 32-bit machine, at least), the largest number expressible as an integer is 2^31-1 (i.e 2147483647). Adding 1 to this in PHP will give you 2147483647, stored as a double, while C will tell you (assuming you're using a signed int and not an unsigned one) that the answer is an int with a value of -1, since 10000000000000000000000000000000 binary is the two's complement representation of -1 (in a thirty two bit environment). In fact, PHP will also tell you that 1<<31 is equal to -1, because it will take the 1, shift it left thirty-one places and get the above bit pattern. I'm starting to think i should just encode all numbers as doubles and be done with it. i can't imagine I'll be using numbers that large for most purposes anyway.
all righty! first, why on EARTH would flash truncate a decimal number of all numbers are doubles??? this statement: trace(12343434874.348373872); returns this: 12343434874.3484 where'd my other six digits go?? as for PHP and how it decides doubles versus singles, I have done some experiments and now have some results...i defined an array and serialized it: $arr[0] = 1; $arr[1] = 2; $arr[2] = "foo"; $arr[3] = 12343434874.348373872; $arr[4] = 2147483647; $arr[5] = 1.1; $arr[6] = 2147483648; $arr[7][0] = "first sub array"; $arr[7][1] = "second sub array"; the results are pretty helpful and not that suprising. double/single integers are cut EXACTLY on the 2147483647 boundary. interestingly, any decimal number is serialized as a double: $arr[3] is a double (php actually added more digits when it serialized: 12343434874.3483734130859375) $arr[4] is a single $arr[5] is a double (php added digits to this too: 1.100000000000000088817841970012523233890533447265625) $arr[6] is a double WEIRD! I don't think i can write a function to duplicate THOSE results. I'm currently posting phpbuilder to get more detail. good news is that i have a serialize() function that works except for these crazy rounding issues. if anybody wants it, i can email it to them.
i haven't followed the details of this thread but, in general, if you're trying to make sense of binary numbers and the way computers handle them you'll have a major, almost unfathomable, mess on your hands if you try and use decimal numbers to probe. you would have a much easier time understanding what's going on if you use hexidecimal numbers.
i reckon you're right about the binary numbers thing. it never occurred to me that binary cannot represent 1.1 at all...much less with only 32 bits (or even 64 bits). I think this thread as it is currently titled is thoroughly answered. i believe it's time to start a new one to find out why this actionscript: trace(12343434874.348373872); returns this: 12343434874.3484
[quoted text, click to view] > I believe it's time to start a new one to find out why this > actionscript: > > trace(12343434874.348373872); > > returns this: > > 12343434874.3484
That is correct behaviour .. because double numbers are not accurate to more places than that .. so showing more decimal places would not be accurate. It has to be rounded off somewhere. Unfortunately, AS does not provide a means of specifying the number of digits precision for conversion.
Don't see what you're looking for? Try a search.
|