Editing 1537: Types

Jump to: navigation, search

Warning: You are not logged in. Your IP address will be publicly visible if you make any edits. If you log in or create an account, your edits will be attributed to your username, along with other benefits.

The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then save the changes below to finish undoing the edit.
Latest revision Your text
Line 22: Line 22:
 
Weird results abound in the new XKCD programming language:
 
Weird results abound in the new XKCD programming language:
 
# <code>2 + "2"</code> uses the <code>+</code> operator on a number and a string. In some programming languages, this might result in the number <code>4</code> in math addition, or <code>"22"</code> in string concatenation; however, the new language converts the string to an integer, adds them to produce <code>4</code> and converts back to a string. Alternatively, it may instead be adding 2 to the ASCII value of the character <code>"2"</code> (50), resulting in the character <code>"4"</code> (52). This is (somewhat) consistent with the behavior for item 4.
 
# <code>2 + "2"</code> uses the <code>+</code> operator on a number and a string. In some programming languages, this might result in the number <code>4</code> in math addition, or <code>"22"</code> in string concatenation; however, the new language converts the string to an integer, adds them to produce <code>4</code> and converts back to a string. Alternatively, it may instead be adding 2 to the ASCII value of the character <code>"2"</code> (50), resulting in the character <code>"4"</code> (52). This is (somewhat) consistent with the behavior for item 4.
βˆ’
# <code>"2" + []</code> adds a string to an array or list. This first inexplicably converts the string to a number again, and then it literally adds the number to the list by prepending it. And then the result (the entire array) is converted to a string again. (Possibly, this is meant to be read as 'adding brackets to the string "2" produces the string "[2]"?')
+
# <code>"2" + []</code> adds a string to an array or list. This first inexplicably converts the string to a number again, and then it literally adds the number to the list by prepending it. And then the result (the entire array) is converted to a string again.
 
# <code>(2/0)</code> divides <code>2</code> by <code>0</code> and quite reasonably results in <code>NaN</code>, meaning "Not a Number", though in most languages, as prescribed by the IEEE 754 standard for floating point numbers, dividing a nonzero number by zero would instead return an infinity value.
 
# <code>(2/0)</code> divides <code>2</code> by <code>0</code> and quite reasonably results in <code>NaN</code>, meaning "Not a Number", though in most languages, as prescribed by the IEEE 754 standard for floating point numbers, dividing a nonzero number by zero would instead return an infinity value.
 
# <code>(2/0)+2</code> adds <code>2</code> to <code>NaN</code>. <code>2</code> Is "added" to the string <code>"NaN"</code> as again, the number is converted to a string for apparently no reason, which produces <code>"NaP"</code>. If the language's convention is to add to the ASCII value of a character or string, then in this case it added 2 to the character <code>"N"</code> (78), resulting in <code>"P"</code> (80). How the string "NaP" is converted into a bare NaP with undefined meaning is not clear. It is possible the "NaP" means "Not a Positive" as opposed to "Not a Negative".  It could also mean "Not a Prayer", as you're taking a "NaN" condition and trying to do more with it.
 
# <code>(2/0)+2</code> adds <code>2</code> to <code>NaN</code>. <code>2</code> Is "added" to the string <code>"NaN"</code> as again, the number is converted to a string for apparently no reason, which produces <code>"NaP"</code>. If the language's convention is to add to the ASCII value of a character or string, then in this case it added 2 to the character <code>"N"</code> (78), resulting in <code>"P"</code> (80). How the string "NaP" is converted into a bare NaP with undefined meaning is not clear. It is possible the "NaP" means "Not a Positive" as opposed to "Not a Negative".  It could also mean "Not a Prayer", as you're taking a "NaN" condition and trying to do more with it.

Please note that all contributions to explain xkcd may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see explain xkcd:Copyrights for details). Do not submit copyrighted work without permission!

To protect the wiki against automated edit spam, we kindly ask you to solve the following CAPTCHA:

Cancel | Editing help (opens in new window)