[multiprecision] how to catch both underflow and overflow
Hello, when using boost multiprecision [1] to execute even simple math operations like addition x = x+1 or x-1 it seems on mathematical error it might throw either std::range_error or std::overflow_error or std::underflow_error . Sadly it seems that it's not the case that overflow and underflow are child classes of range_error. So, in more complex math expression to detect any math error you must write catch(....) for all 3 of them (because just catching common std::runtime_error would be too broad and might hide some unrelated error). Is there some better way for this? Perhaps boost should introduce a common type for math error and throw own under/over/range errors that inherit from std's one (for compatibility) and also inherit from boost::math_range_error to allow catch (boost::math_range_error) // either over/under flow or other math [1] for example using type like: boost::multiprecision::number< boost::multiprecision::cpp_int_backend< 64-1, 64-1, boost::multiprecision::signed_magnitude, boost::multiprecision::checked, void> > -- GPG: 31A8 55AA 07BE 5145 65EB 2292 65A6 1F7A 9781 DF89
On 9/19/19 3:49 AM, blacksmith--- via Boost wrote:
Hello, when using boost multiprecision [1] to execute even simple math operations like addition x = x+1 or x-1 it seems on mathematical error it might throw either std::range_error or std::overflow_error or std::underflow_error .
Sadly it seems that it's not the case that overflow and underflow are child classes of range_error.
So, in more complex math expression to detect any math error you must write catch(....) for all 3 of them (because just catching common std::runtime_error would be too broad and might hide some unrelated error).
Is there some better way for this?
Perhaps boost should introduce a common type for math error and throw own under/over/range errors that inherit from std's one (for compatibility) and also inherit from boost::math_range_error to allow catch (boost::math_range_error) // either over/under flow or other math
[1] for example using type like:
boost::multiprecision::number< boost::multiprecision::cpp_int_backend< 64-1, 64-1, boost::multiprecision::signed_magnitude, boost::multiprecision::checked, void> >
take a look at safe numerics. Robert Ramey
On 19/09/2019 11:49, blacksmith--- via Boost wrote:
Hello, when using boost multiprecision [1] to execute even simple math operations like addition x = x+1 or x-1 it seems on mathematical error it might throw either std::range_error or std::overflow_error or std::underflow_error . Only range_error or overflow_error, there's no underflow_error for checked integers - it doesn't really make sense.
Sadly it seems that it's not the case that overflow and underflow are child classes of range_error. They both inherit from runtime_error.
So, in more complex math expression to detect any math error you must write catch(....) for all 3 of them (because just catching common std::runtime_error would be too broad and might hide some unrelated error).
What are you planning to do with the exceptions? Ignore them? If so you would be better with unchecked integers? In any case any runtime_error surely indicates something that must be "handled"?
Is there some better way for this?
Perhaps boost should introduce a common type for math error and throw own under/over/range errors that inherit from std's one (for compatibility) and also inherit from boost::math_range_error to allow catch (boost::math_range_error) // either over/under flow or other math
We could I guess, it seems over-complex given that the std defines these exception types for us. HTH, John.
[1] for example using type like:
boost::multiprecision::number< boost::multiprecision::cpp_int_backend< 64-1, 64-1, boost::multiprecision::signed_magnitude, boost::multiprecision::checked, void> >
--- This email has been checked for viruses by Avast antivirus software. https://www.avast.com/antivirus
participants (3)
-
blacksmith@peerfreedom.org
-
John Maddock
-
Robert Ramey