Overview
Features
Download
Documentation
Community
Add-Ons & Services

DynamicAny double to float conversion

Please post support and help requests here.

DynamicAny double to float conversion

Postby WITTROCK » 26 Mar 2009, 19:31

I have been using DynamicAny, and have run into a problem converting from a double or a std::string to a float. The following code produces an exception.

Code: Select all
DynamicAny any("0");
float f = any; // an "Out of range: Value to small" exception occurs here


and also
Code: Select all
double d = 0;
DynamicAny any(d)
float f = any; // an "Out of range: Value to small" exception occurs here


It looks like the problem is occurring in the DynamicAnyHolder::checkLowerLimit() method.
Code: Select all
template <typename F, typename T>
void checkLowerLimit(const F& from, T&) const
{
   if (from < std::numeric_limits<T>::min())
      throw RangeException("Value too small.");
}


the std::numeric_limits<T>::min() function behaves differently for integer and floating point types. For integer types it returns the min value as most would expect, but for floating point types it returns the smallest positive number the type can represent. I don't know the history behind this, but I guess the solution would be something like this:
Code: Select all
template <typename F, typename T>
void checkLowerLimit(const F& from, T&) const
{
   if (std::numeric_limits<T>::is_integer)
   {
      if (from < std::numeric_limits<T>::min())
         throw RangeException("Value too small.");
   }
   else
   {
     // if (from < -std::numeric_limits<T>::min()) edited 03/30/09
      if (from < -std::numeric_limits<T>::max())
         throw RangeException("Value too small.");
   }
}


Probably a cleaner way, but that's what comes to mind.

-WITTROCK
Last edited by WITTROCK on 30 Mar 2009, 16:35, edited 2 times in total.
WITTROCK
 
Posts: 4
Joined: 17 Feb 2009, 17:35

Re: DynamicAny double to float conversion

Postby alex » 26 Mar 2009, 20:12

Thanks. I have posted bug report.
alex
 
Posts: 1101
Joined: 11 Jul 2006, 16:27
Location: United_States

Re: DynamicAny double to float conversion

Postby WITTROCK » 30 Mar 2009, 16:36

edit - Sorry, -std::numeric_limits<T>::min() should read -std::numeric_limits<T>::max()

-WITTROCK
WITTROCK
 
Posts: 4
Joined: 17 Feb 2009, 17:35


Return to Support

Who is online

Users browsing this forum: No registered users and 1 guest

cron