| 43 | */ |
| 44 | template <class A> |
| 45 | A SafeDivision(A a, A b) |
| 46 | { |
| 47 | // Avoid overflow |
| 48 | if ((b < static_cast<A>(1)) && (a > b * std::numeric_limits<A>::max())) |
| 49 | { |
| 50 | return std::numeric_limits<A>::max(); |
| 51 | } |
| 52 | |
| 53 | // Avoid underflow |
| 54 | if ((a == static_cast<A>(0)) || |
| 55 | ((b > static_cast<A>(1)) && (a < b * std::numeric_limits<A>::min()))) |
| 56 | { |
| 57 | return static_cast<A>(0); |
| 58 | } |
| 59 | |
| 60 | // safe to do the division |
| 61 | return (a / b); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * A slightly different fuzzy comparator that checks if two values are |
no test coverage detected