fasseg
fasseg

Reputation: 17761

Evaluating Math.tan(Math.acos(0d))

Shouldn't the following statement hold?

assertTrue(Double.isNaN(Math.tan(Math.acos(0d))));

But instead of Double.NaN Java returns

6.123233995736766 * 10^-17    

on my 64-bit box.

EDIT: This was a Copy and Paste error. In fact Java returns 1.633123935319537E16

I'm aware that this is because of the floating point representation, but i was under the impression that those undefined values of the tangent function would get the same treatment as e.g. Math.sqrt(-1d) but I guess in this case java.lang.Math just checks if the argument is positive before evaluating.

Upvotes: 0

Views: 176

Answers (1)

Peter Lawrey
Peter Lawrey

Reputation: 533700

I get something different.

System.out.println(Math.tan(Math.acos(0d)));
// and the tan for the next representable value.
System.out.println(Math.tan(Math.acos(0d) + Math.ulp(Math.acos(0d))));

prints

1.633123935319537E16
-6.218431163823738E15

A 64-bit floating point cannot represent PI/2 exactly (it has an infinite number of digits) it represents a number close to this value and the tan() of this value is finite.

Upvotes: 3

Related Questions