Comments (5)
Wait, is
turnPrincipalBranch
calculating the normalized version of the angle? If so, what are the reasons why it might be desirable?
As mentioned in my opening comment above
As latitude and longitude are often expressed as decimal degrees in the intervals
[−90°, +90°]
and[−180°, +180°]
respectively, in my implementation I choose to ensure all return values are in an interval[-perigon/2, +perigon/2]
(similar toMath.asin
andMath.atan
). This reduces surprises and may help maintain precision across multiple floating-point operations. This convention equates an angle in radians with the directed minor arc from one point to another on the unit circle, which is useful in some distance calculations.With a suitable modulo operation (#21), it is simple for calling code to convert from
[-perigon/2, +perigon/2]
to[0, perigon]
if needed.
However, this is simply a choice I made in my implementation; a different choice could be made.
Aside: my use of the term "principal branch" may not be strictly accurate. If we wanted to consider θ
and θ + τ
as distinct angles then we're kinda getting a Riemann surface where concepts like branch cuts and principal branches apply (like with complex logarithms). This is quite advanced stuff and I guess most people would consider θ
and θ + τ
to be the same angle (like 6/4
and 3/2
are the same fraction). I can't think of any use cases that would depend on un-normalized angles.
from proposal-math-extensions.
Seems good to me
from proposal-math-extensions.
An example implementation of the two suggested functions
/**
* @private
* @const {number}
*/
var TAU = 2 * Math.PI;
/**
* @private
* @param {number} turns - An angle in turns.
* @return {number} The same angle in the left-closed interval from -0.5 to +0.5.
*/
function turnPrincipalBranch( turns ) {
return turns - Math.floor( turns + 0.5 );
}
/**
* @param {number} angle - The angle expressed in some unit (default is decimal degrees).
* @param {number=} perigon - The number of units in a full rotation (default perigon is 360).
* @return {number} The same angle expressed in radians in the left-closed interval from -π to +π.
*/
Math.toRadians = function ( angle, perigon ) {
return turnPrincipalBranch( angle / ( perigon || 360 ) ) * TAU;
};
/**
* @param {number} radians - The angle expressed in radians.
* @param {number=} perigon - The number of units in a full rotation (default perigon is 360).
* @return {number} The same angle expressed in some unit (default is decimal degrees).
*/
Math.fromRadians = function ( radians, perigon ) {
return turnPrincipalBranch( radians / TAU ) * ( perigon || 360 );
};
from proposal-math-extensions.
Before I realized this proposal existed (and this specific Issue) I was using these polyfills:
/**
converts degrees to radians by default
@param {number} x
@param {number} [y=360] the input scale
@return {number}
*/
Math.angleToRad = function(x, y = 360) {return TAU / +y * +x}
//scale = 360: degrees
//scale = 1: Tau radians
/**
converts radians to degrees by default
@param {number} x
@param {number} [y=360] the output scale
@return {number}
*/
Math.radToAngle = function(x, y = 360) {return +x / (TAU / +y)}
//unary plus is used to replicate the exact error type as the built-in Math methods
But I think the naming toRadians
and fromRadians
seems better. I just wanted to show the code to give more ideas and inspiration for other names, just in case they're changed
from proposal-math-extensions.
Wait, is turnPrincipalBranch
calculating the normalized version of the angle? If so, what are the reasons why it might be desirable? I think the function shouldn't normalize angles unless explicitly specified by the user/dev. We could add a 3rd bool arg, or just add the normalizer as a standalone Math
method
from proposal-math-extensions.
Related Issues (20)
- [Suggestion] Methods to be added HOT 8
- [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
- 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.