| 1 | #cxx . . $ `<file>/melder/melder_ftoi.h`/ifndef _melder_ftoi_h_ |
| 2 | #define _melder_ftoi_h_cxx . . $ `melder/melder_ftoi.h:2:9`! |
| 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_roundDowncxx . . $ Melder_roundDown(447c67c6676f75c3). (double x) { |
| 24 | return floorcxx . . $ floor(fee4f1d22bc8e617). (x); |
| 25 | } |
| 26 | |
| 27 | inline integercxx . . $ integer# Melder_iroundDowncxx . . $ Melder_iroundDown(a6e4a4c8b00c35a3). (double x) { |
| 28 | const double xround = Melder_roundDowncxx . . $ Melder_roundDown(447c67c6676f75c3). (x); |
| 29 | Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (xround >= (double) INTEGER_MINcxx . . $ `melder/melder_int.h:55:9`! && xround <= (double) INTEGER_MAXcxx . . $ `melder/melder_int.h:54:9`!, // 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 (integercxx . . $ integer#) xround; |
| 32 | } |
| 33 | #define Melder_ifloorcxx . . $ `melder/melder_ftoi.h:33:9`! Melder_iroundDown |
| 34 | |
| 35 | inline double Melder_roundUpcxx . . $ Melder_roundUp(447c67c6676f75c3). (double x) { |
| 36 | return ceilcxx . . $ ceil(fee4f1d22bc8e617). (x); |
| 37 | } |
| 38 | |
| 39 | inline integercxx . . $ integer# Melder_iroundUpcxx . . $ Melder_iroundUp(a6e4a4c8b00c35a3). (double x) { |
| 40 | const double xround = Melder_roundUpcxx . . $ Melder_roundUp(447c67c6676f75c3). (x); |
| 41 | Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (xround >= (double) INTEGER_MINcxx . . $ `melder/melder_int.h:55:9`! && xround <= (double) INTEGER_MAXcxx . . $ `melder/melder_int.h:54:9`!, |
| 42 | U"When rounding up the real value ", x, U", the result cannot be represented in an integer."); |
| 43 | return (integercxx . . $ integer#) xround; |
| 44 | } |
| 45 | #define Melder_iceilingcxx . . $ `melder/melder_ftoi.h:45:9`! Melder_iroundUp |
| 46 | |
| 47 | inline double Melder_roundTowardsZerocxx . . $ Melder_roundTowardsZero(447c67c6676f75c3). (double x) { |
| 48 | return x >= 0.0 ? Melder_roundDowncxx . . $ Melder_roundDown(447c67c6676f75c3). (x) : Melder_roundUpcxx . . $ Melder_roundUp(447c67c6676f75c3). (x); |
| 49 | } |
| 50 | |
| 51 | inline integercxx . . $ integer# Melder_iroundTowardsZerocxx . . $ Melder_iroundTowardsZero(a6e4a4c8b00c35a3). (double x) { |
| 52 | Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (x >= (double) INTEGER_MINcxx . . $ `melder/melder_int.h:55:9`! && x <= (double) INTEGER_MAXcxx . . $ `melder/melder_int.h:54:9`!, |
| 53 | U"When rounding the real value ", x, U" towards zero, the result cannot be represented in an integer."); |
| 54 | return (integercxx . . $ integer#) x; |
| 55 | } |
| 56 | |
| 57 | inline double Melder_roundAwayFromZerocxx . . $ Melder_roundAwayFromZero(447c67c6676f75c3). (double x) { |
| 58 | return x >= 0.0 ? Melder_roundUpcxx . . $ Melder_roundUp(447c67c6676f75c3). (x) : Melder_roundDowncxx . . $ Melder_roundDown(447c67c6676f75c3). (x); |
| 59 | } |
| 60 | |
| 61 | inline integercxx . . $ integer# Melder_iroundAwayFromZerocxx . . $ Melder_iroundAwayFromZero(a6e4a4c8b00c35a3). (double x) { |
| 62 | const double xround = Melder_roundAwayFromZerocxx . . $ Melder_roundAwayFromZero(447c67c6676f75c3). (x); |
| 63 | Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (xround >= (double) INTEGER_MINcxx . . $ `melder/melder_int.h:55:9`! && xround <= (double) INTEGER_MAXcxx . . $ `melder/melder_int.h:54:9`!, |
| 64 | U"When rounding the real value ", x, U" away from zero, the result cannot be represented in an integer."); |
| 65 | return (integercxx . . $ integer#) xround; |
| 66 | } |
| 67 | |
| 68 | inline double Melder_round_tieUpcxx . . $ Melder_round_tieUp(447c67c6676f75c3). (double x) { |
| 69 | return Melder_roundDowncxx . . $ Melder_roundDown(447c67c6676f75c3). (x + 0.5); |
| 70 | } |
| 71 | |
| 72 | inline integercxx . . $ integer# Melder_iround_tieUpcxx . . $ Melder_iround_tieUp(a6e4a4c8b00c35a3). (double x) { |
| 73 | const double xround = Melder_round_tieUpcxx . . $ Melder_round_tieUp(447c67c6676f75c3). (x); |
| 74 | Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (xround >= (double) INTEGER_MINcxx . . $ `melder/melder_int.h:55:9`! && xround <= (double) INTEGER_MAXcxx . . $ `melder/melder_int.h:54:9`!, |
| 75 | U"When rounding the real value ", x, U", the result cannot be represented in an integer."); |
| 76 | return (integercxx . . $ integer#) xround; |
| 77 | } |
| 78 | #define Melder_iroundcxx . . $ `melder/melder_ftoi.h:78:9`! Melder_iround_tieUp |
| 79 | |
| 80 | inline double Melder_round_tieDowncxx . . $ Melder_round_tieDown(447c67c6676f75c3). (double x) { |
| 81 | return Melder_roundUpcxx . . $ Melder_roundUp(447c67c6676f75c3). (x - 0.5); |
| 82 | } |
| 83 | |
| 84 | inline integercxx . . $ integer# Melder_iround_tieDowncxx . . $ Melder_iround_tieDown(a6e4a4c8b00c35a3). (double x) { |
| 85 | const double xround = Melder_round_tieDowncxx . . $ Melder_round_tieDown(447c67c6676f75c3). (x); |
| 86 | Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (xround >= (double) INTEGER_MINcxx . . $ `melder/melder_int.h:55:9`! && xround <= (double) INTEGER_MAXcxx . . $ `melder/melder_int.h:54:9`!, |
| 87 | U"When rounding the real value ", x, U", the result cannot be represented in an integer."); |
| 88 | return (integercxx . . $ integer#) xround; |
| 89 | } |
| 90 | |
| 91 | inline double Melder_round_tieTowardsZerocxx . . $ Melder_round_tieTowardsZero(447c67c6676f75c3). (double x) { |
| 92 | return x >= 0.0 ? Melder_round_tieDowncxx . . $ Melder_round_tieDown(447c67c6676f75c3). (x) : Melder_round_tieUpcxx . . $ Melder_round_tieUp(447c67c6676f75c3). (x); |
| 93 | } |
| 94 | |
| 95 | inline integercxx . . $ integer# Melder_iround_tieTowardsZerocxx . . $ Melder_iround_tieTowardsZero(a6e4a4c8b00c35a3). (double x) { |
| 96 | const double xround = Melder_round_tieTowardsZerocxx . . $ Melder_round_tieTowardsZero(447c67c6676f75c3). (x); |
| 97 | Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (xround >= (double) INTEGER_MINcxx . . $ `melder/melder_int.h:55:9`! && xround <= (double) INTEGER_MAXcxx . . $ `melder/melder_int.h:54:9`!, |
| 98 | U"When rounding the real value ", x, U", the result cannot be represented in an integer."); |
| 99 | return (integercxx . . $ integer#) xround; |
| 100 | } |
| 101 | |
| 102 | inline double Melder_round_tieAwayFromZerocxx . . $ Melder_round_tieAwayFromZero(447c67c6676f75c3). (double x) { |
| 103 | return x >= 0.0 ? Melder_round_tieUpcxx . . $ Melder_round_tieUp(447c67c6676f75c3). (x) : Melder_round_tieDowncxx . . $ Melder_round_tieDown(447c67c6676f75c3). (x); |
| 104 | } |
| 105 | |
| 106 | inline integercxx . . $ integer# Melder_iround_tieAwayFromZerocxx . . $ Melder_iround_tieAwayFromZero(a6e4a4c8b00c35a3). (double x) { |
| 107 | const double xround = Melder_round_tieAwayFromZerocxx . . $ Melder_round_tieAwayFromZero(447c67c6676f75c3). (x); |
| 108 | Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (xround >= (double) INTEGER_MINcxx . . $ `melder/melder_int.h:55:9`! && xround <= (double) INTEGER_MAXcxx . . $ `melder/melder_int.h:54:9`!, |
| 109 | U"When rounding the real value ", x, U", the result cannot be represented in an integer."); |
| 110 | return (integercxx . . $ integer#) xround; |
| 111 | } |
| 112 | |
| 113 | /* End of file melder_ftoi.h */ |
| 114 | #endif |