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.
SpiderMonkey Bug?
Moderators: winston, another_commander, Getafix
- Capt. Murphy
- Commodore
- Posts: 1127
- Joined: Fri Feb 25, 2011 8:46 am
- Location: UK South Coast.
SpiderMonkey Bug?
Last edited by Capt. Murphy on Thu Mar 22, 2012 8:46 am, edited 1 time in total.
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
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
- Eric Walch
- Slightly Grand Rear Admiral
- Posts: 5536
- Joined: Sat Jun 16, 2007 3:48 pm
- Location: Netherlands
Re: SpiderMonkey Bug?
No, when both variables are of type string, SpiderMonkey sees no reason to do a string conversion.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?
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
Code: Select all
file1
file2
file10
UPS-Courier & DeepSpacePirates & others at the box and some older versions
- Capt. Murphy
- Commodore
- Posts: 1127
- Joined: Fri Feb 25, 2011 8:46 am
- Location: UK South Coast.
Re: SpiderMonkey Bug?
Thanks Eric, that makes sense and matches what the MDN documentation says when I read it properly.Eric Walch wrote:No, when both variables are of type string, SpiderMonkey sees no reason to do a string conversion.
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
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
Re: SpiderMonkey Bug?
There's an old js 'trick' to make sure a number-like object is actually a number.
In this case you could have:
Ah, the magic of minus zero!
In this case you could have:
Code: Select all
x.toFixed(2)-0<=y.toFixed(2)-0
Hey, free OXPs: farsun v1.05 & tty v0.5! :0)
- Cody
- Sharp Shooter Spam Assassin
- Posts: 16081
- Joined: Sat Jul 04, 2009 9:31 pm
- Location: The Lizard's Claw
- Contact:
Re: SpiderMonkey Bug?
Love Minus Zero/No Limit... a fine old song!Kaks wrote:Ah, the magic of minus zero!
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!
And any survivors, their debts I will certainly pay. There's always a way!