Adobe Source Libraries 1.49.0
A collection of C++ libraries.
Loading...
Searching...
No Matches
cmath.hpp
Go to the documentation of this file.
1/*
2 Copyright 2013 Adobe
3 Distributed under the Boost Software License, Version 1.0.
4 (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
5*/
6/**************************************************************************************************/
7
8#ifndef ADOBE_CMATH_HPP
9#define ADOBE_CMATH_HPP
10
11#include <adobe/config.hpp>
12
13#include <cmath>
14#include <functional>
15
16/**************************************************************************************************/
17
18#define ADOBE_HAS_C99_STD_MATH_H
19
20namespace adobe {
21
22/**************************************************************************************************/
23
24using std::double_t;
25using std::float_t;
26
27using std::lround;
28using std::round;
29using std::trunc;
30
31/**************************************************************************************************/
32
33} // namespace adobe
34
35/**************************************************************************************************/
36
37namespace adobe {
38
39/**************************************************************************************************/
40
41template <typename A, typename R>
42struct nearest_cast_fn;
43
44/**************************************************************************************************/
45
46inline double round_half_up(double x) { return std::floor(x + 0.5); }
47
48inline float round_half_up(float x) { return std::floor(x + 0.5f); }
49
50inline long lround_half_up(double x) { return static_cast<long>(std::floor(x + 0.5)); }
51
52inline long lround_half_up(float x) { return static_cast<long>(std::floor(x + 0.5f)); }
53
54/*
55 REVISIT (sparent) : Should complete the rounding modes by providing a round_half_even()
56 function.
57
58 Names are borrowed from the EDA rounding modes:
59
60 <http://www.gobosoft.com/eiffel/gobo/math/decimal/>
61*/
62
63/**************************************************************************************************/
64
65template <typename R, typename A>
66inline R nearest_cast(const A& x) {
67 return nearest_cast_fn<A, R>()(x);
68}
69
70/**************************************************************************************************/
71
72template <typename A, typename R>
74 R operator()(const A& x) const { return static_cast<R>(round_half_up(x)); }
75};
76
77template <typename A>
78struct nearest_cast_fn<A, float> {
79 float operator()(const A& x) const { return static_cast<float>(x); }
80};
81
82template <typename A>
83struct nearest_cast_fn<A, double> {
84 double operator()(const A& x) const { return static_cast<double>(x); }
85};
86
87/**************************************************************************************************/
88
89} // namespace adobe
90
91/**************************************************************************************************/
92
93#endif
94
95/**************************************************************************************************/
double round_half_up(double x)
Definition cmath.hpp:46
long lround_half_up(double x)
Definition cmath.hpp:50
R nearest_cast(const A &x)
Definition cmath.hpp:66
double operator()(const A &x) const
Definition cmath.hpp:84
float operator()(const A &x) const
Definition cmath.hpp:79
R operator()(const A &x) const
Definition cmath.hpp:74