josdejong / mathjs

An extensive math library for JavaScript and Node.js
https://mathjs.org
Apache License 2.0
14.44k stars 1.24k forks source link

Support ECMA 6 Draft #235

Closed AndrewGG closed 2 years ago

AndrewGG commented 10 years ago

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

josdejong commented 10 years ago

Thanks, that would be a nice addition (and not that much work to implement).

auscompgeek commented 9 years ago

On this note, math.js should probably also use native ES6 functions where possible.

josdejong commented 9 years ago

Which functions (besides hypot and cbrt)?

auscompgeek commented 9 years ago

Off the top of my head: hyperbolic trig functions, Math.sign, String.prototype.repeat.

josdejong commented 9 years ago

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.

josdejong commented 9 years ago

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

Yaffle commented 9 years ago

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).

josdejong commented 9 years ago

Thanks, good to take that into account...

josdejong commented 9 years ago

I've just implemented cbrt for numbers, complex numbers, and BigNumbers. It's in the develop branch.

I will to implement hypot too.

josdejong commented 9 years ago

Ok then, hypot is available in develop too.

josdejong commented 9 years ago

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.

BigFav commented 9 years ago

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.

josdejong commented 9 years ago

@BigFav yes sure, good idea. I suppose the native versions will be faster.

BigFav commented 9 years ago

@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.

josdejong commented 9 years ago

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.

BigFav commented 9 years ago

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)

josdejong commented 9 years ago

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.

BigFav commented 9 years ago

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.

cshaa commented 3 years ago

Just for completeness: as of now, math.js has implemented all Math functions except for trunc, fround, imul and clz32.

josdejong commented 2 years ago

Closing in favor of https://github.com/josdejong/mathjs/discussions/2692