| 1 |
#ifndef _melder_ftoi_h_ |
| 2 |
#define _melder_ftoi_h_ |
| 3 |
/* melder_ftoi.h |
| 4 |
* |
| 5 |
* Copyright (C) 1992-2018,2020,2021 Paul Boersma |
| 6 |
* |
| 7 |
* This code is free software; you can redistribute it and/or modify |
| 8 |
* it under the terms of the GNU General Public License as published by |
| 9 |
* the Free Software Foundation; either version 2 of the License, or (at |
| 10 |
* your option) any later version. |
| 11 |
* |
| 12 |
* This code is distributed in the hope that it will be useful, but |
| 13 |
* WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 15 |
* See the GNU General Public License for more details. |
| 16 |
* |
| 17 |
* You should have received a copy of the GNU General Public License |
| 18 |
* along with this work. If not, see <http://www.gnu.org/licenses/>. |
| 19 |
*/ |
| 20 |
|
| 21 |
#pragma mark - REAL TO INTEGER CONVERSION |
| 22 |
|
| 23 |
inline double Melder_roundDown (double x) { |
| 24 |
return floor (x); |
| 25 |
} |
| 26 |
|
| 27 |
inline integer Melder_iroundDown (double x) { |
| 28 |
const double xround = Melder_roundDown (x); |
| 29 |
Melder_require (xround >= (double) INTEGER_MIN && xround <= (double) INTEGER_MAX, // this formulation handles NaN correctly |
| 30 |
U"When rounding down the real value ", x, U", the result cannot be represented in an integer."); |
| 31 |
return (integer) xround; |
| 32 |
} |
| 33 |
#define Melder_ifloor Melder_iroundDown |
| 34 |
|
| 35 |
inline double Melder_roundUp (double x) { |
| 36 |
return ceil (x); |
| 37 |
} |
| 38 |
|
| 39 |
inline integer Melder_iroundUp (double x) { |
| 40 |
const double xround = Melder_roundUp (x); |
| 41 |
Melder_require (xround >= (double) INTEGER_MIN && xround <= (double) INTEGER_MAX, |
| 42 |
U"When rounding up the real value ", x, U", the result cannot be represented in an integer."); |
| 43 |
return (integer) xround; |
| 44 |
} |
| 45 |
#define Melder_iceiling Melder_iroundUp |
| 46 |
|
| 47 |
inline double Melder_roundTowardsZero (double x) { |
| 48 |
return x >= 0.0 ? Melder_roundDown (x) : Melder_roundUp (x); |
| 49 |
} |
| 50 |
|
| 51 |
inline integer Melder_iroundTowardsZero (double x) { |
| 52 |
Melder_require (x >= (double) INTEGER_MIN && x <= (double) INTEGER_MAX, |
| 53 |
U"When rounding the real value ", x, U" towards zero, the result cannot be represented in an integer."); |
| 54 |
return (integer) x; |
| 55 |
} |
| 56 |
|
| 57 |
inline double Melder_roundAwayFromZero (double x) { |
| 58 |
return x >= 0.0 ? Melder_roundUp (x) : Melder_roundDown (x); |
| 59 |
} |
| 60 |
|
| 61 |
inline integer Melder_iroundAwayFromZero (double x) { |
| 62 |
const double xround = Melder_roundAwayFromZero (x); |
| 63 |
Melder_require (xround >= (double) INTEGER_MIN && xround <= (double) INTEGER_MAX, |
| 64 |
U"When rounding the real value ", x, U" away from zero, the result cannot be represented in an integer."); |
| 65 |
return (integer) xround; |
| 66 |
} |
| 67 |
|
| 68 |
inline double Melder_round_tieUp (double x) { |
| 69 |
return Melder_roundDown (x + 0.5); |
| 70 |
} |
| 71 |
|
| 72 |
inline integer Melder_iround_tieUp (double x) { |
| 73 |
const double xround = Melder_round_tieUp (x); |
| 74 |
Melder_require (xround >= (double) INTEGER_MIN && xround <= (double) INTEGER_MAX, |
| 75 |
U"When rounding the real value ", x, U", the result cannot be represented in an integer."); |
| 76 |
return (integer) xround; |
| 77 |
} |
| 78 |
#define Melder_iround Melder_iround_tieUp |
| 79 |
|
| 80 |
inline double Melder_round_tieDown (double x) { |
| 81 |
return Melder_roundUp (x - 0.5); |
| 82 |
} |
| 83 |
|
| 84 |
inline integer Melder_iround_tieDown (double x) { |
| 85 |
const double xround = Melder_round_tieDown (x); |
| 86 |
Melder_require (xround >= (double) INTEGER_MIN && xround <= (double) INTEGER_MAX, |
| 87 |
U"When rounding the real value ", x, U", the result cannot be represented in an integer."); |
| 88 |
return (integer) xround; |
| 89 |
} |
| 90 |
|
| 91 |
inline double Melder_round_tieTowardsZero (double x) { |
| 92 |
return x >= 0.0 ? Melder_round_tieDown (x) : Melder_round_tieUp (x); |
| 93 |
} |
| 94 |
|
| 95 |
inline integer Melder_iround_tieTowardsZero (double x) { |
| 96 |
const double xround = Melder_round_tieTowardsZero (x); |
| 97 |
Melder_require (xround >= (double) INTEGER_MIN && xround <= (double) INTEGER_MAX, |
| 98 |
U"When rounding the real value ", x, U", the result cannot be represented in an integer."); |
| 99 |
return (integer) xround; |
| 100 |
} |
| 101 |
|
| 102 |
inline double Melder_round_tieAwayFromZero (double x) { |
| 103 |
return x >= 0.0 ? Melder_round_tieUp (x) : Melder_round_tieDown (x); |
| 104 |
} |
| 105 |
|
| 106 |
inline integer Melder_iround_tieAwayFromZero (double x) { |
| 107 |
const double xround = Melder_round_tieAwayFromZero (x); |
| 108 |
Melder_require (xround >= (double) INTEGER_MIN && xround <= (double) INTEGER_MAX, |
| 109 |
U"When rounding the real value ", x, U", the result cannot be represented in an integer."); |
| 110 |
return (integer) xround; |
| 111 |
} |
| 112 |
|
| 113 |
/* End of file melder_ftoi.h */ |
| 114 |
#endif |