Generated on for Gecode by doxygen 1.17.0
rounding.hpp
Go to the documentation of this file.
1/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2/*
3 * Main authors:
4 * Christian Schulte <schulte@gecode.dev>
5 *
6 * Copyright:
7 * Christian Schulte, 2012
8 *
9 * This file is part of Gecode, the generic constraint
10 * development environment:
11 * http://www.gecode.dev
12 *
13 * Permission is hereby granted, free of charge, to any person obtaining
14 * a copy of this software and associated documentation files (the
15 * "Software"), to deal in the Software without restriction, including
16 * without limitation the rights to use, copy, modify, merge, publish,
17 * distribute, sublicense, and/or sell copies of the Software, and to
18 * permit persons to whom the Software is furnished to do so, subject to
19 * the following conditions:
20 *
21 * The above copyright notice and this permission notice shall be
22 * included in all copies or substantial portions of the Software.
23 *
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 *
32 */
33
34namespace Gecode { namespace Float {
35
38 const FloatNum inf = std::numeric_limits<FloatNum>::infinity();
39 const FloatNum max = std::numeric_limits<FloatNum>::max();
40 if (l == -inf) {
41 if (u == -inf)
42 return l;
43 // Preserve the extended endpoint for semi-infinite intervals.
44 return (u == inf) ? 0.0 : l;
45 }
46 if (u == inf)
47 return u;
48
49 const FloatNum h = max / 2.0;
50 if ((u > h) || (l < -h))
51 return std::ldexp(RoundingControl::median(l / 2.0,u / 2.0),1);
52 return RoundingControl::median(l,u);
53 }
54
55#define GECODE_ROUND_OP(name) \
56 forceinline FloatNum \
57 RoundingBase::name##_down(FloatNum x, FloatNum y) { \
58 return RoundingControl::name##_down(x,y); \
59 } \
60 forceinline FloatNum \
61 RoundingBase::name##_up(FloatNum x, FloatNum y) { \
62 return RoundingControl::name##_up(x,y); \
63 }
64
69
70#undef GECODE_ROUND_OP
71
72#define GECODE_ROUND_FUN(name) \
73 forceinline FloatNum \
74 RoundingBase::name##_down(FloatNum x) { \
75 return RoundingControl::name##_down(x); \
76 } \
77 forceinline FloatNum \
78 RoundingBase::name##_up(FloatNum x) { \
79 return RoundingControl::name##_up(x); \
80 }
81
83
84#if defined(_M_X64) || defined(_M_IA64)
85
86 // Workaround as MSVC on x64 does not have a rint function
89 return floor(x);
90 }
93 return ceil(x);
94 }
95
96#else
97
99
100#endif
101
102#undef GECODE_ROUND_FUN
103
104}}
105
106// STATISTICS: float-var
107
FloatNum int_up(FloatNum x)
Return next upward-rounded integer of x (domain: ).
FloatNum int_down(FloatNum x)
Return next downward-rounded integer of x (domain: ).
FloatNum median(FloatNum x, FloatNum y)
Return an overflow-safe median of x and y, where x <= y.
Definition rounding.hpp:37
double FloatNum
Floating point number base type.
Definition float.hh:106
Floating point numbers.
Gecode toplevel namespace
void div(Home home, FloatVar x0, FloatVar x1, FloatVar x2)
Post propagator for .
void sqrt(Home home, FloatVar x0, FloatVar x1)
Post propagator for .
void max(Home home, FloatVar x0, FloatVar x1, FloatVar x2)
Post propagator for .
#define GECODE_ROUND_FUN(name)
Definition rounding.hpp:72
#define GECODE_ROUND_OP(name)
Definition rounding.hpp:55
#define forceinline
Definition config.hpp:144