Prev | Current Page 151 | Next

L. McColl-Sylvester and F. Ponticelli

"Professional haXe and Neko"

NaN , which means Not A Number,
can be used to check if a Float or Int are valid numerical types. If a numerical type is not valid, then its
value is NaN :
var myFlt : Float;
untyped { myFlt = ???Not a Number???; }
var isValid : Bool = (myFlt != Math.NaN);
On Neko, where such an expression is valid, the isValid variable will test true , while in Flash, the
result will not be a valid number, and so will be false . The haXe Math class also provides a method
called isNaN . This method returns true if the numerical value is not valid, and can be used in the
following way:
var myFlt : Float;
untyped { myFlt = ???Not a Number???; }
var isValid : Bool = !Math.isNaN(myFlt);
As you can see, the negation operator ( ! ) was used to return true if the result of the method is a valid
numerical type.
Float to Int Conversion Functions
When converting a Float value to an Int , the virtual machine just simply disregards the mantissa (the
value after the decimal point). This is the equivalent of rounding the Float down to the nearest integer.


Pages:
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163