Comments (8)
IEEE 754, in subsection 9.2, recommends some additional operations to be predefined:
Math.rsqrt(x) => 1/Math.sqrt(x)
Math.compound(x, n) => Math.pow(x + 1, Math.round(n))
, i.e. integer nMath.rootn(x, n) => Math.pow(x, 1/Math.round(n))
, i.e. integer nMath.pown(x, n) => Math.pow(x, Math.round(n))
, i.e. integer nMath.powr(x, y) => Math.pow(Math.abs(x), y)
Math.sinpi(x) => Math.sin(x * Math.PI)
Math.cospi(x) => Math.cos(x * Math.PI)
Math.tanpi(x) => Math.tan(x * Math.PI)
Math.asinpi(x) => Math.asin(x) / Math.PI
Math.acospi(x) => Math.acos(x) / Math.PI
Math.atanpi(x) => Math.atan(x) / Math.PI
Math.atan2pi(y, x) => Math.atan2(y, x) / Math.PI
(?)
I think ECMA-262 has all operations that are required in section 5.
Section 9 has even more operations.
from proposal-math-extensions.
There is a bunch of trigonometric functions used historically that could potentially make some calculations more precise or at least shorter.
Math.cot(x) => 1 / Math.tan(x)
cotangentMath.sec(x) => 1 / Math.cos(x)
secantMath.csc(x) => 1 / Math.sin(x)
cosecantMath.vsn(x) => 1 - Math.cos(x)
versed sineversin
Math.vcs(x) => 1 + Math.cos(x)
versed cosineMath.cvs(x) => 1 - Math.sin(x)
coversed sineMath.cvc(x) => 1 + Math.sin(x)
coversed cosineMath.hvs(x) => Math.vsn(x) / 2
haversed sineMath.hvc(x) => Math.vcs(x) / 2
haversed cosineMath.hcs(x) => Math.cvs(x) / 2
hacoversed sineMath.hcc(x) => Math.cvc(x) / 2
hacoversed cosineMath.xsc(x) => Math.sec(x) - 1
exsecantexsec
Math.xcs(x) => Math.csc(x) - 1
excosecantexcsc
Math.crd(x) => 2 * Math.sin(x/2)
chord
from proposal-math-extensions.
I suggest Math.clamp
argument order to be changed to (min, value, max)
instead of (value, min, max)
so it's in line with CSS's clamp()
.
from proposal-math-extensions.
@Crissov I would also recommend this one:
Math.sinc = x => +x == 0 ? 1 : Math.sin(x) / x
It's important and useful
from proposal-math-extensions.
@OlsonDev Good point, I'll amend the OP.
from proposal-math-extensions.
I also agree that factorial
should be added since its use is common. Also because a native implementation would definitely be faster. And if this proposal gets to Stage 4, a native factorial optimized for BigInt
s would be a great idea, because efficient implementations are hard to get right, and require a lot of code, this repo is proof of it
from proposal-math-extensions.
gcd
and lcm
should be variadic because code like this:
//num_list is an arbitrary iterable object here
let d = [...num_list].reduce(Math.gcd, 0)
Or this:
let d = 0;
for (const x of num_list)
d = Math.gcd(d, x);
Can be refactored as this:
let d = Math.gcd(...num_list)
The output of the empty GCD gcd()
should equal the initial value of the algorithm. In all cases, the init is 0
, because if it were 1
the results would be wrong. gcd(x) == x
(except for NaN
) because gcd(x, 0) == x && gcd(0, x) == x
. Reference
LCM can be defined as:
Math.lcm = function(...args) {
return args.reduce((a, b) => {
a = +a; b = +b;
return a != 0 || b != 0 ? a / Math.gcd(a, b) * b : 0
}, 1)
}
Since 0
gives wrong results as init, it seems lcm() == 1
is valid. References (see comments)
Now the real problem is non-ints. Should these functions return NaN
? actually, there's no need for that. The solution is to let implementations choose an algorithm to convert floats into fractions, then compute the GCD or LCM of those fractions, and finally convert the resulting fraction to a float. The main problem is that the accuracy of the algorithm would be implementation-defined. I proposed the fraction solution because the Euclidean algorithm for floats has intrinsic rounding errors becase the %
op treats floats as fractions whose denominator is a power of 2 (because they are)
from proposal-math-extensions.
There is a bunch of trigonometric functions used historically that could potentially make some calculations more precise or at least shorter.
Math.hvs(x) => Math.vsn(x) / 2
haversed sine
These may potentially also be useful
// restatement of the above suggestion, in the closed interval [0, 1]
Math.hvs = function (radians) {
return Math.pow(Math.sin(radians / 2), 2);
};
// return value in radians, in the closed interval [0, π]
Math.ahvs = function (haversine) {
return 2 * Math.asin(Math.sqrt(haversine));
};
// haversine of the central angle, in the closed interval [0, 1]
Math.haversineFormula = function (latA, lonA, latB, lonB) {
return Math.hvs(latB - latA) + Math.cos(latA) * Math.cos(latB) * Math.hvs(lonB - lonA);
};
// on the unit sphere, in the closed interval [0, π]
Math.sphericalDistance = function (latA, lonA, latB, lonB) {
return Math.ahvs(Math.haversineFormula(latA, lonA, latB, lonB));
};
from proposal-math-extensions.
Related Issues (20)
- [Suggestion] Math.mean and Math.roundBy HOT 2
- [Suggestion] Constants to be added HOT 1
- Is it still active? HOT 7
- [Suggestion] Math.lerp and Math.normalize HOT 2
- alternatives to Math.radians and Math.degrees HOT 5
- Math.exp2
- modulo function HOT 8
- Math.pow should accept Bigints, modulo and pow=-1 HOT 12
- Proposal Math.clamp separately HOT 1
- In the spec draft missed conversion of arguments to numbers
- Repository should be transferred to tc39 organization HOT 2
- Should `factorial` be defined in all or some domains? HOT 2
- Dot product?
- New rounding modes HOT 1
- Bitwise methods HOT 4
- Tuple methods
- `Number.prototype.toByteSize` / `Intl.SizeFormat` HOT 2
- `Math.copysign`? HOT 1
- `Math.MAX_RANDOM_BITS`
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from proposal-math-extensions.