• source navigation  • diff markup  • identifier search  • freetext search  • 

Sources/ucode/tests/custom/01_arithmetic/00_value_conversion

  1 Performing unary plus or minus, or performing arithmetic operations may
  2 implicitly convert non-numeric values to numeric ones.
  3 
  4 If an addition is performed and either operand is a string, the other
  5 operand is converted into a string as well and the addition result is a
  6 concatenated string consisting of the two operand values.
  7 
  8 -- Expect stdout --
  9 HelloWorld
 10 12
 11 34
 12 true
 13 false
 14 null
 15 { "some": "object" }
 16 [ "some", "array" ]
 17 function() { ... }
 18 1.2
 19 Infinity
 20 124
 21 123
 22 123
 23 NaN
 24 NaN
 25 NaN
 26 124.2
 27 Infinity
 28 1
 29 0
 30 0
 31 NaN
 32 NaN
 33 NaN
 34 1.2
 35 Infinity
 36 123
 37 12.3
 38 NaN
 39 -1
 40 0
 41 0
 42 NaN
 43 NaN
 44 NaN
 45 -1.2
 46 -Infinity
 47 -123
 48 -12.3
 49 NaN
 50 4.2
 51 9.6
 52 -- End --
 53 
 54 -- Testcase --
 55 {%
 56         print(join("\n", [
 57 
 58                 // Adding two strings concatenates them:
 59                 "Hello" + "World",
 60 
 61                 // Adding a number to a string results in a string:
 62                 "1" + 2,
 63 
 64                 // Adding a string to a number results in a string:
 65                 3 + "4",
 66 
 67                 // Adding any non-string value to a string or vice versa will
 68                 // force stringification of the non-string value
 69                 "" + true,
 70                 "" + false,
 71                 "" + null,
 72                 "" + { some: "object" },
 73                 "" + [ "some", "array" ],
 74                 "" + function() {},
 75                 "" + 1.2,
 76                 "" + (1 / 0),
 77 
 78                 // Adding a numeric value to a non-string, non-numeric value
 79                 // or vice versa will convert the non-numeric argument to a
 80                 // number
 81                 123 + true,
 82                 123 + false,
 83                 123 + null,
 84                 123 + { some: "object" },
 85                 123 + [ "some", "array" ],
 86                 123 + function() {},
 87                 123 + 1.2,
 88                 123 + (1 / 0),
 89 
 90                 // The unary "+" operator follows the same logic as adding a
 91                 // non-numeric, non-string value to a numeric one. Additionally
 92                 // the unary plus forces conversion of string values into numbers
 93                 +true,
 94                 +false,
 95                 +null,
 96                 +{ some: "object" },
 97                 +[ "some", "array" ],
 98                 +function() {},
 99                 +1.2,
100                 +(1 / 0),
101                 +"123",
102                 +"12.3",
103                 +"this is not a number",
104 
105                 // The unary "-" operator functions like the unary "+" one and
106                 // it additionally returns the negation of the numeric value
107                 -true,
108                 -false,
109                 -null,
110                 -{ some: "object" },
111                 -[ "some", "array" ],
112                 -function() {},
113                 -1.2,
114                 -(1 / 0),
115                 -"123",
116                 -"12.3",
117                 -"this is not a number",
118 
119                 // Adding a double to an integer or vice versa will force the
120                 // result to a double as well
121                 1.2 + 3,
122                 4 + 5.6,
123 
124         "" ]));
125 -- End --

This page was automatically generated by LXR 0.3.1.  •  OpenWrt