| 137 | */ |
| 138 | template<class ValueType> |
| 139 | ValueType Ceil(const ValueType & x, ErrorCode * err = 0) |
| 140 | { |
| 141 | if( x.IsNan() ) |
| 142 | { |
| 143 | if( err ) |
| 144 | *err = err_improper_argument; |
| 145 | |
| 146 | return x; // NaN |
| 147 | } |
| 148 | |
| 149 | ValueType result(x); |
| 150 | uint c = 0; |
| 151 | |
| 152 | result.SkipFraction(); |
| 153 | |
| 154 | if( result != x ) |
| 155 | { |
| 156 | // x is with fraction |
| 157 | // if x is negative we don't have to do anything |
| 158 | if( !x.IsSign() ) |
| 159 | { |
| 160 | ValueType one; |
| 161 | one.SetOne(); |
| 162 | |
| 163 | c += result.Add(one); |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | if( err ) |
| 168 | *err = c ? err_overflow : err_ok; |
| 169 | |
| 170 | return result; |
| 171 | } |
| 172 | |
| 173 | |
| 174 | /*! |