.toFixed() bug in MS JScript script engine 
Author Message
 .toFixed() bug in MS JScript script engine

For values between 0.5 and 0.94, .toFixed() doesn't
work properly in IE6 (possibly this occurs also in IE5).
I've tried some tests and concluded that the problem is in
MS JScript script engine in general, thus the problem will
present in IE as well as in ASP / WSH.

Try the follow code in IE and you'll see:

var a = 0.94
alert(a.toFixed())

     Or the following for WSH

var a = 0.94
WScript.Echo(a.toFixed())



Tue, 23 Aug 2005 01:55:54 GMT  
 .toFixed() bug in MS JScript script engine


Quote:
> For values between 0.5 and 0.94, .toFixed() doesn't
> work properly in IE6 (possibly this occurs also in IE5).
> I've tried some tests and concluded that the problem is in
> MS JScript script engine in general, thus the problem will
> present in IE as well as in ASP / WSH.

> Try the follow code in IE and you'll see:

> var a = 0.94
> alert(a.toFixed())

>      Or the following for WSH

> var a = 0.94
> WScript.Echo(a.toFixed())

Per the JScript documentation:
Arguments
  numObj
  Required A Number object.
  fractionDigits
  Optional. Number of digits after the decimal point. Must be in the range
0 - 20, inclusive.
Remarks
The toFixed method returns a string representation of a number in
fixed-point notation. The string contains one digit before the significand's
decimal point, and must contain fractionDigits digits after it.

If fractionDigits is not supplied or undefined, the toFixed method assumes
the value is zero.

Thus, 0.94 to zero digits after the decimal (i.e. "0").

var a = 0.94;
WScript.Echo(a.toFixed());        // "0"
WScript.Echo(a.toFixed( 1 ));    // "0.9"
WScript.Echo(a.toFixed( 2 ));    // "0.94"
WScript.Echo(a.toFixed( 3 ));    // "0.940"

Dale Hurtt



Tue, 23 Aug 2005 02:44:12 GMT  
 .toFixed() bug in MS JScript script engine
I think the point here is that (0.94).toFixed() is supposed to return "1"

Consider:
    (0.94).toFixed() returns "0"
    (0.95).toFixed() returns "1"
    (1.94).toFixed() returns "2"

Similar results with negative numbers:
    (-0.94).toFixed() returns "0"
    (-0.95).toFixed() returns "-1"

Quote:

> If fractionDigits is not supplied or undefined, the toFixed method assumes
> the value is zero.

> Thus, 0.94 to zero digits after the decimal (i.e. "0").

> var a = 0.94;
> WScript.Echo(a.toFixed());        // "0"
> WScript.Echo(a.toFixed( 1 ));    // "0.9"
> WScript.Echo(a.toFixed( 2 ));    // "0.94"
> WScript.Echo(a.toFixed( 3 ));    // "0.940"

--
Dave Anderson

Unsolicited commercial email will be read at a cost of $500 per message. Use of
this email address implies consent to these terms. Please do not contact me
directly or ask me to contact you directly for assistance. If your question is
worth asking, it's worth posting.



Tue, 23 Aug 2005 02:55:59 GMT  
 .toFixed() bug in MS JScript script engine

Quote:



>> For values between 0.5 and 0.94, .toFixed() doesn't
>> work properly in IE6 (possibly this occurs also in IE5).
>> I've tried some tests and concluded that the problem is in
>> MS JScript script engine in general, thus the problem will
>> present in IE as well as in ASP / WSH.

>> Try the follow code in IE and you'll see:

>> var a = 0.94
>> alert(a.toFixed())

>>      Or the following for WSH

>> var a = 0.94
>> WScript.Echo(a.toFixed())

> Per the JScript documentation:
> Arguments
>   numObj
>   Required A Number object.
>   fractionDigits
>   Optional. Number of digits after the decimal point. Must be in the range
> 0 - 20, inclusive.
> Remarks
> The toFixed method returns a string representation of a number in
> fixed-point notation. The string contains one digit before the significand's
> decimal point, and must contain fractionDigits digits after it.

> If fractionDigits is not supplied or undefined, the toFixed method assumes
> the value is zero.

> Thus, 0.94 to zero digits after the decimal (i.e. "0").

> var a = 0.94;
> WScript.Echo(a.toFixed());        // "0"
> WScript.Echo(a.toFixed( 1 ));    // "0.9"
> WScript.Echo(a.toFixed( 2 ));    // "0.94"
> WScript.Echo(a.toFixed( 3 ));    // "0.940"

Then explain the third string produced by:

  var a = 0.906;
  WScript.Echo(a.toFixed(0), a.toFixed(1), a.toFixed(2), a.toFixed(3));

If toFixed truncates, the string should be "0.90". If toFixed rounds, the first
string should be "1". Either way, JScript's implementation of toFixed returns
inconsistent results (i.e. is buggy).

--
Steve

Speech is conveniently located midway between thought and action, where it
often substitutes for both. -John Andrew Holmes



Tue, 23 Aug 2005 03:05:11 GMT  
 .toFixed() bug in MS JScript script engine


Quote:
> I think the point here is that (0.94).toFixed() is supposed to return "1"

Why would you think that? Both the JScript documentation and the ECMAScript
standards indicate that no rounding as you suggest occurs. The method
Number.toFixed ( ) (from the standard 15.7.4.5):

"Returns a string containing the number represented in fixed-point notation
with fractionDigits digits after the decimal point. If fractionDigits is
undefined, 0 is assumed."

The integer portion of 0.94 is "0" (see the standard above, step 11). The
fractional part is "94", but with zero digits showing it translates to "". 0
+ "" is "0".

Perhaps Math.ceil ( 0.94) will return the value he wants... But, toFixed
does not appear to have a "bug".

Dale Hurtt



Tue, 23 Aug 2005 04:08:18 GMT  
 .toFixed() bug in MS JScript script engine
That's a ridiculous argument. If toFixed(0) were supposed to return *unrounded*
values, (0.95).toFixed() would equal (0.94).toFixed(), just as (1.95).toFixed()
exactly equals (1.94).toFixed().

And I'm glad you brought up ECMA, because we can step through the ECMA
specification with (0.94).toFixed():

: 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).

f = 0

:     2. If f < 0 or f > 20, throw a RangeError exception.
:     3. Let x be this number value.

x = 0.94

:     4. If x is NaN, return the string "NaN".
:     5. Let s be the empty string.

s = ""

:     6. If x >= 0, go to step 9.

Skipping to step 9...

:     7. Let s be "-".
:     8. Let x = -x.
:     9. If x >= 10^21, let m = ToString(x) and go to step 20.

0.94 < 10^21, so continue...

:    10. Let n be an integer for which the exact mathematical value of
:        n/10^f - x is as close to zero as possible. If there are two
:        such n, pick the larger n.

n/10^0 = n/1 = n, which must be the closest integer to 0.94. This is the
rounding step. Since f is 0, this says "pick the closest integer to x, rounding
up". Thus, n = 1.

:    11. If n = 0, let m be the string "0". Otherwise, let m be the
:        string consisting of the digits of the decimal representation
:        of n (in order, with no leading zeroes).

m = "1"

:    12. If f = 0, go to step 20.

f = 0, so let's go to step 20...

:    13. Let k be the number of characters in m.
:    14. If k > f, go to step 18.
:    15. Let z be the string consisting of f+1-k occurrences of the
:        character "0".
:    16. Let m be the concatenation of strings z and m.
:    17. Let k = f + 1.
:    18. Let a be the first kCf characters of m, and let b be the
:        remaining f characters of m.
:    19. Let m be the concatenation of the three strings a, ".",
:        and b.
:    20. Return the concatenation of the strings s and m.

return value: ("" + "1")

Thus, JScript does not conform to the ECMA 262 spec for Number.toFixed(). Could
not be more evident.

Quote:

> Why would you think that? Both the JScript documentation and the ECMAScript
> standards indicate that no rounding as you suggest occurs. The method
> Number.toFixed ( ) (from the standard 15.7.4.5):

> "Returns a string containing the number represented in fixed-point notation
> with fractionDigits digits after the decimal point. If fractionDigits is
> undefined, 0 is assumed."

> The integer portion of 0.94 is "0" (see the standard above, step 11). The
> fractional part is "94", but with zero digits showing it translates to "". 0
> + "" is "0".

This is where you read the spec incorrectly. Step 11 does not look at the
integer portion of x, it looks at the computed value n, which is 1.

Quote:
> Perhaps Math.ceil (0.94) will return the value he wants...
> But, toFixed does not appear to have a "bug".

On the contrary, it has a demonstrated and illustrated bug.

--
Dave Anderson

Unsolicited commercial email will be read at a cost of $500 per message. Use of
this email address implies consent to these terms. Please do not contact me
directly or ask me to contact you directly for assistance. If your question is
worth asking, it's worth posting.



Tue, 23 Aug 2005 04:56:58 GMT  
 .toFixed() bug in MS JScript script engine


Quote:
> :    10. Let n be an integer for which the exact mathematical value of
> :        n/10^f - x is as close to zero as possible. If there are two
> :        such n, pick the larger n.

> n/10^0 = n/1 = n, which must be the closest integer to 0.94. This is the
> rounding step. Since f is 0, this says "pick the closest integer to x,
rounding
> up". Thus, n = 1.

I stand corrected. I misread the formula as "n/10^f - x" (I saw the em dash
in my PDF as a minus). [snort] When I did the math using the steps, using
the incorrect formula, the integer would always have to be zero. Kind of
skews the results, huh.

My bust.

Dale Hurtt



Tue, 23 Aug 2005 05:43:35 GMT  
 .toFixed() bug in MS JScript script engine



Quote:
>For values between 0.5 and 0.94, .toFixed() doesn't
>work properly in IE6 (possibly this occurs also in IE5).
>I've tried some tests and concluded that the problem is in
>MS JScript script engine in general, thus the problem will
>present in IE as well as in ASP / WSH.

See <URL:http://www.merlyn.demon.co.uk/js-round.htm#RoundS> para 2.

--

  <URL:http://www.jibbering.com/faq/> FAQ for comp.lang.javascript by Jim Ley.
  <URL:http://www.merlyn.demon.co.uk/js-index.htm>   JS maths, dates, sources.
  <URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.



Tue, 23 Aug 2005 05:48:26 GMT  
 .toFixed() bug in MS JScript script engine
   First, thanks to Steve Fulton and Dave Anderson for
defending my point during my absence :-)

   I have to say sorry because my mail was too short and
unclear.  Yes, the problem is that values between 0.5 and
0.94 aren't rounded to 1 using toFixed.  Consider this :

Example 1 :
(0.94).toFixed(0) gives 0 whilst
(0.95).toFixed(0) gives 1 !!

Example 2 :
(0.88).toFixed(0) gives 0 whilst
(1.88).toFixed(0) gives 2 !!

   I think now everybody agrees that toFixed is buggy, and
now the most important question is :
What could we do to make Microsoft aware of this problem?

   This problem is quite serious because it affects
everything using JScript engine, from IE to ASP through
WSH.  My computer is making a kind of accounting Web
application and this bug is very very annoying.  I just
couldn't tell my clients that IE is buggy and there's
nothing I could do.  Nobody would be happy about this.  If
the solution isn't available in the following months, I
think I'm forced to tell my clients to use Netscape 7
because, sorry to Microsoft supporters and fans, I find
NS7 is far more superior than IE6.

     I saw that Dr. Stockton has suggested me something
but I'll need time to see what it is.  Thanks in advance.



Thu, 25 Aug 2005 04:20:09 GMT  
 .toFixed() bug in MS JScript script engine
Seak Teng-Fong wrote on 08 mrt 2003 in microsoft.public.scripting.jscript:

Quote:
> This problem is quite serious because it affects
> everything using JScript engine

Final solution:

DO NOT USE .tofixed

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)



Thu, 25 Aug 2005 04:44:52 GMT  
 .toFixed() bug in MS JScript script engine



Quote:
>     I saw that Dr. Stockton has suggested me something
>but I'll need time to see what it is.  Thanks in advance.

I have in js-round.htm a list of test values which seem good at bringing
out defects in various code for formatting numbers to strings.

It could be useful to have a number that illustrates each defect of
toFixed; also to have a careful description so accurate that it could be
used to write a matching toFixed.

Banker's rounding in toFixed(0) is not necessarily an unintentional
defect.

--

  <URL:http://www.jibbering.com/faq/> FAQ for comp.lang.javascript by Jim Ley.
  <URL:http://www.merlyn.demon.co.uk/js-index.htm>   JS maths, dates, sources.
  <URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.



Fri, 26 Aug 2005 07:00:10 GMT  
 .toFixed() bug in MS JScript script engine

Quote:
>Final solution:

>DO NOT USE .tofixed

   I don't agree.  Do not use .toFixed should only be a
temporary solution, but not a _final_ solution.  On one
hand, the standard suggests this method, and if it's badly
implemented, it's the fault of the implementor.  Thus,
Microsoft has no excuse not to repair its fault.

   On the other hand, actually I'm not the one who found
this bug.  I've got to know it in a JavaScript mailing-
list.  There're a lot of people out there using this
method without knowing this serious bug.  It's the
implementor's responsibility to fix it.



Fri, 26 Aug 2005 17:36:40 GMT  
 .toFixed() bug in MS JScript script engine
Seak Teng-Fong wrote on 10 mrt 2003 in microsoft.public.scripting.jscript:

Quote:
> On the other hand, actually I'm not the one who found
> this bug.  I've got to know it in a JavaScript mailing-
> list.  There're a lot of people out there using this
> method without knowing this serious bug.  It's the
> implementor's responsibility to fix it.

You are right.

The only trouble is, that lots off bad fixes using .tofixed will create
havoc when half of the browsers are "fixed" and half not.

So do not use .tofixed till all updates have reached 99% of the users.

That will be in 5 years ????

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)



Fri, 26 Aug 2005 17:58:25 GMT  
 .toFixed() bug in MS JScript script engine
This is a ridiculous argument. If Microsoft has incorrectly implemented
Number.toFixed(), then Microsoft should not compound the error by waiting to
correct it.

Judging by (a) the nature of the problem, and (b) the amount of misunderstanding
displayed in this group, I seriously doubt whether *anyone* is exploiting the
error for some collateral purpose.

The more likely scenario is that the existing "fixes" are self-written
extensions to the language (or simple functions) that will continue to perform
correctly if/when Microsoft gets off its behind and corrects this deficiency.

And repairing the error will result in immediate relief for those of us who want
to use it on the SERVER side.

Quote:

> The only trouble is, that lots off bad fixes using .tofixed
> will create havoc when half of the browsers are "fixed" and
> half not.

> So do not use .tofixed till all updates have reached 99% of
> the users.

--
Dave Anderson

Unsolicited commercial email will be read at a cost of $500 per message. Use of
this email address implies consent to these terms. Please do not contact me
directly or ask me to contact you directly for assistance. If your question is
worth asking, it's worth posting.



Sat, 27 Aug 2005 03:22:32 GMT  
 .toFixed() bug in MS JScript script engine
Dave Anderson wrote on 10 mrt 2003 in
microsoft.public.scripting.jscript:

Quote:

>> The only trouble is, that lots off bad fixes using .tofixed
>> will create havoc when half of the browsers are "fixed" and
>> half not.

>> So do not use .tofixed till all updates have reached 99% of
>> the users.
> This is a ridiculous argument. If Microsoft has incorrectly
> implemented Number.toFixed(), then Microsoft should not compound the
> error by waiting to correct it.

It is no argument at all against repairing, because I never said it should
not be repaired.

It was my argument for not using it at all (Yes indeed clientside) for the
next 5 years.

It is a function I can do without easily, even serverside.

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)



Sat, 27 Aug 2005 03:40:58 GMT  
 
 [ 29 post ]  Go to page: [1] [2]

 Relevant Pages 

1. bug in the MS JScript engine?

2. running different script engines(vbscript, jscript) with script control

3. VB Script engine variable dec bug? (Workaround Needed)

4. IActiveScript Script Engine and JScript Events

5. Windows JScript scripting engine

6. JScript bug on IE with Script Debugger or MDE Loaded

7. Restrictions of JScript in MS Script Control?

8. questions: printers,engines,bugs

9. Bug in Remote Scripting Caused by Javascript bug?

10. Script MS-Access Import of MS-Excel?

11. Error: ActiveX Scripting was not able to initialize the script execution engine

12. Cannot find script engine VBScript for script

 

 
Powered by phpBB® Forum Software