SpiderMonkey Bug?

For test results, bug reports, announcements of new builds etc.

Moderators: winston, another_commander, Getafix

Post Reply
User avatar
Capt. Murphy
Commodore
Commodore
Posts: 1127
Joined: Fri Feb 25, 2011 8:46 am
Location: UK South Coast.

SpiderMonkey Bug?

Post by Capt. Murphy »

Hi,

Scenario 1

x = 110.0678
y = 120.0789

x = x.toFixed(2)
y= y.toFixed(2)

x<=y returns true as expected.

Scenario 2

x = 90.0678
y = 120.0789

x=x.toFixed(2)
y=y.toFixed(2)

x<=y returns false which appears to be a bug that occurs when the numbers have a different number of leading digits. I know that toFixed returns a string rather than a number, but shouldn't SpiderMonkey be converting the values to numbers on the fly?

Workaround is

parseFloat(x)<=parseFloat(y)

which returns true as it should in both situations.

or alternatively to

x= Number(x.toFixed(2))
y= Number(y.toFixed(2))

in which case x<=y returns the expected value in both situations.
Last edited by Capt. Murphy on Thu Mar 22, 2012 8:46 am, edited 1 time in total.
[EliteWiki] Capt. Murphy's OXPs
External JavaScript resources - W3Schools & Mozilla Developer Network
Win 7 64bit, Intel Core i5 with HD3000 (driver rev. 8.15.10.2696 - March 2012), Oolite 1.76.1
User avatar
Eric Walch
Slightly Grand Rear Admiral
Slightly Grand Rear Admiral
Posts: 5536
Joined: Sat Jun 16, 2007 3:48 pm
Location: Netherlands

Re: SpiderMonkey Bug?

Post by Eric Walch »

Capt. Murphy wrote:
x<=y returns false which appears to be a bug that occurs when the numbers have a different number of leading digits. I know that toFixed returns a string rather than a number, but shouldn't SpiderMonkey be converting the values to numbers on the fly?
No, when both variables are of type string, SpiderMonkey sees no reason to do a string conversion.

This is a more general bug of string comparisons algorithms. When I sort on files by name, I also get the order:

Code: Select all

file1
file10
file2
Im sure there is a hook to fix that, as with the classic mac operating system, I had a system tool that changes the string sorting order so the files were correctly sorted as:

Code: Select all

file1
file2
file10
I have no idea why that is not the default string sorting order and I would like to have this behaviour back with OSX10 :wink: I am sure that when you could add custom string sort routines in classic mac os, this must also be possible in the current os.
User avatar
Capt. Murphy
Commodore
Commodore
Posts: 1127
Joined: Fri Feb 25, 2011 8:46 am
Location: UK South Coast.

Re: SpiderMonkey Bug?

Post by Capt. Murphy »

Eric Walch wrote:
No, when both variables are of type string, SpiderMonkey sees no reason to do a string conversion.
Thanks Eric, that makes sense and matches what the MDN documentation says when I read it properly.
[EliteWiki] Capt. Murphy's OXPs
External JavaScript resources - W3Schools & Mozilla Developer Network
Win 7 64bit, Intel Core i5 with HD3000 (driver rev. 8.15.10.2696 - March 2012), Oolite 1.76.1
User avatar
Kaks
Quite Grand Sub-Admiral
Quite Grand Sub-Admiral
Posts: 3009
Joined: Mon Jan 21, 2008 11:41 pm
Location: The Big Smoke

Re: SpiderMonkey Bug?

Post by Kaks »

There's an old js 'trick' to make sure a number-like object is actually a number.
In this case you could have:

Code: Select all

x.toFixed(2)-0<=y.toFixed(2)-0
Ah, the magic of minus zero! :D
Hey, free OXPs: farsun v1.05 & tty v0.5! :0)
User avatar
Cody
Sharp Shooter Spam Assassin
Sharp Shooter Spam Assassin
Posts: 16081
Joined: Sat Jul 04, 2009 9:31 pm
Location: The Lizard's Claw
Contact:

Re: SpiderMonkey Bug?

Post by Cody »

Kaks wrote:
Ah, the magic of minus zero!
Love Minus Zero/No Limit... a fine old song!
I would advise stilts for the quagmires, and camels for the snowy hills
And any survivors, their debts I will certainly pay. There's always a way!
Post Reply