
This page demonstrates a flaw in the Opera 3.0 Web browser. An apparent error in the JavaScript engine causes the Math.round method to produce incorrect results.
According to ECMA-262, if the fractional portion of a number is less than 0.5, the number is to be rounded down to the next lower integer. If the fractional portion is 0.5 or greater, the number is rounded up to the next higher integer.
In Opera, however, any number with a fractional portion greater than zero is rounded up to the next higher integer.
Here is a view of the JavaScript source. The script compiles a table of values and the results of two different but equivalent rounding methods (ECMA-262 states that Math.round (x) is equivalent to Math.floor (x + 0.5)).
The results of the script are shown in the next section.
<SCRIPT>
<!--
var lbound = -1.0;
var ubound = 1.0;
var i, steps = 200;
var value;
for (i = 0; i < steps; i++) {
value = lbound + i * ((ubound - lbound) / steps);
document.write ("<TR><TD>" + value + "</TD><TD>"
+ Math.round (value) + "</TD><TD>"
+ Math.floor (value + 0.5) + "</TD></TR>");
}
//-->
</SCRIPT>
The table below lists the results of the script's execution. In most browsers, the Math.round and Math.floor columns will be equal. In the Opera browser, however, the columns are not equal. In experiments conducted so far, the Math.floor column has been correct while the Math.round column has included erroneous values.
Note that, due to the inexact representation of many floating-point numbers, you may see values in the left column that are off by a small amount (1x10-15 or smaller).
| Original Value | Math.round (value) | Math.floor (value + 0.5) |
|---|