Closed Bug 186562 Opened 23 years ago Closed 23 years ago

Number.toFixed: no "RangeError" exception is thrown

Categories

(Core :: JavaScript Engine, defect)

x86
Windows 2000
defect
Not set
major

Tracking

()

VERIFIED DUPLICATE of bug 90551

People

(Reporter: lapsap7+mz, Assigned: rogerl)

Details

User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.3a) Gecko/20021201 Build Identifier: Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.3a) Gecko/20021201 According to ECMA-262, 3rd Edition, PDF version, page 109: Number.toFixed(f) has to throw a "RangeError" exception when f < 0 or f > 20, but the following code shows that no exception is thrown and the code runs through, unfortunately: var a = 0.827; var b = 1.827; var f = 21; try { document.write( "a = " + a + " ==> a.toFixed(" + f + ") = " + a.toFixed(f) + "<br>" + "b = " + b + " ==> b.toFixed(" + f + ") = " + b.toFixed(f)) } catch(e) { alert(e.name) } For f < 0 in the above code, no exception is thrown either. Reproducible: Always Steps to Reproduce:
This issue came up once before, in bug 90551, "toFixed, toExponential and toPrecision not throwing range errors" The spec actually gives the language leeway to extend the range of digits. See http://www.mozilla.org/js/language/E262-3.pdf: ------------------------------------------------------------------------ 15.7.4.5 Number.prototype.toFixed (fractionDigits) Return a string containing the number represented in fixed-point notation with fractionDigits digits after the decimal point. If fractionDigits is undefined, 0 is assumed. Specifically, perform the following steps: 1. Let f be ToInteger(fractionDigits). (If fractionDigits is undefined, this step produces the value 0). 2. If f < 0 or f > 20, throw a RangeError exception. etc. etc. The length property of the toFixed method is 1. If the toFixed method is called with more than one argument, then the behaviour is undefined (see section 15). An implementation is permitted to extend the behaviour of toFixed for values of fractionDigits less than 0 or greater than 20. In this case toFixed would not necessarily throw RangeError for such values. ------------------------------------------------------------------------ *** This bug has been marked as a duplicate of 90551 ***
Status: NEW → RESOLVED
Closed: 23 years ago
Resolution: --- → DUPLICATE
Marking Verified; thank you for this report -
Status: RESOLVED → VERIFIED
I'm confused. The spec I get from ECMA official web site is still the 3rd Edition. How come there is a final edition and it resides in Mozilla website???
Good point. I was tripped up by that once, also: the ECMAScript spec published at ECMA's own site is not the final version !!! Whereas the spec at the Mozilla site is the final version. I will cc Waldemar to confirm this and answer your question if possible -
Phil is correct. Waldemar
Final version can't be found in official site. That's really illogical. I went to this page: http://www.ecma.ch/ecma1/STAND/ECMA-262.HTM and saw that I can send an email to documents@ecma.ch. I did so to ask this question and I got the answer: "ECMA-262 3rd Edition is the latest version."
> Final version can't be found in official site. That's really illogical. You are absolutely correct. I will work with Waldemar to see if we can correct this error at the ECMA site -
I looked at the E262 pdf on ECMA's site, and its description of toFixed seems to match the E262 pdf that I posted on the mozilla.org site. The two documents are formatted a little differently and the mozilla.org one has a larger list of acknowledgments, but what's the specific discrepancy? If it's just formatting and informative comments, then it's hard for me to complain to ECMA. If there is a normative difference somewhere, then I can get this fixed.
In December 1999 version found in ECMA site, there's precisely no this paragraph which changes a lot: An implementation is permitted to extend the behaviour of toFixed for values of fractionDigits less than 0 or greater than 20. In this case toFixed would not necessarily throw RangeError for such values. There might be other changes as well ...
I checked ftp://ftp.ecma.ch/ecma-st/Ecma-262.pdf and searched for the phrase "extend the behaviour of toFixed" and found it on the top of page 110 in the December 1999 version. It's a bit hard to catch, because it is presented after the 20-step algorithm, and the definitition of the |length|, etc.; but that's the way it appears in the Final Version, too. I know I missed it the first time around -
Oh, I see, sorry for that. But back to the question I ask in bug 90551, what's the valid range then? It can't be infinite.
In SpiderMonkey I allow up to about 100 digits after the decimal point. Waldemar
References: http://lxr.mozilla.org/mozilla/source/js/src/jsnum.c#377 http://lxr.mozilla.org/mozilla/source/js/src/jsnum.c#331 From the file jsnum.c, I found: static JSBool num_toFixed(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) { /*We allow a larger range of precision than ECMA requires; permitted by ECMA.*/ return num_to(cx, obj, argc, argv, rval, DTOSTR_FIXED, DTOSTR_FIXED, -20, MAX_PRECISION, 0); } where we have: #define MAX_PRECISION 100 static JSBool num_to(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval, JSDToStrMode zeroArgMode, JSDToStrMode oneArgMode, jsint precisionMin, jsint precisionMax, jsint precisionOffset) { etc. So it looks like the bounds are -20 to 100. I will have let the developers correct me if I've made a mistake. But this does seem to be right. Here is a session in the JS shell: js> var num = 1; js> num.toFixed(100); 1.00000000000000000000 etc. etc. 0000000000000000000 js> num.toFixed(101); 5: RangeError: precision 101 out of range js> num.toFixed(-20); 0 js> num.toFixed(-21); 8: RangeError: precision -21 out of range
You need to log in before you can comment on or make changes to this bug.