STIR 6.4.0
round.inl
Go to the documentation of this file.
1//
2//
13/*
14 Copyright (C) 2000- 2010, Hammersmith Imanet Ltd
15 This file is part of STIR.
16
17 SPDX-License-Identifier: Apache-2.0
18
19 See STIR/LICENSE.txt for details
20*/
21
22START_NAMESPACE_STIR
23
24template <typename integerT>
25inline void
26round_to(integerT& result, const float x)
27{
28 if (x >= 0)
29 result = static_cast<integerT>(x + 0.5F);
30 else
31 result = -static_cast<integerT>(-x + 0.5F);
32}
33
34template <typename integerT>
35inline void
36round_to(integerT& result, const double x)
37{
38 if (x >= 0)
39 result = static_cast<integerT>(x + 0.5);
40 else
41 result = -static_cast<integerT>(-x + 0.5);
42}
43
44/* next 2 are just to avoid compiler warnings about using - on an unsigned type */
45inline void
46round_to(unsigned& result, const double x)
47{
48 result = static_cast<unsigned>(x + 0.5);
49}
50
51inline void
52round_to(unsigned long& result, const double x)
53{
54 result = static_cast<unsigned long>(x + 0.5);
55}
56
57/* could be implemented in terms of the above */
58int
59round(const float x)
60{
61 if (x >= 0)
62 return static_cast<int>(x + 0.5F);
63 else
64 return -static_cast<int>(-x + 0.5F);
65}
66
67int
68round(const double x)
69{
70 if (x >= 0)
71 return static_cast<int>(x + 0.5);
72 else
73 return -static_cast<int>(-x + 0.5);
74}
75
76template <int num_dimensions, class elemT>
77BasicCoordinate<num_dimensions, int>
79{
81 for (int i = 1; i <= num_dimensions; ++i)
82 rnd_x[i] = round(x[i]);
83 return rnd_x;
84}
85
86template <int num_dimensions, class integerT, class elemT>
87inline void
89{
90 for (int i = 1; i <= num_dimensions; ++i)
91 round_to(result[i], x[i]);
92}
93
94END_NAMESPACE_STIR
class BasicCoordinate<int num_dimensions, typename coordT> defines num_dimensions -dimensional coordi...
Definition BasicCoordinate.h:57
int round(const float x)
Implements rounding of floating point numbers.
Definition round.inl:59
void round_to(integerT &result, const float x)
Implements rounding of floating point numbers to other integer types.
Definition round.inl:26