constchar* DoubleToCString(double v, base::Vector<char> buffer){ switch (FPCLASSIFY_NAMESPACE::fpclassify(v)) { case FP_NAN: return"NaN"; case FP_INFINITE: return (v < 0.0 ? "-Infinity" : "Infinity"); case FP_ZERO: return"0"; default: { if (IsInt32Double(v)) { // This will trigger if v is -0 and -0.0 is stringified to "0". // (see ES section 7.1.12.1 #sec-tostring-applied-to-the-number-type) returnIntToCString(FastD2I(v), buffer); } SimpleStringBuilder builder(buffer.begin(), buffer.length()); int decimal_point; int sign; constint kV8DtoaBufferCapacity = base::kBase10MaximalLength + 1; char decimal_rep[kV8DtoaBufferCapacity]; int length;
不用过多解释, 已经很清晰了, FastD2I 就是 Fast Double to Integer的意思, 定义如下, 注释也很详尽:
1 2 3 4 5 6 7 8 9
// The fast double-to-(unsigned-)int conversion routine does not guarantee // rounding towards zero. // The result is undefined if x is infinite or NaN, or if the rounded // integer value is outside the range of type int. inlineintFastD2I(double x){ DCHECK(x <= INT_MAX); DCHECK(x >= INT_MIN); returnstatic_cast<int32_t>(x); }