HaxeDoc2

(やまだぃちぅ) #1
6 var z = -2;
7
8 trace(Math.abs(z)); // 2
9 trace(Math.sin(x*Math.PI)); // 1
10 trace(Math.ceil(y)); // 21
11
12 // log is the natural logarithm
13 trace(Math.log(Math.exp(5)));// 5
14
15 // Output for neko target, mayvary
16 // depending on platform
17 trace(1/0); //inf
18 trace(-1/0); //-inf
19 trace(Math.sqrt(-1)); //nan
20 }
21 }

10.4.1 Special Numbers


The math library has definitions for several special numbers:


  • NaN (Not a Number): returned when a mathmatically incorrect operation is executed, e.g.
    Math.sqrt(-1)

  • POSITIVEINFINITY: e.g. divide a positive number by zero

  • NEGATIVEINFINITY: e.g. divide a negative number by zero

  • PI : 3.1415...


10.4.2 Mathematical Errors


Although neko can fluidly handle mathematical errors, like division by zero, this is not true for
all targets. Depending on the target, mathematical errors may produce exceptions and ultimately
errors.

10.4.3 Integer Math


If you are targeting a platform that can utilize integer operations, e.g. integer division, it should
be wrapped inStd.int()for improved performance. The Haxe Compiler can then optimize for
integer operations. An example:
1 var intDivision = Std.int(6.2/4.7);

I think C++ can use
integer operatins, but I
don’t know about any
other targets. Only
saw this mentioned
in an old discussion
thread, still true?


I think C++ can use
integer operatins, but I
don’t know about any
other targets. Only
saw this mentioned
in an old discussion
thread, still true?


10.4.4 Extensions......................................


It is common to seeStatic Extension(Section 6.3) used with the math library. This code shows a
simple example:
Free download pdf