2002-08-21 Miguel de Icaza <miguel@ximian.com>
[mono.git] / mcs / class / corlib / System / Double.cs
index ddc8b3e8e32659fe9abf4d8c27ae26c4a1d694dc..7f72c334561992a7ad3dd881b45001cd0c08d56e 100644 (file)
@@ -35,22 +35,29 @@ namespace System {
                        if (!(v is System.Double))
                                throw new ArgumentException (Locale.GetText ("Value is not a System.Double"));
 
-                       if (IsPositiveInfinity(value) && IsPositiveInfinity((double) v)){
+                       double dv = (double)v;
+
+                       if (IsPositiveInfinity(value) && IsPositiveInfinity(dv))
                                return 0;
-                       }
 
-                       if (IsNegativeInfinity(value) && IsNegativeInfinity((double) v)){
+                       if (IsNegativeInfinity(value) && IsNegativeInfinity(dv))
                                return 0;
-                       }
 
-                       if (IsNaN((double) v)) {
+                       if (IsNaN(dv))
                                if (IsNaN(value))
                                        return 0;
                                else
                                        return 1;
-                       }
 
-                       return (int) (value - ((double) v));
+                       if (IsNaN(value))
+                               if (IsNaN(dv))
+                                       return 0;
+                               else
+                                       return -1;
+
+                       if (value > dv) return 1;
+                       else if (value < dv) return -1;
+                       else return 0;
                }
 
                public override bool Equals (object o)
@@ -108,9 +115,14 @@ namespace System {
                        return Parse (s, style, null);
                }
 
-               enum State {
-                       AllowSign, Digits, Decimal, ExponentSign, Exponent, ConsumeWhiteSpace
-               }
+               // We're intentionally using constants here to avoid some bigger headaches in mcs.
+               // This struct must be compiled before System.Enum so we can't use enums here.
+               private const int State_AllowSign = 1;
+               private const int State_Digits = 2;
+               private const int State_Decimal = 3;
+               private const int State_ExponentSign = 4;
+               private const int State_Exponent = 5;
+               private const int State_ConsumeWhiteSpace = 6;
                
                [MonoTODO("check if digits are group in correct numbers between the group separators")]
                public static double Parse (string s, NumberStyles style, IFormatProvider provider)
@@ -148,7 +160,7 @@ namespace System {
                        //
                        // Machine state
                        //
-                       State state = State.AllowSign;
+                       int state = State_AllowSign;
 
                        //
                        // Setup
@@ -172,33 +184,33 @@ namespace System {
                                c = s [sidx];
 
                                switch (state){
-                               case State.AllowSign:
+                               case State_AllowSign:
                                        if ((style & NumberStyles.AllowLeadingSign) != 0){
                                                if (c == positive [0] &&
                                                    s.Substring (sidx, positive.Length) == positive){
-                                                       state = State.Digits;
+                                                       state = State_Digits;
                                                        sidx += positive.Length-1;
                                                        continue;
                                                }
 
                                                if (c == negative [0] &&
                                                    s.Substring (sidx, negative.Length) == negative){
-                                                       state = State.Digits;
+                                                       state = State_Digits;
                                                        b [didx++] = (byte) '-';
                                                        sidx += negative.Length-1;
                                                        continue;
                                                }
                                        }
-                                       state = State.Digits;
-                                       goto case State.Digits;
+                                       state = State_Digits;
+                                       goto case State_Digits;
                                        
-                               case State.Digits:
+                               case State_Digits:
                                        if (Char.IsDigit (c)){
                                                b [didx++] = (byte) c;
                                                break;
                                        }
                                        if (c == 'e' || c == 'E')
-                                               goto case State.Decimal;
+                                               goto case State_Decimal;
                                        
                                        if (decimal_separator != null &&
                                            decimal_separator [0] == c){
@@ -206,7 +218,7 @@ namespace System {
                                                    decimal_separator){
                                                        b [didx++] = (byte) '.';
                                                        sidx += decimal_separator_len-1;
-                                                       state = State.Decimal; 
+                                                       state = State_Decimal; 
                                                        break;
                                                }
                                        }
@@ -215,17 +227,17 @@ namespace System {
                                                if (s.Substring (sidx, group_separator_len) ==
                                                    group_separator){
                                                        sidx += group_separator_len-1;
-                                                       state = State.Digits; 
+                                                       state = State_Digits; 
                                                        break;
                                                }
                                        }
                                        
                                        if (Char.IsWhiteSpace (c))
-                                               goto case State.ConsumeWhiteSpace;
+                                               goto case State_ConsumeWhiteSpace;
 
                                        throw new FormatException ("Unknown char: " + c);
 
-                               case State.Decimal:
+                               case State_Decimal:
                                        if (Char.IsDigit (c)){
                                                b [didx++] = (byte) c;
                                                break;
@@ -235,51 +247,51 @@ namespace System {
                                                if ((style & NumberStyles.AllowExponent) == 0)
                                                        throw new FormatException ("Unknown char: " + c);
                                                b [didx++] = (byte) c;
-                                               state = State.ExponentSign;
+                                               state = State_ExponentSign;
                                                break;
                                        }
                                        
                                        if (Char.IsWhiteSpace (c))
-                                               goto case State.ConsumeWhiteSpace;
+                                               goto case State_ConsumeWhiteSpace;
                                        throw new FormatException ("Unknown char: " + c);
 
-                               case State.ExponentSign:
+                               case State_ExponentSign:
                                        if (Char.IsDigit (c)){
-                                               state = State.Exponent;
-                                               goto case State.Exponent;
+                                               state = State_Exponent;
+                                               goto case State_Exponent;
                                        }
 
                                        if (c == positive [0] &&
                                            s.Substring (sidx, positive.Length) == positive){
-                                               state = State.Digits;
+                                               state = State_Digits;
                                                sidx += positive.Length-1;
                                                continue;
                                        }
 
                                        if (c == negative [0] &&
                                            s.Substring (sidx, negative.Length) == negative){
-                                               state = State.Digits;
+                                               state = State_Digits;
                                                b [didx++] = (byte) '-';
                                                sidx += negative.Length-1;
                                                continue;
                                        }
 
                                        if (Char.IsWhiteSpace (c))
-                                               goto case State.ConsumeWhiteSpace;
+                                               goto case State_ConsumeWhiteSpace;
                                        
                                        throw new FormatException ("Unknown char: " + c);
 
-                               case State.Exponent:
+                               case State_Exponent:
                                        if (Char.IsDigit (c)){
                                                b [didx++] = (byte) c;
                                                break;
                                        }
                                        
                                        if (Char.IsWhiteSpace (c))
-                                               goto case State.ConsumeWhiteSpace;
+                                               goto case State_ConsumeWhiteSpace;
                                        throw new FormatException ("Unknown char: " + c);
 
-                               case State.ConsumeWhiteSpace:
+                               case State_ConsumeWhiteSpace:
                                        if (allow_trailing_white && Char.IsWhiteSpace (c))
                                                break;
                                        throw new FormatException ("Unknown char");