Prev | Current Page 150 | Next

L. McColl-Sylvester and F. Ponticelli

"Professional haXe and Neko"

Probably the most annoying of these is the divide by zero issue. The
problem is that, no matter how great computers are at crunching numbers, dividing any number
whatsoever by zero produces an infinite value, and, although the Float type in haXe can deal with very
Chapter 3: Learning the Basics
65
large numbers, there ??™ s no way you could fit an infinite value inside one. To help resolve this issue, the
haXe Math class provides two constants that allow you to test for such a mistake: NEGATIVE_INFINITY
and POSITIVE_INFINITY . These two constant values are of the type Float , but do not represent actual
values. They merely allow you to compare your own Float values against them to see if they are valid
numbers:
var myFlt : Float = 2 / 0;
var isValid : Bool = (myFlt != Math.POSITIVE_INFINITY);
You can also use the Math method isFinite , which returns true if the contained number is not an
infinite value:
var myFlt : Float = 2 / 0;
var isValid : Bool = Math.isFinite(myFlt);
Not a Number
Another way to test for numerical validity is using the NaN constant.


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