Bug 24156 - Unary + conversion to number fails for negative hexadecimal (-0xff) 0x
: Unary + conversion to number fails for negative hexadecimal (-0xff) 0x
Status: RESOLVED INVALID
Product: WebKit
Classification: Unclassified
Component: JavaScriptCore
: 528+ (Nightly build)
: All All
: P2 Normal
Assigned To: Cameron Zwarich (cpst)
http://groups.google.com/group/comp.l...
:
Depends on:
Blocks:
  Show dependency treegraph
 
Reported: 2009-02-25 01:47 PST by Jorge
Modified: 2011-09-07 00:54 PDT (History)
3 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Jorge 2009-02-25 01:47:21 PST
See: 
javascript:s= "0x12";alert((+s)+ ","+ parseInt(s,16)); //-> 18,18 
but: 
javascript:s= "-0x12";alert((+s)+ ","+ parseInt(s,16)); //-> NaN,-18
Comment 1 Cameron Zwarich (cpst) 2009-02-25 23:22:11 PST
This works as expected. See section 9.3.1 of the ECMA spec:

http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-262.pdf

The ToNumber operation (which is used by the unary plus operator) only handles negative signs for decimal numbers, not hex or octal. It might be a bit strange, but it's the spec.
Comment 2 Jorge 2009-02-26 01:03:21 PST
(In reply to comment #1)
>
> The ToNumber operation (which is used by the unary plus operator) only handles
> negative signs for decimal numbers, not hex or octal. It might be a bit
> strange, but it's the spec.

FF, Opera and Chrome handle them fine, and that might be perceived as "better". Better than the spec -if you want- but better, after all. Now, what's the best browser ? Not Safari when it comes to hex string conversion to number... IMO, in this case better is better than the spec. Safari is doing it to/in-spec, but worse.
Comment 3 Cameron Zwarich (cpst) 2009-02-26 07:50:25 PST
If it's a potential compatibility issue down the road, then it probably makes sense to match Firefox on this.
Comment 4 Gavin Barraclough 2011-09-07 00:54:05 PDT
This was a bug in other browsers, and fixed in the latest FireFox.  Behaves correctly.