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

Is -0 < 0, regarding [EnforceRange]? #306

Closed
domenic opened this issue Feb 11, 2017 · 10 comments · Fixed by #316
Closed

Is -0 < 0, regarding [EnforceRange]? #306

domenic opened this issue Feb 11, 2017 · 10 comments · Fixed by #316

Comments

@domenic
Copy link
Member

domenic commented Feb 11, 2017

[EnforceRange] will throw a TypeError

If x < lowerBound or x > upperBound, then throw a TypeError.

If lowerBound is 0 and x is -0, will this throw? Note that -0 < 0 is false in JavaScript.

The answer might be well-defined by ECMAScript conventions, but we should check that it is, and in any case we should add a note about this explicit case.

Note that the [Clamp] and no-extended-attribute cases don't have this problem as they explicitly censor -0 to 0. Although I guess clamp is a bit ambiguous, if "round -0 to the nearest integer choosing 0 instead of -0" will in fact change -0 to 0.

@domenic
Copy link
Member Author

domenic commented Feb 11, 2017

One of the simplest ways to clear this up might be for the top of the algorithm to just immediately set x to +0 if x is -0.

domenic added a commit to jsdom/webidl-conversions that referenced this issue Feb 11, 2017
It was pointed out in 5f1e6ce#commitcomment-20847302 that the tests for these cases weren't working. Fix the tests, and then fix the code correspondingly.

Note that the spec isn't super-clear on this point; see whatwg/webidl#306.
@bzbarsky
Copy link
Collaborator

If lowerBound is 0 and x is -0, will this throw?

I would hope not!

Note that -0 < 0 is false in JavaScript.

Sure. It's false in IEEE-754 in general, I believe. That is, -0 < 0 is false in C/C++/Rust/etc as well, as far as I know.

But we could be pretty explicit about this, sure.

@annevk
Copy link
Member

annevk commented Feb 14, 2017

I guess we should say we use IEEE-754. Might become more important with wasm bindings, though just spitballing here.

@bzbarsky
Copy link
Collaborator

Well. The return value of ToNumber is an ES Number, and those are defined https://tc39.github.io/ecma262/#sec-ecmascript-language-types-number-type as:

The Number type has exactly 18437736874454810627 (that is, 2^64-2^53+3) values, representing the double-precision 64-bit format IEEE 754-2008 values as specified in the IEEE Standard for Binary Floating-Point Arithmetic, except that the 9007199254740990 (that is, 2^53-2) distinct “Not-a-Number” values of the IEEE Standard are represented in ECMAScript as a single special NaN value.

We could try to be more explicit here, I guess, but we're working with a Number value here....

@domenic
Copy link
Member Author

domenic commented Feb 14, 2017

It's just unclear to me (both here and in the ES spec) whether when you do thinks like <, max(), 2bitLength, etc. you are dealing with mathematical numbers or IEEE-754 numbers. In particular 264 is not accurately representable with IEEE-754 so given "x < 264" it seems like the left-hand side is IEEE-754 and the right-hand side is mathematical, so I'm not clear in general whether < is mathematical or IEEE-754.

@bzbarsky
Copy link
Collaborator

Ah, that's a good point. This part is definitely sucky. :(

@tobie
Copy link
Collaborator

tobie commented Feb 22, 2017

In particular 264 is not accurately representable with IEEE-754 so given "x < 264" it seems like the left-hand side is IEEE-754 and the right-hand side is mathematical, so I'm not clear in general whether < is mathematical or IEEE-754.

We never have that case, though. First step of the algorithm says:

  1. If bitLength is 64, then:
    1. Let upperBound be 253 − 1.

@tobie
Copy link
Collaborator

tobie commented Feb 22, 2017

Oh. Nevermind. We have similar issues in step 9 and 10:

  1. Set x to x modulo 2bitLength.
  2. If signedness is "signed" and x ≥ 2bitLength − 1, then return x − 2bitLength.

:-/

@tobie
Copy link
Collaborator

tobie commented Feb 22, 2017

Digging into this a bit further, from § 5.2 Algorithm Conventions in the ES spec says:

Mathematical operations such as addition, subtraction, negation, multiplication, division, and the mathematical functions defined later in this clause should always be understood as computing exact mathematical results on mathematical real numbers, which unless otherwise noted do not include infinities and do not include a negative zero that is distinguished from positive zero.

So I assume:

  1. If x < 264, then …

really means:

  1. Let x' be the mathematical real number that has the same numeric value as x.
  2. If x' < 264, then …

If this assumption holds, I could add a little bit of text at the beginning of § 3.2.4. Integer types that paraphrases the above quote and links to its source.

@domenic
Copy link
Member Author

domenic commented Feb 22, 2017

But how would you interpret "return x" then? Does it allow returning -0? Again that seems unlikely since a Web IDL byte type (say) doesn't have a -0.

tobie added a commit to tobie/webidl that referenced this issue Feb 22, 2017
Explicitly convert +0 and −0 to 0.

Closes whatwg#306.
tobie added a commit that referenced this issue Feb 22, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging a pull request may close this issue.

4 participants