| 31 | static const int64_t kUnitsSize = arraysize(kBigSIUnits); |
| 32 | |
| 33 | void ToExponentAndMantissa(double val, double thresh, int precision, |
| 34 | double one_k, std::string* mantissa, |
| 35 | int64_t* exponent) { |
| 36 | std::stringstream mantissa_stream; |
| 37 | |
| 38 | if (val < 0) { |
| 39 | mantissa_stream << "-"; |
| 40 | val = -val; |
| 41 | } |
| 42 | |
| 43 | // Adjust threshold so that it never excludes things which can't be rendered |
| 44 | // in 'precision' digits. |
| 45 | const double adjusted_threshold = |
| 46 | std::max(thresh, 1.0 / std::pow(10.0, precision)); |
| 47 | const double big_threshold = adjusted_threshold * one_k; |
| 48 | const double small_threshold = adjusted_threshold; |
| 49 | // Values in ]simple_threshold,small_threshold[ will be printed as-is |
| 50 | const double simple_threshold = 0.01; |
| 51 | |
| 52 | if (val > big_threshold) { |
| 53 | // Positive powers |
| 54 | double scaled = val; |
| 55 | for (size_t i = 0; i < arraysize(kBigSIUnits); ++i) { |
| 56 | scaled /= one_k; |
| 57 | if (scaled <= big_threshold) { |
| 58 | mantissa_stream << scaled; |
| 59 | *exponent = i + 1; |
| 60 | *mantissa = mantissa_stream.str(); |
| 61 | return; |
| 62 | } |
| 63 | } |
| 64 | mantissa_stream << val; |
| 65 | *exponent = 0; |
| 66 | } else if (val < small_threshold) { |
| 67 | // Negative powers |
| 68 | if (val < simple_threshold) { |
| 69 | double scaled = val; |
| 70 | for (size_t i = 0; i < arraysize(kSmallSIUnits); ++i) { |
| 71 | scaled *= one_k; |
| 72 | if (scaled >= small_threshold) { |
| 73 | mantissa_stream << scaled; |
| 74 | *exponent = -static_cast<int64_t>(i + 1); |
| 75 | *mantissa = mantissa_stream.str(); |
| 76 | return; |
| 77 | } |
| 78 | } |
| 79 | } |
| 80 | mantissa_stream << val; |
| 81 | *exponent = 0; |
| 82 | } else { |
| 83 | mantissa_stream << val; |
| 84 | *exponent = 0; |
| 85 | } |
| 86 | *mantissa = mantissa_stream.str(); |
| 87 | } |
| 88 | |
| 89 | std::string ExponentToPrefix(int64_t exponent, bool iec) { |
| 90 | if (exponent == 0) return ""; |
no test coverage detected