OR-Tools  9.3
scalar_validator.cc
Go to the documentation of this file.
1// Copyright 2010-2021 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#include <string>
19
20#include "absl/status/status.h"
21#include "absl/strings/str_cat.h"
22
23namespace operations_research {
24namespace math_opt {
25namespace {
26constexpr double kInf = std::numeric_limits<double>::infinity();
27} // namespace
28
29absl::Status CheckScalarNoNanNoInf(const double d) {
30 if (!std::isfinite(d)) {
31 return absl::InvalidArgumentError(
32 absl::StrCat("Expected no NaN or inf but found value: ", d));
33 }
34 return absl::OkStatus();
35}
36
37absl::Status CheckScalar(const double value, const DoubleOptions& options) {
38 if (std::isnan(value)) {
39 return absl::InvalidArgumentError("Invalid NaN value");
40 }
41 if (!options.allow_positive_infinity && value == kInf) {
42 return absl::InvalidArgumentError("Invalid positive infinite value");
43 }
44 if (!options.allow_negative_infinity && value == -kInf) {
45 return absl::InvalidArgumentError("Invalid negative infinite value");
46 }
47 if (!options.allow_positive && value > 0) {
48 return absl::InvalidArgumentError(
49 absl::StrCat("Invalid positive value = ", value));
50 }
51 if (!options.allow_negative && value < 0) {
52 return absl::InvalidArgumentError(
53 absl::StrCat("Invalid negative value = ", value));
54 }
55 return absl::OkStatus();
56}
57
58} // namespace math_opt
59} // namespace operations_research
int64_t value
absl::Status CheckScalar(const double value, const DoubleOptions &options)
absl::Status CheckScalarNoNanNoInf(const double d)
Collection of objects used to extend the Constraint Solver library.