Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
scalar_validator.cc
Go to the documentation of this file.
1// Copyright 2010-2024 Google LLC
2// Licensed under the Apache License, Version 2.0 (the "License");
3// you may not use this file except in compliance with the License.
4// You may obtain a copy of the License at
5//
6// http://www.apache.org/licenses/LICENSE-2.0
7//
8// Unless required by applicable law or agreed to in writing, software
9// distributed under the License is distributed on an "AS IS" BASIS,
10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11// See the License for the specific language governing permissions and
12// limitations under the License.
13
15
16#include <cmath>
17#include <limits>
18
19#include "absl/status/status.h"
20#include "absl/strings/str_cat.h"
21
22namespace operations_research {
23namespace math_opt {
24namespace {
25constexpr double kInf = std::numeric_limits<double>::infinity();
26} // namespace
27
28absl::Status CheckScalarNoNanNoInf(const double d) {
29 if (!std::isfinite(d)) {
30 return absl::InvalidArgumentError(
31 absl::StrCat("Expected no NaN or inf but found value: ", d));
32 }
33 return absl::OkStatus();
34}
35
36absl::Status CheckScalar(const double value, const DoubleOptions& options) {
37 if (std::isnan(value)) {
38 return absl::InvalidArgumentError("Invalid NaN value");
39 }
40 if (!options.allow_positive_infinity && value == kInf) {
41 return absl::InvalidArgumentError("Invalid positive infinite value");
42 }
43 if (!options.allow_negative_infinity && value == -kInf) {
44 return absl::InvalidArgumentError("Invalid negative infinite value");
45 }
46 if (!options.allow_positive && value > 0) {
47 return absl::InvalidArgumentError(
48 absl::StrCat("Invalid positive value = ", value));
49 }
50 if (!options.allow_negative && value < 0) {
51 return absl::InvalidArgumentError(
52 absl::StrCat("Invalid negative value = ", value));
53 }
54 return absl::OkStatus();
55}
56
57} // namespace math_opt
58} // namespace operations_research
int64_t value
absl::Status CheckScalar(const double value, const DoubleOptions &options)
Checks value is not NaN and satisfies the additional conditions in options.
absl::Status CheckScalarNoNanNoInf(const double d)
In SWIG mode, we don't want anything besides these top-level includes.