From 21e9e0d39724629471e4626fd027860f592d0e75 Mon Sep 17 00:00:00 2001 From: Markus Gans Date: Tue, 30 Jun 2015 00:21:50 +0200 Subject: [PATCH] Class FString: improve floating point exception handling --- src/fstring.cpp | 20 ++++++++++++++++---- src/fstring.h | 5 +---- test/fstring.cpp | 9 +++++---- 3 files changed, 22 insertions(+), 12 deletions(-) diff --git a/src/fstring.cpp b/src/fstring.cpp index f9beedcf..b85d10ff 100644 --- a/src/fstring.cpp +++ b/src/fstring.cpp @@ -965,7 +965,7 @@ long FString::toLong() const if ( num > tenth_limit || (num == tenth_limit && d > tenth_limit_digit) ) { - throw std::out_of_range ("overflow"); + throw std::overflow_error ("overflow"); } num = (num<<3)+(num<<1) + d; // (10 * num) + d p++; @@ -1009,7 +1009,7 @@ uLong FString::toULong() const if ( num > tenth_limit || (num == tenth_limit && d > tenth_limit_digit) ) { - throw std::out_of_range ("overflow"); + throw std::overflow_error ("overflow"); } num = (num<<3)+(num<<1) + d; // (10 * num) + d p++; @@ -1021,6 +1021,18 @@ uLong FString::toULong() const return num; } +//---------------------------------------------------------------------- +float FString::toFloat() const +{ + double d; + d = this->toDouble(); + + if ( d > FLT_MAX || d < FLT_MIN ) + throw std::overflow_error ("overflow"); + + return float(d); +} + //---------------------------------------------------------------------- double FString::toDouble() const { @@ -1041,9 +1053,9 @@ double FString::toDouble() const if ( errno == ERANGE ) { if ( ret >= HUGE_VAL || ret <= -HUGE_VAL ) - throw std::out_of_range ("overflow"); + throw std::overflow_error ("overflow"); if ( ret == 0.0l ) - throw std::out_of_range ("underflow"); + throw std::underflow_error ("underflow"); } return ret; } diff --git a/src/fstring.h b/src/fstring.h index 3a54ce88..0f53591e 100644 --- a/src/fstring.h +++ b/src/fstring.h @@ -10,6 +10,7 @@ #include #include // for read errno +#include #include #include #include // need for va_list, va_start and va_end @@ -335,10 +336,6 @@ inline int FString::toInt() const inline uInt FString::toUInt() const { return uInt( toULong() ); } -//---------------------------------------------------------------------- -inline float FString::toFloat() const -{ return float( toDouble() ); } - //---------------------------------------------------------------------- inline std::vector FString::split (std::wstring& s) { return split(FString(s)); } diff --git a/test/fstring.cpp b/test/fstring.cpp index 9617e3a4..054bd79a 100644 --- a/test/fstring.cpp +++ b/test/fstring.cpp @@ -140,9 +140,9 @@ int main (int, char**) { std::cerr << "Invalid argument: " << ex.what() << std::endl; } - catch (const std::out_of_range& ex) + catch (const std::exception& ex) { - std::cerr << "Out of range: " << ex.what() << std::endl; + std::cerr << "Arithmetic error: " << ex.what() << std::endl; } setlocale(LC_NUMERIC, "C"); @@ -156,10 +156,11 @@ int main (int, char**) { std::cerr << "Invalid argument: " << ex.what() << std::endl; } - catch (const std::out_of_range& ex) + catch (const std::exception& ex) { - std::cerr << "Out of range: " << ex.what() << std::endl; + std::cerr << "Arithmetic error: " << ex.what() << std::endl; } + FString num1, num2, num3; num1.setNumber(137); num2.setNumber(-512);