Skip to content Skip to sidebar Skip to footer

Ios Javascript Engine Parsefloat(1) Returns Negative Number

This code will make the bug appear: function causeBug(d) { var k; var n = parseFloat(1); var c = Math.abs(d); if (n < 0) { k = '-'; } else { k = '+'; } ret

Solution 1:

JIT issue perhaps? I found the implementation of Math.abs in the JavascriptCore source which I'm guessing is different from Nitro (ios js engine) or is perhaps marked as JIT executable.

Value MathFuncImp::call(ExecState *exec, Object &/*thisObj*/, constList &args)
{
  double arg = args[0].toNumber(exec);
  double arg2 = args[1].toNumber(exec);
  double result;

  switch (id) {
  case MathObjectImp::Abs:
    result = ( arg < 0 || arg == -0) ? (-arg) : arg;
    break;

This can be implemented directly in javascript. So you can replace your Math.abs calls with

result = ( arg < 0 || arg == -0) ? (-arg) : arg;

This will resolve the issue and be cross browser compatible.

Submitted Apple Bug: 16209709

Does anyone know how this kind of bug occurs or a better way to describe it?

Bit Flip? Memory Collision? Overflow of some sort?

Solution 2:

I suggest that you should use it in standard. for parseInt, it is better to pass second parameter like: parseInt(1, 10). Or in some IOS system, it will give you wrong answers.

For example, parseInt(8) will be traded as octal in IOS (it is weird..).

Post a Comment for "Ios Javascript Engine Parsefloat(1) Returns Negative Number"