Search code examples
mathtrigonometryeiffel

What is the domain of trigonometric functions in Eiffel? Is it [-pi/4,+pi/4]?


In Eiffel, the class DOUBLE_MATH defines trigonometric functions. When I see the interface of this class as shown here, it says

cosine (v: REAL_64): REAL_64 -- Trigonometric cosine of radian `v' approximated -- in the range [-pi/4, +pi/4]

and

sine (v: REAL_64): REAL_64 -- Trigonometric sine of radian `v' approximated -- in range [-pi/4, +pi/4]

and

tangent (v: REAL_64): REAL_64 -- Trigonometric tangent of radian `v' approximated -- in range [-pi/4, +pi/4]

It seems to claim that the trigonometric functions will only work in the domain [-pi/4,+pi/4]. However, when I tried using them for other values, they seemed to work.

I am worried that it might occasionally fail, or that the success I saw is in fact a form of undefined behavior that cannot be relied upon.

Is it safe to use the functions outside the given domain? If so, why is this domain specified? If not, why is it made such that the functions work only in this domain?


Solution

  • The functions are implemented as wrappers of the corresponding C functions from math.h. Because Eiffel can be compiled for virtually any platform with a C compiler, the comment makes sure to restrict the domain to most restrictive domains of C compiler implementations. Also, some CPUs provide direct support for trigonometric functions, but their precision goes down if the input value is beyond a given range.

    To summarize, you need to check the manual of the C compiler for the platforms you are going to use for the specific ranges of trigonometric functions, or, alternatively, make sure the input value is in the range as specified in the comment.