? Parameter Type Checking ● SmileBASIC Source

Sign In

*Usernames are case-sensitive
Forgot my password

Parameter Type Checking

Function parameters in SmileBASIC are not type-checked - a parameter can have any type, regardless of the variable name, depending only on what value was passed to the function. If you want to treat different types of parameters differently, it is useful to be able to identify the type of a parameter. Currently there is no known way to identify arrays, but it is possible to differentiate between real number type, integer type, and string type. Consider the following function: 'Returns 0 for real, 1 for integer, 2 for string DEF TYPEOF(X) IF X*0 && X*0==X*0 THEN RETURN 2 RETURN X*0-&H80000000<0 END So, how does this work? First, let's consider differentiating strings from numbers. The critical point of this is that all strings are considered to be True in IF statements or the && and || operators. So if you multiply a string by 0, you get the empty string, which is considered True. On the other hand, if you multiply a number by 0, usually you get 0, which is False. I say "usually" because there are some special cases as far as real numbers are concerned. Multiplying Infinity or NaN (Not a Number) by 0 will produce NaN, which is considered non-zero, or True. So if we check only X*0, then strings, Infinity, and NaN will be detected. The key to solving this problem is that NaN is considered to be not equal to itself, while on the other hand the empty string is always equal to itself. So X*0==X*0 out of these possible values will be True only if X is a string. After strings are ruled out, it is fairly simple to differentiate between reals and integers. We will detect an instance of integer overflow - calculating (0 - (-2^31)) should return 2^31, but if we are working with integers, that overflows and becomes negative, so we return True or 1. On the other hand, if X is real it will return 2^31 as expected, so we return False or 0. Note also that if X*0 is our good friend NaN, comparing NaN<0 will also return False. All possible cases are handled.
7 votes
  • typeof
8 Comment(s) snail_ snail_ Power User Helper Received for being very helpful around SmileBASIC Source Achievements Expert Programmer Programming no longer gives me any trouble. Come to me for help, if you like! Programming Strength Avatar Taboo I didn't change my avatar for 180 days Website The first statement can be changed to IF (X||0)==3 THEN RETURN 2 This is because string || anything always returns 3 instead of 1 or 0, bizarrely enough. This saves you 3 multiplies and is generally shorter. 12Me21 12Me21 Admin Syntax Highlighter Received for creating the code syntax highlighter on SBS Night Person I like the quiet night and sleep late. Express Yourself Second Year My account is over 2 years old Website IF X==0==3 THEN RETURN 21 character shorter. EDIT: IF X>0>1 THEN RETURN 2 EDIT 2: SmileBASIC, 57 52 bytes DEF Y(X)IF X>0>2THEN RETURN 2 RETURN.>X*0-(1<<31)END SquareFingers SquareFingers Wow, this exposes a very strange aspect of SmileBasic. Your code relies on &H80000000 being interpreted as the 'integer' type, not the 'floating-point' type. But... PRINT &H0-&H80000000 gives a positive result. So, &H0 appears to be getting treated as a floating-point, and &H80000000 appears to be getting treated as integer. Just for fun I tried PRINT &H00000000-&H80000000 and that also showed the zero was treated as floating-point. calc84maniac calc84maniac OSP Contest 1 Winner I won the first SmileBASIC Source OSP Contest! Programming Contest Amazing Page Hidden Achievements Scholar Received for knowing a great deal about programming topics Achievements As I said in the relevant forum thread, none of those constants individually are floating-point, but combined the result are automatically extended to floating point because of the overflow (as a part of the preprocessing step of constant folding). This does not happen at runtime. randomous randomous Robot Hidden Easter Eggs Second Year My account is over 2 years old Website Drawing I like to draw! Hobbies Oh... yeah, there's no bbcode on comments yet. Sorry! Guzzler Guzzler First Week Joined in the very first week of SmileBASIC Source Website Drawing I like to draw! Hobbies Intermediate Programmer I can make programs, but I still have trouble here and there. Programming Strength Hey randomouse, FIX THAT ALREADY . Lol, see what I did? kldck_hul kldck_hul First Month Joined in the very first month of SmileBASIC Source Website Scholar Received for knowing a great deal about programming topics Achievements Zelda Is Awesome! I love The Legend Of Zelda! Express Yourself This is plain brilliant. I'm working on an object-oriented language extension and this was one of the big things missing. I will probably include this in my library. randomous randomous Robot Hidden Easter Eggs Second Year My account is over 2 years old Website Drawing I like to draw! Hobbies This is REALLY useful, and I'm glad you took the time to figure all this out. Thank you for posting this!