Closed AndrewGG closed 2 years ago
Thanks, that would be a nice addition (and not that much work to implement).
On this note, math.js should probably also use native ES6 functions where possible.
Which functions (besides hypot and cbrt)?
Off the top of my head: hyperbolic trig functions, Math.sign, String.prototype.repeat.
Ah, yes sure!
There is one thing we should be careful with: it can be the the custom built implementations have round-off errors in the 16th digit, so we have to check whether all unit tests neatly do an approximation and not an exact match of numbers.
Example implementations:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/hypot https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/cbrt
https://github.com/paulmillr/es6-shim/blob/master/es6-shim.js#L1619-L1633 https://github.com/paulmillr/es6-shim/blob/master/es6-shim.js#L1564-L1577
Note: es6-shim is a little more accurate for js numbers: Math.hypot avoids underflow/overflow; Math.cbrt uses Newton's method (as you do in nth-root).
Thanks, good to take that into account...
I've just implemented cbrt
for numbers, complex numbers, and BigNumbers. It's in the develop
branch.
I will to implement hypot
too.
Ok then, hypot
is available in develop
too.
Functions cbrt
and hypot
are available in the just released v2.3.0. Note that the website is not yet updated, but you can install the latest version via npm.
As of now, we do not check for native ES6 functions where possible. I agree with @auscompgeek that we should, do you @josdejong? If so, I can go through and add Math.<function name here> ||
to the files.
@BigFav yes sure, good idea. I suppose the native versions will be faster.
@josdejong is there a clean way to type-check for a series with a mixture of types while also checking the individual types? Example, in hypot
I want to isolate for a series of numbers from a series of BigNumbers or mixture of numbers and BigNumbers.
That's something not yet supported by typed-function
. I want to add that in the future. Well, it is supported for variable arguments like add(...number | BigNumber)
, but not for array like structures like say add(Array.<number | BigNumber>)
.
A workaround for now is to create a little helper function to typecheck individual values, like done in function median.
Yeah, for this it would be pretty convenient to have something like:
'... number': Math.hypot || _hypot,
'... number | BigNumber': _hypot,
since Math.hypot
would only work on a series of numbers. (This currently throws an error of conflicting types)
Ah, I get your idea. That's a tricky one, as it's basically a conflicting signature. It should be possible to implement support for this in typed-function (in an efficient way) but it's not easy. Let me think about it.
So I looked over the draft, and there a few functions left. I completed the ones I thought were appropriate, but I do suggest @josdejong browse the draft over.
Closing in favor of https://github.com/josdejong/mathjs/discussions/2692
There are a number of functions (e.g. hypot() and cbrt()) in the ECMA 6 Draft spec that are not currently in mathjs. These could be included in mathjs already.
http://people.mozilla.org/~jorendorff/es6-draft.html#sec-math-object