dir -> true = round up, false = round down
| 185 | |
| 186 | // dir -> true = round up, false = round down |
| 187 | double step_round(const double in, const bool dir) { |
| 188 | if (in == 0) { return 0; } |
| 189 | |
| 190 | static const double LOG2 = log10(2); |
| 191 | static const double LOG4 = log10(4); |
| 192 | static const double LOG6 = log10(6); |
| 193 | static const double LOG8 = log10(8); |
| 194 | |
| 195 | // log_in is of the form "s abc.xyz", where |
| 196 | // s is either + or -; + indicates abs(in) >= 1 and - indicates 0 < abs(in) |
| 197 | // < 1 (log10(1) is +0) |
| 198 | const double sign = in < 0 ? -1 : 1; |
| 199 | const double log_in = std::log10(std::fabs(in)); |
| 200 | const double mag = std::pow(10, std::floor(log_in)) * |
| 201 | sign; // Number of digits either left or right of 0 |
| 202 | const double dec = std::log10(in / mag); // log of the fraction |
| 203 | |
| 204 | // This means in is of the for 10^n |
| 205 | if (dec == 0) { return in; } |
| 206 | |
| 207 | // For negative numbers, -ve round down = +ve round up and vice versa |
| 208 | bool op_dir = in > 0 ? dir : !dir; |
| 209 | |
| 210 | double mult = 1; |
| 211 | |
| 212 | // Round up |
| 213 | if (op_dir) { |
| 214 | if (dec <= LOG2) { |
| 215 | mult = 2; |
| 216 | } else if (dec <= LOG4) { |
| 217 | mult = 4; |
| 218 | } else if (dec <= LOG6) { |
| 219 | mult = 6; |
| 220 | } else if (dec <= LOG8) { |
| 221 | mult = 8; |
| 222 | } else { |
| 223 | mult = 10; |
| 224 | } |
| 225 | } else { // Round down |
| 226 | if (dec < LOG2) { |
| 227 | mult = 1; |
| 228 | } else if (dec < LOG4) { |
| 229 | mult = 2; |
| 230 | } else if (dec < LOG6) { |
| 231 | mult = 4; |
| 232 | } else if (dec < LOG8) { |
| 233 | mult = 6; |
| 234 | } else { |
| 235 | mult = 8; |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | return mag * mult; |
| 240 | } |
| 241 | |
| 242 | ForgeModule& forgePlugin() { return detail::forgeManager().plugin(); } |
| 243 |
no test coverage detected