How do we access the transcendental functions (sin, log, etc) in Swift?

Just checked the Standard Library pages, and functions like sine or natural logarithm aren’t provided. Now (most?) operating systems have implementations of those functions. What is the current way to access them? The Numerics package? Foundation (how’d that work for non-Apple systems)?

The Numerics package. Specifically, types which conform to the ElementaryFunctions protocol provide them.

Originally this was supposed to be part of the standard library. But as stated in the proposal:

This proposal is accepted, but currently not implemented in Swift due to source breaking consequences relating to typechecker performance and shadowing rules. These are expected to be resolved in a future release of Swift, at which point this proposal can be implemented.

I'm not sure if those issues have been resolved yet. In any case, for now use the numerics package.

6 Likes