Java Reference
In-Depth Information
> Math.acos(0.5);
<< 1.0471975511965976
The Math.atan() returns the arctangent of a number. The result is an angle:
> Math.atan(Math.sqrt(3)); // Same as Math.PI/3
<< 1.0471975511965976
Warning: Rounding Errors
You might have noticed that some of the values in the previous examples
were not exactly accurate. For example, sin(π/6) should be 0.5, yet
Math.sin(Math.PI/6) returns 0.49999999999999994.
This is to be expected when dealing with floating-point decimal numbers.
Computers have lots of trouble dealing with decimal fractions (as they work
in binary) and the answers can vary from one platform to another.
Another problem is that the value of π using Math.PI is only given correct
to 16 significant figures, which will affect the overall accuracy.
These rounding errors are no big deal for most web applications. Whenever
you perform any calculations, make sure that your program doesn't rely on
Random Numbers
The Math.random() method is used to create random numbers, which can be very use-
ful when writing programs. Calling the method will generate a number between 0 and 1,
like so:
> Math.random();
<< 0.7881970851344265
To generate a random number between 0 and another number, we can multiply the value
by that number. The following code generates a random number between 0 and 6:

Search WWH ::

Custom Search