1// Protocol Buffers - Google's data interchange format
2// Copyright 2008 Google Inc. All rights reserved.
3// https://developers.google.com/protocol-buffers/
4//
5// Redistribution and use in source and binary forms, with or without
6// modification, are permitted provided that the following conditions are
7// met:
8//
9// * Redistributions of source code must retain the above copyright
10// notice, this list of conditions and the following disclaimer.
11// * Redistributions in binary form must reproduce the above
12// copyright notice, this list of conditions and the following disclaimer
13// in the documentation and/or other materials provided with the
14// distribution.
15// * Neither the name of Google Inc. nor the names of its
16// contributors may be used to endorse or promote products derived from
17// this software without specific prior written permission.
18//
19// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
31#include <google/protobuf/io/strtod.h>
32
33#include <cstdio>
34#include <cstring>
35#include <limits>
36#include <string>
37
38#include <google/protobuf/stubs/logging.h>
39#include <google/protobuf/stubs/common.h>
40
41#include <google/protobuf/stubs/strutil.h>
42
43namespace google {
44namespace protobuf {
45namespace io {
46
47// This approximately 0x1.ffffffp127, but we don't use 0x1.ffffffp127 because
48// it won't compile in MSVC.
49const double MAX_FLOAT_AS_DOUBLE_ROUNDED = 3.4028235677973366e+38;
50
51float SafeDoubleToFloat(double value) {
52 // static_cast<float> on a number larger than float can result in illegal
53 // instruction error, so we need to manually convert it to infinity or max.
54 if (value > std::numeric_limits<float>::max()) {
55 // Max float value is about 3.4028234664E38 when represented as a double.
56 // However, when printing float as text, it will be rounded as
57 // 3.4028235e+38. If we parse the value of 3.4028235e+38 from text and
58 // compare it to 3.4028234664E38, we may think that it is larger, but
59 // actually, any number between these two numbers could only be represented
60 // as the same max float number in float, so we should treat them the same
61 // as max float.
62 if (value <= MAX_FLOAT_AS_DOUBLE_ROUNDED) {
63 return std::numeric_limits<float>::max();
64 }
65 return std::numeric_limits<float>::infinity();
66 } else if (value < -std::numeric_limits<float>::max()) {
67 if (value >= -MAX_FLOAT_AS_DOUBLE_ROUNDED) {
68 return -std::numeric_limits<float>::max();
69 }
70 return -std::numeric_limits<float>::infinity();
71 } else {
72 return static_cast<float>(value);
73 }
74}
75
76double NoLocaleStrtod(const char* str, char** endptr) {
77 return google::protobuf::internal::NoLocaleStrtod(str, endptr);
78}
79
80} // namespace io
81} // namespace protobuf
82} // namespace google
83