Dev.Opera - Follow the standards, break the rulesDev.Opera - Follow the standards, break the rules

Login

Lost password?

Forums » Article Discussions

Discuss the articles posted on Dev.Opera.

Note: You need to login to post in the forums. if you don't have an account you first need to sign up.

By chrispi anchor Wednesday, 31. October 2007, 12:02:39

Why I Love ECMAScript 4: Real Decimals

Have you ever noticed how calculations involving floating point numbers in JavaScript and other languages can sometimes turn out inaccurately, due to it's use of Binary floats? ECMAScript 4 fixes this by allowing us to use real decimals if desired - in this article, Chris Pine explores the topic.


( Read the article )

By JeroenH anchor Wednesday, 31. October 2007, 13:11:11

avatarDoes ES4 really work in the latest alpha (=9.5 beta)?

I tried this:
<script type="application/ecmascript;version=4">
{
  use decimal;
  
  var a = 0.1;    //  a is a decimal
  var b = 0.2;    //  b is a decimal
  var c = a + b;  //  c is a decimal (0.3)
alert(c);
}
</script>
, but it did not work...

By shadowk anchor Wednesday, 31. October 2007, 14:28:07

avataryou forgot to add the initial ( in every alert call :wink:

javascript:alert(0.6 + 0.2);

@JeroenH
ES4 isn't supported by anyone, it's just a guide at the moment

By chrismills O anchor Wednesday, 31. October 2007, 14:48:11

avatarNo, it doesn't - my apologies. It was me who got this wrong. It will work in future versions of Opera, but not right now. I've changed the article to reflect this.

By chrismills O anchor Wednesday, 31. October 2007, 14:53:35

avatarJeroen: I've fixed these too - they were in the original source code, but a weird parsing blip made them disappear. dev.opera.com is officially in the twilight zone today ;-)

By jax anchor Thursday, 1. November 2007, 17:34:00

avatarThis topic was recently covered by New Scientist's Feedback column. While it is less than certain JavaScript was the culprit, the non-decimal decimals almost certainly were.

Originally posted by Feedback:

Superfluous precision

THE reminder sent by UK phone company BT to a client of Derek Woodroffe stated: "This email is to advise you of an outstanding amount of £87.510000000000005 relating to the above BT Business Broadband account. Please pay your bill promptly as failure to do so could lead to a late payment charge of £10." Woodroffe is suggesting his client writes to his bank requesting a "much wider cheque book" because of the difficulty of fitting in the amount to be paid.

Meanwhile, Jonathan Privett tells us that his local Caffè Nero coffee shop in south London has posted in its window a licence from Lambeth council permitting the shop to have seating outside the premises, so long as the chairs leave 3.4500000000000002 metres of pavement free for pedestrians. Hopefully Caffè Nero is in possession of a sufficiently accurate tape measure.



The moral seems clear: If you don't want to be lampooned by a major international magazine, you better switch to ECMAScript 4.

By jmudya anchor Thursday, 15. November 2007, 11:08:18

avatarSuper that searched. That regarding corrections (Jeroen:) - for you did all turn out?

By fpmarin anchor Friday, 4. September 2009, 06:37:37

avatarYou can use Math.floor((0.1 + 0.2)*10.0 + 0.5)/10.0; // It yields 0.3


========================================================================


Originally posted by JeroenH:

Does ES4 really work in the latest alpha (=9.5 beta)?

I tried this:

<script type="application/ecmascript;version=4">
{
  use decimal;
  
  var a = 0.1;    //  a is a decimal
  var b = 0.2;    //  b is a decimal
  var c = a + b;  //  c is a decimal (0.3)
alert(c);
}
</script>
, but it did not work...

Moderators: pepelsbey | dstorey | mcx | operadev | chrismills | shwetankdixit | brucelawson | iheni | andreasbovens | zibin | mollydotcom