MongoDB C++ Driver legacy-1.1.1
Loading...
Searching...
No Matches
compare_numbers.h
1/* Copyright 2015 MongoDB Inc.
2 *
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU Affero General Public License, version 3,
5 * as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU Affero General Public License for more details.
11 *
12 * You should have received a copy of the GNU Affero General Public License
13 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14 *
15 * As a special exception, the copyright holders give permission to link the
16 * code of portions of this program with the OpenSSL library under certain
17 * conditions as described in each individual source file and distribute
18 * linked combinations including the program with the OpenSSL library. You
19 * must comply with the GNU Affero General Public License in all respects
20 * for all of the code used other than as permitted herein. If you modify
21 * file(s) with this exception, you may extend this exception to your
22 * version of the file(s), but you are not obligated to do so. If you do not
23 * wish to do so, delete this exception statement from your version. If you
24 * delete this exception statement from all source files in the program,
25 * then also delete it in the license file.
26 */
27
28#pragma once
29
30#include "mongo/platform/float_utils.h"
31
32namespace mongo {
33
40inline int compareInts(int lhs, int rhs) {
41 return lhs == rhs ? 0 : lhs < rhs ? -1 : 1;
42}
43
44inline int compareLongs(long long lhs, long long rhs) {
45 return lhs == rhs ? 0 : lhs < rhs ? -1 : 1;
46}
47
48inline int compareDoubles(double lhs, double rhs) {
49 if (lhs == rhs)
50 return 0;
51 if (lhs < rhs)
52 return -1;
53 if (lhs > rhs)
54 return 1;
55
56 // If none of the above cases returned, lhs or rhs must be NaN.
57 if (isNaN(lhs))
58 return isNaN(rhs) ? 0 : -1;
59 return 1;
60}
61
62// This is the tricky one. Needs to support the following cases:
63// * Doubles with a fractional component.
64// * Longs that can't be precisely represented as a double.
65// * Doubles outside of the range of Longs (including +/- Inf).
66// * NaN (defined by us as less than all Longs)
67// * Return value is always -1, 0, or 1 to ensure it is safe to negate.
68inline int compareLongToDouble(long long lhs, double rhs) {
69 // All Longs are > NaN
70 if (isNaN(rhs))
71 return 1;
72
73 // Ints with magnitude <= 2**53 can be precisely represented as doubles.
74 // Additionally, doubles outside of this range can't have a fractional component.
75 static const long long kEndOfPreciseDoubles = 1ll << 53;
76 if (lhs <= kEndOfPreciseDoubles && lhs >= -kEndOfPreciseDoubles) {
77 return compareDoubles(lhs, rhs);
78 }
79
80 // Large magnitude doubles (including +/- Inf) are strictly > or < all Longs.
81 static const double kBoundOfLongRange = -static_cast<double>(LLONG_MIN); // positive 2**63
82 if (rhs >= kBoundOfLongRange)
83 return -1; // Can't be represented in a Long.
84 if (rhs < -kBoundOfLongRange)
85 return 1; // Can be represented in a Long.
86
87 // Remaining Doubles can have their integer component precisely represented as long longs.
88 // If they have a fractional component, they must be strictly > or < lhs even after
89 // truncation of the fractional component since low-magnitude lhs were handled above.
90 return compareLongs(lhs, rhs);
91}
92
93inline int compareDoubleToLong(double lhs, long long rhs) {
94 // Only implement the real logic once.
95 return -compareLongToDouble(rhs, lhs);
96}
97
98} // namespace mongo
Utility functions for parsing numbers from strings.
Definition compare_numbers.h:32
int compareInts(int lhs, int rhs)
These functions compare numbers using the same rules as BSON.
Definition compare_numbers.h:40