Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length



Pages: [1]
  Print  
Author Topic: Micro Framework Bug in Double.Parse  (Read 32 times)
Andy.NApex
Newbie
*
Posts: 2


View Profile WWW
« on: March 08, 2010, 01:57:31 PM »

I wasn't sure where at Microsoft I can submit this as a bug, so I figured I would start here since I am using your hardware.

I have found a bug in the .NET Micro Framework.  It occurs when you try to Parse a string into a Double using Double.Parse() or Convert.ToDouble().
The Function works correctly except when the value is between '0.0' and '-1.0'. It then incorrectly reports the values as positive.

A little digging into the Double.Parse code in the Micro Framework, I have found where this happens.
The Double.Parse calls Convert.ToDouble() which calls a private static function GetDoubleNumber(). The problem occurs because the Convert.ToDouble splits the string at the decimal point and calls GetDoubleNumber on each part.

When you have a number such as '-0.5' the Convert.ToDouble returns '0.5' because the function calls GetDoubleNumber on '-0' and '5' and the GetDoubleNumber does not handle returning a -0.  This is an issue in v3.0 and v4.0, I've included code to show the original and the 'fix' that I implemented.

Original GetDoubleNumber in Micro Framework
Code:
private static double GetDoubleNumber(char[] chars)
{
   double num2 = 1.0;
   double num3 = 0.0;
   int num4 = ((chars[0] == '-') || (chars[0] == '+')) ? 1 : 0;
   int num5 = (chars[0] == '-') ? -1 : 1;
   for (int i = chars.Length - 1; i >= num4; i--)
   {
      if ((chars[i] < '0') || (chars[i] > '9'))
      {
         throw new Exception();
      }
      int num = chars[i] - '0';
      num3 += num * num2;
      num2 *= 10.0;
   }
   return (num3 * num5);
}

Modified so it works GetDoubleNumber
Code:
  private static double GetDoubleNumber( char[] chars )
   {
      double num2 = 1.0;
      double num3 = 0.0;
      int num4 = ( ( chars[ 0 ] == '-' ) || ( chars[ 0 ] == '+' ) ) ? 1 : 0;
      int num5 = ( chars[ 0 ] == '-' ) ? -1 : 1;
      for ( int i = chars.Length - 1; i >= num4; i-- )
      {
         if ( ( chars[ i ] < '0' ) || ( chars[ i ] > '9' ) )
         {
            throw new Exception();
         }
         int num = chars[ i ] - '0';
         num3 += num * num2;
         num2 *= 10.0;
      }
      //==============================
      // If num3 (integer magnatude) is 0 and num5 is -1 (sign), then
      // the sign is lost because this function will return 0 (no sign)
      // Kludge fix is to return a smallest negative fraction just below
      // zero when num3 is 0 and num5 is -1
      //==============================
      //return (num3 * num5);
      if ( ( num3 == 0 ) && ( num5 == -1 ) )
         return -Double.Epsilon;
      else
         return ( num3 * num5 );
      //==============================
   }
« Last Edit: March 10, 2010, 09:11:26 AM by Andy.NApex » Logged
SupportAdmin
Administrator
Hero Member
*****
Posts: 4595



View Profile WWW
« Reply #1 on: March 08, 2010, 02:16:16 PM »

Thanks for the details, we look into it on our and and forward this to Microsoft.
Logged
Pages: [1]
  Print  
 
Jump to: