float js::math_roundf_impl(float x) { int32_t ignored; if (NumberIsInt32(x, &ignored)) return x; /* Some numbers are so big that adding 0.5 would give the wrong number. */ if (ExponentComponent(x) >= int_fast16_t(FloatingPoint<float>::ExponentShift)) return x; return js_copysign(floorf(x + 0.5f), x); }
double js::math_round_impl(double x) { int32_t ignored; if (NumberIsInt32(x, &ignored)) return x; /* Some numbers are so big that adding 0.5 would give the wrong number. */ if (ExponentComponent(x) >= int_fast16_t(FloatingPoint<double>::kExponentShift)) return x; double add = (x >= 0) ? GetBiggestNumberLessThan(0.5) : 0.5; return js_copysign(floor(x + add), x); }