Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Why aren't Math.ceil and Math.floor specified in terms of the spec's mathematical operations #1422

Closed
dead-claudia opened this issue Jan 19, 2019 · 8 comments
Labels

Comments

@dead-claudia
Copy link

dead-claudia commented Jan 19, 2019

In the mathematical operations part of the spec, there's a reference to a floor function, specified in terms of subtraction and modulo in its note. There is no corresponding ceil function, but it could be similarly specified as ceil(x) = -floor(-x).

Is there a reason these aren't used for Math.ceil and Math.floor? Similarly, is there a reason why max and min aren't used for Math.max or Math.min, respectively?

@ljharb

This comment has been minimized.

@dead-claudia

This comment has been minimized.

@ljharb
Copy link
Member

ljharb commented Jan 19, 2019

cc @allenwb; I'm assuming because it's tricky to talk about the boundaries between mathematical operations and the edge cases of JS number values (zeroes, infinities, NaN).

@dead-claudia
Copy link
Author

I think the Math.* algorithms could all use a little bit of cleanup because most non-implementation-defined algorithms are defined in prose, but they could be better and more clearly and precisely defined using algorithms.

  • Ones with algorithm-based definitions:

    • Math.clz32
    • Math.fround
    • Math.imul
    • Math.pow, in terms of the implementation-defined ** operator
  • Ones that should have algorithm-based definitions:

    • Math.abs, in terms of the abs function specified in the spec
    • Math.ceil, in terms of the ceil function specified the initial comment
    • Math.floor, in terms of the floor function specified in the spec
    • Math.max, in terms of the max function specified in the spec
    • Math.min, in terms of the min function specified in the spec
    • Math.round, through explicit comparisons
    • Math.sign, through explicit comparisons
    • Math.trunc, through explicit comparisons

@dead-claudia
Copy link
Author

I'm not touching things like Math.sin or Math.log1p, though: that's obviously implementation-defined stuff, and it's just easier to specify in prose with a few constraints.

@ljharb
Copy link
Member

ljharb commented Jan 19, 2019

These seem like reasonable editorial PRs to me.

@waldemarhorwat
Copy link

The mathematical floor function and Math.floor are different things. One works on mathematical real numbers (which don't have infinities and signed zeroes), the other on IEEE doubles, so you still need special cases for ±0, numbers between 0 and 1, etc. I suppose you could add several more rows to the table with the last one being the catchall "use the mathematical floor function".

@bakkot
Copy link
Contributor

bakkot commented Jan 24, 2021

The Math operations are specified using algorithms as of #2122, as suggested above. ceil and floor still bottom out in prose rather than the ceil and floor operations, but I don't think it would be an improvement to convert them, since as @waldemarhorwat says above they operate on different domains: using them would require converting to reals and back, which I think is unnecessary overhead.

As such I'm closing this, but feel free to reopen if you think there's still something to be done here.

@bakkot bakkot closed this as completed Jan 24, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants