Esempio n. 1
0
Variant f_abs(CVarRef number) {
  int64_t ival;
  double dval;
  DataType k = number.toNumeric(ival, dval, true);
  if (k == KindOfDouble) {
    return fabs(dval);
  } else if (k == KindOfInt64) {
    return ival >= 0 ? ival : -ival;
  } else {
    return 0;
  }
}
Esempio n. 2
0
Numeric f_pow(CVarRef base, CVarRef exp) {
  int64_t bint, eint;
  double bdbl, edbl;
  DataType bt = base.toNumeric(bint, bdbl, true);
  DataType et = exp.toNumeric(eint, edbl, true);
  if (bt == KindOfInt64 && et == KindOfInt64 && eint >= 0) {
    if (eint == 0) return 1LL;
    if (bint == 0) return 0LL;

    // calculate pow(long,long) in O(log exp) operations, bail if overflow
    int64_t l1 = 1;
    while (eint >= 1) {
      int overflow;
      double dval = 0.0;
      if (eint % 2) {
        --eint;
        ZEND_SIGNED_MULTIPLY_LONG(l1, bint, l1, dval, overflow);
        if (overflow) return dval * pow(bint, eint);
      } else {
        eint /= 2;
        ZEND_SIGNED_MULTIPLY_LONG(bint, bint, bint, dval, overflow);
        if (overflow) return (double)l1 * pow(dval, eint);
      }
      if (eint == 0) {
        return l1;
      }
    }
  }
  if (bt != KindOfDouble) {
    bdbl = base.toDouble();
  }
  if (et != KindOfDouble) {
    edbl = exp.toDouble();
  }
  return pow(bdbl, edbl);
}
Esempio n. 3
0
double f_round(CVarRef val, int64 precision /* = 0 */) {
  int64 ival;
  double dval;
  DataType k = val.toNumeric(ival, dval, true);
 if (k == KindOfInt64) {
   if (precision >= 0) {
    return ival;
   } else {
     dval = ival;
   }
 } else if (k != KindOfDouble) {
   dval = val.toDouble();
 }
 PHP_ROUND_WITH_FUZZ(dval, precision);
 return dval;
}
Esempio n. 4
0
double f_round(CVarRef val, int64_t precision /* = 0 */,
               int64_t mode /* = PHP_ROUND_HALF_UP */) {
  int64_t ival;
  double dval;
  DataType k = val.toNumeric(ival, dval, true);
  if (k == KindOfInt64) {
    if (precision >= 0) {
     return ival;
    } else {
      dval = ival;
    }
  } else if (k != KindOfDouble) {
    dval = val.toDouble();
  }
  dval = php_math_round(dval, precision, mode);
  return dval;
}