dotgnu-pnet-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[dotgnu-pnet-commits] pnetlib ChangeLog runtime/System/Byte.cs runtim...


From: Heiko Weiss
Subject: [dotgnu-pnet-commits] pnetlib ChangeLog runtime/System/Byte.cs runtim...
Date: Fri, 25 May 2007 05:49:27 +0000

CVSROOT:        /sources/dotgnu-pnet
Module name:    pnetlib
Changes by:     Heiko Weiss <brubbel>   07/05/25 05:49:27

Modified files:
        .              : ChangeLog 
        runtime/System : Byte.cs Double.cs Int16.cs Int32.cs Int64.cs 
                         SByte.cs Single.cs UInt16.cs UInt32.cs 
                         UInt64.cs 
        runtime/System/Private/NumberFormat: FixedPointFormatter.cs 
                                             Formatter.cs 
                                             GeneralFormatter.cs 
                                             ScientificFormatter.cs 

Log message:
        optimized performance and do correct formatting.

CVSWeb URLs:
http://cvs.savannah.gnu.org/viewcvs/pnetlib/ChangeLog?cvsroot=dotgnu-pnet&r1=1.2500&r2=1.2501
http://cvs.savannah.gnu.org/viewcvs/pnetlib/runtime/System/Byte.cs?cvsroot=dotgnu-pnet&r1=1.9&r2=1.10
http://cvs.savannah.gnu.org/viewcvs/pnetlib/runtime/System/Double.cs?cvsroot=dotgnu-pnet&r1=1.15&r2=1.16
http://cvs.savannah.gnu.org/viewcvs/pnetlib/runtime/System/Int16.cs?cvsroot=dotgnu-pnet&r1=1.11&r2=1.12
http://cvs.savannah.gnu.org/viewcvs/pnetlib/runtime/System/Int32.cs?cvsroot=dotgnu-pnet&r1=1.13&r2=1.14
http://cvs.savannah.gnu.org/viewcvs/pnetlib/runtime/System/Int64.cs?cvsroot=dotgnu-pnet&r1=1.10&r2=1.11
http://cvs.savannah.gnu.org/viewcvs/pnetlib/runtime/System/SByte.cs?cvsroot=dotgnu-pnet&r1=1.10&r2=1.11
http://cvs.savannah.gnu.org/viewcvs/pnetlib/runtime/System/Single.cs?cvsroot=dotgnu-pnet&r1=1.10&r2=1.11
http://cvs.savannah.gnu.org/viewcvs/pnetlib/runtime/System/UInt16.cs?cvsroot=dotgnu-pnet&r1=1.9&r2=1.10
http://cvs.savannah.gnu.org/viewcvs/pnetlib/runtime/System/UInt32.cs?cvsroot=dotgnu-pnet&r1=1.10&r2=1.11
http://cvs.savannah.gnu.org/viewcvs/pnetlib/runtime/System/UInt64.cs?cvsroot=dotgnu-pnet&r1=1.10&r2=1.11
http://cvs.savannah.gnu.org/viewcvs/pnetlib/runtime/System/Private/NumberFormat/FixedPointFormatter.cs?cvsroot=dotgnu-pnet&r1=1.4&r2=1.5
http://cvs.savannah.gnu.org/viewcvs/pnetlib/runtime/System/Private/NumberFormat/Formatter.cs?cvsroot=dotgnu-pnet&r1=1.16&r2=1.17
http://cvs.savannah.gnu.org/viewcvs/pnetlib/runtime/System/Private/NumberFormat/GeneralFormatter.cs?cvsroot=dotgnu-pnet&r1=1.10&r2=1.11
http://cvs.savannah.gnu.org/viewcvs/pnetlib/runtime/System/Private/NumberFormat/ScientificFormatter.cs?cvsroot=dotgnu-pnet&r1=1.7&r2=1.8

Patches:
Index: ChangeLog
===================================================================
RCS file: /sources/dotgnu-pnet/pnetlib/ChangeLog,v
retrieving revision 1.2500
retrieving revision 1.2501
diff -u -b -r1.2500 -r1.2501
--- ChangeLog   23 May 2007 20:16:41 -0000      1.2500
+++ ChangeLog   25 May 2007 05:49:26 -0000      1.2501
@@ -1,3 +1,17 @@
+2007-05-25  Heiko Weiss <address@hidden>
+
+       * runtime/System/Byte.cs, runtime/System/Double.cs,
+       runtime/System/Int16.cs, runtime/System/Int32.cs,
+       runtime/System/Int64.cs, runtime/System/SByte.cs,
+       runtime/System/Single.cs, runtime/System/UInt16.cs,
+       runtime/System/UInt32.cs, runtime/System/UInt64.cs,
+       runtime/System/Private/NumberFormat/FixedPointFormatter.cs,
+       runtime/System/Private/NumberFormat/Formatter.cs,
+       runtime/System/Private/NumberFormat/GeneralFormatter.cs,
+       runtime/System/Private/NumberFormat/ScientificFormatter.cs:
+       optimized performance and do correct formatting.
+
+
 2007-05-23  Klaus Treichel  <address@hidden>
 
        * runtime/Microsoft/Win32/RegistryValueOptions.cs: Add the System using

Index: runtime/System/Byte.cs
===================================================================
RCS file: /sources/dotgnu-pnet/pnetlib/runtime/System/Byte.cs,v
retrieving revision 1.9
retrieving revision 1.10
diff -u -b -r1.9 -r1.10
--- runtime/System/Byte.cs      27 Nov 2002 14:37:27 -0000      1.9
+++ runtime/System/Byte.cs      25 May 2007 05:49:26 -0000      1.10
@@ -65,9 +65,7 @@
                        }
        public String ToString(String format, IFormatProvider provider)
                        {
-                               if (format == null) format = "G";
-                               return 
-                                       
Formatter.CreateFormatter(format).Format(this, provider);
+                               return Formatter.FormatByte( value_, format, 
provider );
                        }
 
        // Parsing methods.

Index: runtime/System/Double.cs
===================================================================
RCS file: /sources/dotgnu-pnet/pnetlib/runtime/System/Double.cs,v
retrieving revision 1.15
retrieving revision 1.16
diff -u -b -r1.15 -r1.16
--- runtime/System/Double.cs    18 Oct 2006 08:16:01 -0000      1.15
+++ runtime/System/Double.cs    25 May 2007 05:49:26 -0000      1.16
@@ -126,9 +126,7 @@
                                        }
                                }
                                
-                               if (format == null) format = "G";
-                               return
-                                       
Formatter.CreateFormatter(format).Format(this, provider);
+                               return Formatter.FormatDouble( value_, format, 
provider );
                        }
 
        // Parsing methods.

Index: runtime/System/Int16.cs
===================================================================
RCS file: /sources/dotgnu-pnet/pnetlib/runtime/System/Int16.cs,v
retrieving revision 1.11
retrieving revision 1.12
diff -u -b -r1.11 -r1.12
--- runtime/System/Int16.cs     21 Jul 2003 16:54:45 -0000      1.11
+++ runtime/System/Int16.cs     25 May 2007 05:49:26 -0000      1.12
@@ -68,9 +68,7 @@
                        }
        public String ToString(String format, IFormatProvider provider)
                        {
-                               if (format == null) format = "G";
-                               return 
-                                       
Formatter.CreateFormatter(format).Format(this, provider);
+                               return Formatter.FormatInt16( value_, format, 
provider );
                        }
 
        // Parsing methods.

Index: runtime/System/Int32.cs
===================================================================
RCS file: /sources/dotgnu-pnet/pnetlib/runtime/System/Int32.cs,v
retrieving revision 1.13
retrieving revision 1.14
diff -u -b -r1.13 -r1.14
--- runtime/System/Int32.cs     21 Jul 2003 16:54:45 -0000      1.13
+++ runtime/System/Int32.cs     25 May 2007 05:49:26 -0000      1.14
@@ -65,9 +65,7 @@
                        }
        public String ToString(String format, IFormatProvider provider)
                        {
-                               if (format == null) format = "G";
-                               return 
-                                       
Formatter.CreateFormatter(format).Format(this, provider);
+                               return Formatter.FormatInt32( value_, format, 
provider );
                        }
 
        // Parsing methods.

Index: runtime/System/Int64.cs
===================================================================
RCS file: /sources/dotgnu-pnet/pnetlib/runtime/System/Int64.cs,v
retrieving revision 1.10
retrieving revision 1.11
diff -u -b -r1.10 -r1.11
--- runtime/System/Int64.cs     21 Jul 2003 16:54:45 -0000      1.10
+++ runtime/System/Int64.cs     25 May 2007 05:49:26 -0000      1.11
@@ -67,9 +67,7 @@
                        }
        public String ToString(String format, IFormatProvider provider)
                        {
-                               if (format == null) format = "G";
-                               return
-                                       
Formatter.CreateFormatter(format).Format(this, provider);
+                               return Formatter.FormatInt64( value_, format, 
provider );
                        }
 
        // Parsing methods.

Index: runtime/System/SByte.cs
===================================================================
RCS file: /sources/dotgnu-pnet/pnetlib/runtime/System/SByte.cs,v
retrieving revision 1.10
retrieving revision 1.11
diff -u -b -r1.10 -r1.11
--- runtime/System/SByte.cs     27 Nov 2002 14:37:27 -0000      1.10
+++ runtime/System/SByte.cs     25 May 2007 05:49:26 -0000      1.11
@@ -68,9 +68,7 @@
                        }
        public String ToString(String format, IFormatProvider provider)
                        {
-                               if (format == null) format = "G";
-                               return
-                                       
Formatter.CreateFormatter(format).Format(this, provider);
+                               return Formatter.FormatSByte( value_, format, 
provider );
                        }
 
        // Parsing methods.

Index: runtime/System/Single.cs
===================================================================
RCS file: /sources/dotgnu-pnet/pnetlib/runtime/System/Single.cs,v
retrieving revision 1.10
retrieving revision 1.11
diff -u -b -r1.10 -r1.11
--- runtime/System/Single.cs    15 Apr 2003 07:27:54 -0000      1.10
+++ runtime/System/Single.cs    25 May 2007 05:49:26 -0000      1.11
@@ -87,9 +87,7 @@
                        }
        public String ToString(String format, IFormatProvider provider)
                        {
-                               if (format == null) format = "G";
-                               return
-                                       
Formatter.CreateFormatter(format).Format(this, provider);
+                               return Formatter.FormatSingle( value_, format, 
provider );
                        }
 
        // Value testing methods.

Index: runtime/System/UInt16.cs
===================================================================
RCS file: /sources/dotgnu-pnet/pnetlib/runtime/System/UInt16.cs,v
retrieving revision 1.9
retrieving revision 1.10
diff -u -b -r1.9 -r1.10
--- runtime/System/UInt16.cs    27 Nov 2002 14:37:27 -0000      1.9
+++ runtime/System/UInt16.cs    25 May 2007 05:49:26 -0000      1.10
@@ -66,9 +66,7 @@
                        }
        public String ToString(String format, IFormatProvider provider)
                        {
-                               if (format == null) format = "G";
-                               return 
-                                       
Formatter.CreateFormatter(format).Format(this, provider);
+                               return Formatter.FormatUInt16( value_, format, 
provider );
                        }
 
        // Parsing methods.

Index: runtime/System/UInt32.cs
===================================================================
RCS file: /sources/dotgnu-pnet/pnetlib/runtime/System/UInt32.cs,v
retrieving revision 1.10
retrieving revision 1.11
diff -u -b -r1.10 -r1.11
--- runtime/System/UInt32.cs    21 Jul 2003 16:54:45 -0000      1.10
+++ runtime/System/UInt32.cs    25 May 2007 05:49:26 -0000      1.11
@@ -68,9 +68,7 @@
                        }
        public String ToString(String format, IFormatProvider provider)
                        {
-                               if (format == null) format ="G";
-                               return 
-                                       
Formatter.CreateFormatter(format).Format(this, provider);
+                               return Formatter.FormatUInt32( value_, format, 
provider );
                        }
 
        // Parsing methods.

Index: runtime/System/UInt64.cs
===================================================================
RCS file: /sources/dotgnu-pnet/pnetlib/runtime/System/UInt64.cs,v
retrieving revision 1.10
retrieving revision 1.11
diff -u -b -r1.10 -r1.11
--- runtime/System/UInt64.cs    21 Jul 2003 16:54:45 -0000      1.10
+++ runtime/System/UInt64.cs    25 May 2007 05:49:26 -0000      1.11
@@ -70,9 +70,7 @@
 
        public String ToString(String format, IFormatProvider provider)
                        {
-                               if (format == null) format = "G";
-                               return
-                                       
Formatter.CreateFormatter(format).Format(this, provider);
+                               return Formatter.FormatUInt64( value_, format, 
provider );
                        }
 
        // Parsing methods.

Index: runtime/System/Private/NumberFormat/FixedPointFormatter.cs
===================================================================
RCS file: 
/sources/dotgnu-pnet/pnetlib/runtime/System/Private/NumberFormat/FixedPointFormatter.cs,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -b -r1.4 -r1.5
--- runtime/System/Private/NumberFormat/FixedPointFormatter.cs  10 May 2007 
10:38:58 -0000      1.4
+++ runtime/System/Private/NumberFormat/FixedPointFormatter.cs  25 May 2007 
05:49:26 -0000      1.5
@@ -35,6 +35,10 @@
 
        internal static string Format(Object o, int precision, IFormatProvider 
provider)
        {
+#if CONFIG_EXTENDED_NUMERICS
+               if( !IsDecimal(o) )
+                       return Formatter.FormatDouble( OToDouble(o), precision, 
true, provider );
+#endif
                //  Get string
                string rawnumber = FormatAnyRound(o, precision, provider);
                StringBuilder ret = new StringBuilder();

Index: runtime/System/Private/NumberFormat/Formatter.cs
===================================================================
RCS file: 
/sources/dotgnu-pnet/pnetlib/runtime/System/Private/NumberFormat/Formatter.cs,v
retrieving revision 1.16
retrieving revision 1.17
diff -u -b -r1.16 -r1.17
--- runtime/System/Private/NumberFormat/Formatter.cs    10 May 2007 10:38:58 
-0000      1.16
+++ runtime/System/Private/NumberFormat/Formatter.cs    25 May 2007 05:49:27 
-0000      1.17
@@ -34,6 +34,8 @@
        //
        private const string validformats = "CcDdEeFfGgNnPpRrXx";
        static private IDictionary formats = new Hashtable();
+       // We need this ULongMaxValue, because cscc has a bug converting 
ulong.MaxValue to double
+       static private double ULongMaxValue = 18446744073709551615d;
 
        // 
----------------------------------------------------------------------
        //  Protected data for other methods
@@ -438,7 +440,7 @@
                return ret.ToString();
        }
 
-       internal static int GetExponent( double value ) {
+       internal static int GetExponent( double value, int precision ) {
                // return (int)Math.Floor(Math.Log10(Math.Abs(value)));
                /*
                        Note:
@@ -448,25 +450,47 @@
                        so the exponent would be one to big.
                        So the Method below is now used to calculate the 
exponent.
                */
+
                if( value == 0.0 ) return 0;
                
-               value = Math.Abs(value);
-               int exponent = 0;
-               if( value >= 1.0 ) {
-                       while( value >= 10.0 ) {
+               int exponent = (int)Math.Floor(Math.Log10(Math.Abs(value)));
+
+               if( exponent >= -4 && exponent < 15 ) {
+       
+                       double work = Math.Abs(value);
+                       if( precision >=0 ) work += 5*Math.Pow(10, -precision-1 
);
+               
+                       if( work < ULongMaxValue ) {
+                               ulong  value1 = (ulong)work;
+                               exponent = ULog10( value1 );
+               
+                               work -= value1;
+                               work *= Math.Pow(10, 15 - exponent -1 );
+               
+                               ulong  value2 = (ulong) (work+.5);
+               
+                               if( value2 != 0 ) {
+                                       ulong  test   = (ulong) work;
+                                       // check for rounding error
+                                       // if floor Log10(test) is not equal 
then floor Log10(value2) then value2 is factor 10 too big
+                                       // so increment value1 by 1 and set 
value2 to 0 -> return new value1
+                                       if( (test != value2) && ( ULog10(test) 
!= ULog10(value2) ) ) {
                                exponent++;
-                               value /= 10.0;
+                                       }
+               
+                                       if( exponent < 0 ) {
+                                               exponent = ULog10( value2 ) - 
15;
                        } 
                }
-               else {
-                       while( value <= 1.0 ) {
-                               exponent--;
-                               value *= 10.0;
                        } 
                }
                return exponent;
        }
        
+       internal static int GetExponent( double value ) {
+               return GetExponent( value, -1 );
+       }
+       
        static protected string FormatFloat(double value, int precision)
        {
                if (value == 0.0) return ".";
@@ -489,7 +513,7 @@
                        new 
StringBuilder(FormatInteger((ulong)Math.Floor(work)), 30 );
                sb.Remove(sb.Length-1, 1);    // Ditch the trailing decimal 
point
 
-               if (sb.Length > precision + exponent + 1)
+               if (sb.Length > 0 && sb.Length > precision + exponent + 1)
                {
                        sb.Remove(precision+exponent+1, 
sb.Length-(precision+exponent+1));
                }       
@@ -671,6 +695,378 @@
        //
        public abstract string Format(Object o, IFormatProvider provider);
 
+       static bool UseScientificFormatter( string format, out int precision ) {
+
+               precision = -1;
+
+               if( null != format && format.Length > 1 && format.Length <= 3 
&& (format[0] == 'E' || format[0] == 'e') ) {
+                       try 
+                       {
+                               precision = Byte.Parse(format.Substring(1));
+                               return true;
+                       }
+                       catch (FormatException)
+                       {
+                               return false;
+                       }
+               }
+               return false;
+       }
+
+       static bool UseStandardFormatter( string format, out int precision ) {
+
+               precision = -1;
+
+               if ( null == format || format == "G" || format == "g" || 
format.Length == 0 || format == "G0" ) return true;
+
+               if( format.Length > 1 && format.Length <= 3 && (format[0] == 
'F' || format[0] == 'f') ) {
+                       try 
+                       {
+                               precision = Byte.Parse(format.Substring(1));
+                               return true;
+                       }
+                       catch (FormatException)
+                       {
+                               return false;
+                       }
+               }
+               return false;
+       }
+
+       const string strNull = "0";
+
+       static public string FormatDouble( double value, string format, 
IFormatProvider provider ) {
+               int precision;
+               if( UseStandardFormatter(format, out precision ) ) {
+                       if( value == 0.0 && precision <= 0 ) return strNull;
+
+                       int exponent  = GetExponent(value);
+
+                       if( precision >= 0 ) {
+                               return FormatDouble( value, exponent, 
precision, true, provider );
+                       }
+                       else {
+                               if (exponent >= -4 && exponent < 15) {
+                                       return FormatDouble( value, exponent, 
15, false, provider );
+                               }
+                               else {
+                                       return FormatScientific(value, 
exponent, 15, format, provider);
+                                       //return 
Formatter.CreateFormatter(format == null ? "G" : format).Format(value, 
provider);
+                               }
+                       }
+               }
+               else if( UseScientificFormatter(format, out precision ) ) {
+                       return FormatScientific(value, GetExponent(value), 
precision, format, provider);
+               }
+               
+               return Formatter.CreateFormatter(format == null ? "G" : 
format).Format(value, provider);
+       }
+
+       static public string FormatSingle( float value, string format, 
IFormatProvider provider ) {
+               int precision;
+               if( UseStandardFormatter(format, out precision ) ) {
+                       if( value == 0.0 && precision <= 0 ) return strNull;
+
+                       int exponent  = GetExponent(value);
+               
+                       if( precision >= 0 ) {
+                               return FormatDouble( value, exponent, 
precision, true, provider );
+                       }
+                       else {
+                               if (exponent >= -4 && exponent < 7) {
+                                       return FormatDouble( value, exponent, 
7, false, provider );
+                               }
+                               else {
+                                       return FormatScientific(value, 
exponent, 7, format, provider);
+                                       // return 
Formatter.CreateFormatter(format == null ? "G" : format).Format(value, 
provider);
+                               }
+                       }
+               }
+               else if( UseScientificFormatter(format, out precision ) ) {
+                       return FormatScientific(value, GetExponent(value), 
precision, format, provider);
+               }
+               return Formatter.CreateFormatter(format == null ? "G" : 
format).Format(value, provider);
+       }
+
+       static public string FormatInt64( long value, string format, 
IFormatProvider provider ) {
+               int precision;
+               if( UseStandardFormatter(format, out precision ) ) {
+                       NumberFormatInfo nfi = NumberFormatInfo.GetInstance( 
provider );
+                       StringBuilder sb = new StringBuilder( FormatInt64( 
value, nfi.NegativeSign ) );
+                       if( precision > 0 ) {
+                               sb.Append( nfi.NumberDecimalSeparator );
+                               sb.Append( '0', precision );
+                       }
+                       return sb.ToString();
+               }
+               else if( UseScientificFormatter(format, out precision ) ) {
+                       return FormatScientific(value, GetExponent(value), 
precision, format, provider);
+               }
+               return Formatter.CreateFormatter(format).Format(value, 
provider);
+       }
+
+       static public string FormatUInt64( ulong value, string format, 
IFormatProvider provider ) {
+               int precision;
+               if( UseStandardFormatter(format, out precision ) ) {
+                       if( precision > 0 ) {
+                               NumberFormatInfo nfi = 
NumberFormatInfo.GetInstance( provider );
+                               StringBuilder sb = new StringBuilder( 
FormatUInt64( value ) );
+                               sb.Append( nfi.NumberDecimalSeparator );
+                               sb.Append( '0', precision );
+                               return sb.ToString();
+                       }
+                       else {
+                               return FormatUInt64( value );
+                       }
+               }
+               else if( UseScientificFormatter(format, out precision ) ) {
+                       return FormatScientific(value, GetExponent(value), 
precision, format, provider);
+               }
+               return Formatter.CreateFormatter(format).Format(value, 
provider);
+       }
+
+       static public string FormatInt32( int value, string format, 
IFormatProvider provider ) {
+               return FormatInt64( value, format, provider );
+       }
+
+       static public string FormatUInt32( uint value, string format, 
IFormatProvider provider ) {
+               return FormatUInt64( value, format, provider );
+       }
+
+       static public string FormatInt16( short value, string format, 
IFormatProvider provider ) {
+               return FormatInt64( value, format, provider );
+       }
+
+       static public string FormatUInt16( ushort value, string format, 
IFormatProvider provider ) {
+               return FormatUInt64( value, format, provider );
+       }
+
+       static public string FormatSByte( sbyte value, string format, 
IFormatProvider provider ) {
+               return FormatInt64( value, format, provider );
+       }
+
+       static public string FormatByte( byte value, string format, 
IFormatProvider provider ) {
+               return FormatUInt64( value, format, provider );
+       }
+
+       // General formatting
+
+       // return the absolut value of Log10
+       static int ULog10( ulong val ) {
+               if( val == 0 ) return -1;
+               return (int) Math.Log10( val );
+       }
+
+       static protected string FormatDouble( double value, int exponent, int 
precision, bool FixedPoint, IFormatProvider provider ) {
+
+               NumberFormatInfo nfi = NumberFormatInfo.GetInstance( provider );
+
+               bool negativ = value < 0;
+               double work  = negativ ? -value : value;
+               if( precision < 0 ) precision = 15;
+
+               StringBuilder sb = new StringBuilder(30);
+
+               do {
+                       if( work < ULongMaxValue ) {
+
+                               work += 5*Math.Pow(10,-precision-1 + 
(FixedPoint ? 0 : exponent+1) );
+                               ulong value1 = (ulong) work;
+
+                               if( negativ && value1 != 0 ) sb.Append( 
nfi.NegativeSign );
+                               sb.Append( FormatUInt64( value1 ) );
+
+                               if( precision == 0 ) break;
+
+                               work -= value1;
+                               work *= Math.Pow( 10, 15 );
+                               ulong value2 = (ulong) work;
+
+                               if( value2 == 0 && !FixedPoint ) break;
+
+                               int iCount = FixedPoint ? precision : 
precision-exponent-1;
+                               if( iCount <= 0 ) break;
+
+                               if( value2 == 0 ) {
+                                       sb.Append( nfi.NumberDecimalSeparator );
+                                       sb.Append( '0', iCount );
+                                       break;
+                               }
+
+                               int iLen   = ULog10( value2 )+1;
+                               int iNull  = 15-iLen;
+                               if( iNull >= iCount ) {
+                                       value2 = 0;
+                                       if( FixedPoint ) {
+                                               sb.Append( 
nfi.NumberDecimalSeparator );
+                                               sb.Append( '0', iCount );
+                                       }
+                               }
+                               else {
+                                       iCount -= iNull;
+                                       if( FixedPoint || iCount > 0 ) {
+                                               sb.Append( 
nfi.NumberDecimalSeparator );
+                                               if( iNull > 0 ) {
+                                                       sb.Append( '0', iNull );
+                                               }
+                                               if( iLen <= iCount ) {
+                                                       sb.Append( 
FormatUInt64( value2 ) );
+                                                       iCount -= iLen;
+                                                       if( iCount > 0 && 
FixedPoint ) sb.Append( '0', iCount );
+                                               }
+                                               else {
+                                                       sb.Append( 
FormatUInt64( value2 ).Substring( 0, iCount ) );
+                                               }
+                                               if( !FixedPoint ) {
+                                                       while( 
sb[sb.Length-1]=='0') sb.Remove(sb.Length-1,1);
+                                               }
+                                       }
+                               }
+
+                               if( negativ && value1 == 0 && value2 != 0 ) {
+                                       sb.Insert( 0, nfi.NegativeSign );
+                               }
+                       }
+                       else {
+                               if( negativ  ) sb.Append( nfi.NegativeSign );
+                               int pow;
+                               if( exponent > precision && exponent > 15 ) {
+                                       pow = exponent-15+1;
+                               }
+                               else {
+                                       pow = exponent-precision+1;
+                               }
+                               work /= Math.Pow(10,pow);
+                               work += .5;
+                               sb.Append( FormatUInt64( (ulong)work ) );
+                               sb.Append( '0', pow );
+
+                               if( FixedPoint && precision > 0 ) {
+                                       sb.Append( nfi.NumberDecimalSeparator );
+                                       sb.Append( '0', precision );
+                               }
+                       }
+               }while(false);
+
+               return sb.ToString();
+       }
+
+       static protected string FormatDouble( double value, int precision, bool 
FixedPoint, IFormatProvider provider ) {
+               return FormatDouble( value, GetExponent(value, precision ), 
precision, FixedPoint, provider );
+       }
+       
+       static protected string FormatScientific(double value, int exponent, 
int precision, string format, IFormatProvider provider)
+       {
+               if (Double.IsNaN(value)) {
+                       return NumberFormatInfo(provider).NaNSymbol;
+               }
+               else if(Double.IsPositiveInfinity(value)) {
+                       return 
NumberFormatInfo(provider).PositiveInfinitySymbol;
+               }
+               else if(Double.IsNegativeInfinity(value)) {
+                       return 
NumberFormatInfo(provider).NegativeInfinitySymbol;
+               }
+               else if( value == double.Epsilon ) {
+                       return "4" + 
NumberFormatInfo(provider).NumberDecimalSeparator + "94065645841247E-324";
+               }
+
+               bool fixPrecision = false;
+               bool isNegative = false;
+               int  iPadLeft   = 2;
+               char Ee         = 'E';
+               if( null != format && format.Length > 0 ) {
+                       switch( format[0] ) {
+                               case 'g' : 
+                                       Ee = 'e';
+                                       iPadLeft = 2;
+                                       break;
+                               case 'G' : 
+                                       Ee = 'E';
+                                       iPadLeft = 2;
+                                       break;
+                               case 'e' : 
+                                       Ee = 'e';
+                                       iPadLeft = 3;
+                                       fixPrecision = true;
+                                       break;
+                               case 'E' : 
+                                       Ee = 'E';
+                                       iPadLeft = 3;
+                                       fixPrecision = true;
+                                       break;
+                       }
+               }
+
+               if (value < 0) {
+                       isNegative = true;
+                       value = -value;
+               }
+
+               double mantissa = value / Math.Pow(10, exponent);
+               
+               if (Double.IsNaN(mantissa))
+               {
+                       return NumberFormatInfo(provider).NaNSymbol;
+               }
+               else if(Double.IsPositiveInfinity(mantissa))
+               {
+                       return 
NumberFormatInfo(provider).PositiveInfinitySymbol;
+               }
+               else if(Double.IsNegativeInfinity(mantissa))
+               {
+                       return 
NumberFormatInfo(provider).NegativeInfinitySymbol;
+               }
+
+               StringBuilder ret = new StringBuilder( FormatDouble( mantissa, 
precision, fixPrecision, provider ) );
+               if (isNegative)
+               {
+                       ret.Insert(0, NumberFormatInfo(provider).NegativeSign);
+               }
+
+               ret.Append(Ee);
+               if (exponent < 0)
+               {
+                       ret.Append(NumberFormatInfo(provider).NegativeSign);
+               }
+               else
+               {
+                       ret.Append(NumberFormatInfo(provider).PositiveSign);
+               }
+               ret.Append( FormatUInt64( (ulong) Math.Abs(exponent) 
).PadLeft(iPadLeft,'0') );
+               return ret.ToString();
+       }               
+       
+
+       static private string FormatInt64( long value, string sign ) {
+               if( value == 0 ) return strNull;
+               if( value == long.MinValue ) return "-9223372036854775808";
+
+               StringBuilder ret = new StringBuilder(25);
+               long work = value;
+               if( work < 0 ) {
+                       work = -work;
+               }
+               while (work > 0) {
+                       ret.Insert(0, decimalDigits[work % 10]);
+                       work /= 10;
+               }
+               if( value < 0 ) ret.Insert(0, sign );
+               return ret.ToString();
+       }
+
+       static private string FormatUInt64( ulong value ) {
+               if( value == 0 ) return strNull;
+
+               StringBuilder ret = new StringBuilder(25);
+               ulong work = value;
+               while (work > 0) {
+                       ret.Insert(0, decimalDigits[work % 10]);
+                       work /= 10;
+               }
+               return ret.ToString();
+       }
+
+
 } // class Formatter
 
 } // namespace System.Private.Format

Index: runtime/System/Private/NumberFormat/GeneralFormatter.cs
===================================================================
RCS file: 
/sources/dotgnu-pnet/pnetlib/runtime/System/Private/NumberFormat/GeneralFormatter.cs,v
retrieving revision 1.10
retrieving revision 1.11
diff -u -b -r1.10 -r1.11
--- runtime/System/Private/NumberFormat/GeneralFormatter.cs     10 May 2007 
10:38:58 -0000      1.10
+++ runtime/System/Private/NumberFormat/GeneralFormatter.cs     25 May 2007 
05:49:27 -0000      1.11
@@ -31,10 +31,12 @@
 internal class GeneralFormatter : Formatter
 {
        char Ee;
+       char Gg;
 
        public GeneralFormatter(int precision, char Gg)
        {
                this.precision = precision;
+               this.Gg = Gg;
                switch(Gg)
                {
                        case 'G':
@@ -154,13 +156,21 @@
 
                if (exponent >= -4 && exponent < precision)
                {
+#if CONFIG_EXTENDED_NUMERICS
+                       if( !IsDecimal(o) )
+                               return Formatter.FormatDouble( val, exponent, 
precision, false, provider );
+#endif
                        return StripTrail(
                                        FixedPointFormatter.Format(o, 
precision, provider),
                                        provider);
                }
                else
                {
+#if CONFIG_EXTENDED_NUMERICS
+                       return Formatter.FormatScientific( val, exponent, 
precision, this.Gg.ToString(), provider);
+#else
                        return new ScientificFormatter(precision, Ee).Format(o, 
provider);
+#endif
                }
        }               
 

Index: runtime/System/Private/NumberFormat/ScientificFormatter.cs
===================================================================
RCS file: 
/sources/dotgnu-pnet/pnetlib/runtime/System/Private/NumberFormat/ScientificFormatter.cs,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -b -r1.7 -r1.8
--- runtime/System/Private/NumberFormat/ScientificFormatter.cs  13 Apr 2006 
15:18:08 -0000      1.7
+++ runtime/System/Private/NumberFormat/ScientificFormatter.cs  25 May 2007 
05:49:27 -0000      1.8
@@ -41,6 +41,10 @@
        public override string Format(Object o, IFormatProvider provider)
        {
 #if CONFIG_EXTENDED_NUMERICS
+               double value = OToDouble(o);
+               return Formatter.FormatScientific( value, 
Formatter.GetExponent( value ), precision, Ee.ToString(), provider);
+               /*
+
                bool isNegative = false;
 
                double value;
@@ -64,6 +68,11 @@
                {
                        return 
NumberFormatInfo(provider).NegativeInfinitySymbol;
                }
+               else if( value == double.Epsilon ) {
+                return "4" + NumberFormatInfo(provider).NumberDecimalSeparator 
+ "94065645841247E-324";
+               }
+
+               
                if (value < 0)
                {
                        isNegative = true;
@@ -81,6 +90,18 @@
                        //exponent = (int) Math.Floor(Math.Log10(value));
                        exponent = Formatter.GetExponent( value );
                        mantissa = value / Math.Pow(10, exponent);
+                       if (Double.IsNaN(mantissa))
+                       {
+                               return NumberFormatInfo(provider).NaNSymbol;
+                       }
+                       else if(Double.IsPositiveInfinity(mantissa))
+                       {
+                               return 
NumberFormatInfo(provider).PositiveInfinitySymbol;
+                       }
+                       else if(Double.IsNegativeInfinity(mantissa))
+                       {
+                               return 
NumberFormatInfo(provider).NegativeInfinitySymbol;
+                       }
                }
 
                rawnumber = FormatFloat(mantissa, precision);
@@ -118,6 +139,7 @@
                                                                                
                        .PadLeft(3,'0'));
 
                return ret.ToString();
+               */
 #else // !CONFIG_EXTENDED_NUMERICS
                return String.Empty;
 #endif // !CONFIG_EXTENDED_NUMERICS




reply via email to

[Prev in Thread] Current Thread [Next in Thread]