| 1 | // Tencent is pleased to support the open source community by making RapidJSON available. |
| 2 | // |
| 3 | // Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. |
| 4 | // |
| 5 | // Licensed under the MIT License (the "License"); you may not use this file except |
| 6 | // in compliance with the License. You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://opensource.org/licenses/MIT |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software distributed |
| 11 | // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR |
| 12 | // CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 13 | // specific language governing permissions and limitations under the License. |
| 14 | |
| 15 | #ifndef RAPIDJSON_POW10_ |
| 16 | #define RAPIDJSON_POW10_ |
| 17 | |
| 18 | #include "../rapidjson.h" |
| 19 | |
| 20 | RAPIDJSON_NAMESPACE_BEGIN |
| 21 | namespace internal { |
| 22 | |
| 23 | //! Computes integer powers of 10 in double (10.0^n). |
| 24 | /*! This function uses lookup table for fast and accurate results. |
| 25 | \param n non-negative exponent. Must <= 308. |
| 26 | \return 10.0^n |
| 27 | */ |
| 28 | inline double Pow10(int n) { |
| 29 | static const double e[] = { // 1e-0...1e308: 309 * 8 bytes = 2472 bytes |
| 30 | 1e+0, |
| 31 | 1e+1, 1e+2, 1e+3, 1e+4, 1e+5, 1e+6, 1e+7, 1e+ |
| 32 | 1e+21, 1e+22, 1e+23, 1e+24, 1e+25, 1e+26, 1e+27, 1e+ |
| 33 | 1e+41, 1e+42, 1e+43, 1e+44, 1e+45, 1e+46, 1e+47, 1e+ |
| 34 | 1e+61, 1e+62, 1e+63, 1e+64, 1e+65, 1e+66, 1e+67, 1e+ |
| 35 | 1e+81, 1e+82, 1e+83, 1e+84, 1e+85, 1e+86, 1e+87, 1e+ |
| 36 | 1e+101,1e+102,1e+103,1e+104,1e+105,1e+106,1e+107,1e+ |
| 37 | 1e+121,1e+122,1e+123,1e+124,1e+125,1e+126,1e+127,1e+ |
| 38 | 1e+141,1e+142,1e+143,1e+144,1e+145,1e+146,1e+147,1e+ |
| 39 | 1e+161,1e+162,1e+163,1e+164,1e+165,1e+166,1e+167,1e+ |
| 40 | 1e+181,1e+182,1e+183,1e+184,1e+185,1e+186,1e+187,1e+ |
| 41 | 1e+201,1e+202,1e+203,1e+204,1e+205,1e+206,1e+207,1e+ |
| 42 | 1e+221,1e+222,1e+223,1e+224,1e+225,1e+226,1e+227,1e+ |
| 43 | 1e+241,1e+242,1e+243,1e+244,1e+245,1e+246,1e+247,1e+ |
| 44 | 1e+261,1e+262,1e+263,1e+264,1e+265,1e+266,1e+267,1e+ |
| 45 | 1e+281,1e+282,1e+283,1e+284,1e+285,1e+286,1e+287,1e+ |
| 46 | 1e+301,1e+302,1e+303,1e+304,1e+305,1e+306,1e+307,1e+ |
| 47 | }; |
| 48 | RAPIDJSON_ASSERT(n >= 0 && n <= 308); |
| 49 | return e[n]; |
| 50 | } |
| 51 | |
| 52 | } // namespace internal |
| 53 | RAPIDJSON_NAMESPACE_END |
| 54 | |
| 55 | #endif // RAPIDJSON_POW10_ |
| 56 | |