Why does the Math.toRadians method return an inaccurate value?

Asked

Viewed 77 times

4

I was programming a method in which I needed to convert a value from degrees to radians and I wondered if this conversion no longer existed in Java. Searching the Internet I discovered the method Math.toRadians.

The problem is that on almost every website I read about this method, it is said to return an inaccurate value - an approximation of the value in radians.

The Conversion from degrees to Radian is generally inexact.

Why? What would be the difference between using the Math.toRadians and the calculation below?

double radians = (degrees * Math.PI / 180.0);

1 answer

5


How you convert an angle in degree unit to radian?

To formula usual is in the question itself. Given an angle x of degrees, is determined its α in radian by: α = x * π / 180.

Let’s see, for example, an angle of 90°. Replacing x in the above equation, we have π/2 radian.

Although 90° is easily representable by an integer (90) on a computer, as you would represent π/2?

So far, the human being (by the way, the computer) has not even been able to calculate all the digits of π. That’s why the conversion will never, in fact, be exact.

According to the documentation, the method toRadians returns a number of type double. Internally, to store this double, Java uses a binary value of 64 bits, in accordance with the standards set out in IEEE 754 specification.

π digits already exceed 6 terabytes of memory. It is impractical to store all these digits in a mere space of 64 bits. And even if this space were available, the accuracy would not yet be achieved, since the number is irrational (it is not possible to compute all its digits).

In Mathematics accuracy exists because you do not actually evaluate the number π (3.14...). If any Mathematical expression contains π, you will only leave the Greek letter π there, indicating that it is the number, but do not compute it, properly speaking.

The method toRadians, on the other hand, it cannot leave π untouched, unlike mathematics. For example, if you pass the number 90, the implementation will compute the expression Math.PI / 2, where Math.PI is a constant that approaches the value of π. Notice the difference? As the computer is computing the expression with an approximation of π, accuracy is lost.


And as for the difference to the code that is in the question: there is no. Of course the Java implementation can be minimally different from that expression, but deep down it’s the same thing.

Both are equivalent and equally inaccurate. It is a "problem" arising from the way numbers are stored by a computer. It is similar to notorious imprecision of the expression 0.1 + 0.2 when evaluated under the rules of floating point arithmetic (standardized by said IEEE 754).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.