Math.sincos(x)?

Robert Poor rdpoor at gmail.com
Thu Jun 22 18:02:50 UTC 2017


[Preamble: this is my first post to es-discuss -- if this isn't the
right place to suggest language extensions, please let me know.
Thanks. - rdp]

How often have you seen code that calls Math.sin() and Math.cos() with
the same argument, e.g:

   x = r * Math.cos(theta);
   y = r * Math.sin(theta);

?  This trope is repeated for polar coordinates, complex arithmetic, FTTs, etc.

However, most implementations for cos(theta) actually generate
sin(theta) as a "byproduct" (and vice-versa).  Since trigonometric
functions are relatively expensive, and this is a common pattern,
wouldn't it make sense to define a function that returns cos(theta)
and sin(theta) at the same time?  A polyfill might look like this:

   function sincos(theta) {
      return { sin: sin(theta), cos: cos(theta) };
   }

(or whatever the preferred style is for returning multiple values),
but a real implementation would make a single call to the underlying
trigonometric routine.

- rdp


More information about the es-discuss mailing list