| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 class _Double implements double { | 5 class _Double implements double { |
| 6 factory _Double.fromInteger(int value) | 6 factory _Double.fromInteger(int value) |
| 7 native "Double_doubleFromInteger"; | 7 native "Double_doubleFromInteger"; |
| 8 int get hashCode { | 8 int get hashCode { |
| 9 try { | 9 try { |
| 10 return toInt(); | 10 return toInt(); |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 42 } | 42 } |
| 43 double _modulo(double other) native "Double_modulo"; | 43 double _modulo(double other) native "Double_modulo"; |
| 44 | 44 |
| 45 double remainder(num other) { | 45 double remainder(num other) { |
| 46 return _remainder(other.toDouble()); | 46 return _remainder(other.toDouble()); |
| 47 } | 47 } |
| 48 double _remainder(double other) native "Double_remainder"; | 48 double _remainder(double other) native "Double_remainder"; |
| 49 double operator -() { | 49 double operator -() { |
| 50 if (this == 0.0) { | 50 if (this == 0.0) { |
| 51 // -0.0 is canonicalized by the VM's parser, therefore no cycles. | 51 // -0.0 is canonicalized by the VM's parser, therefore no cycles. |
| 52 return isNegative() ? 0.0 : -0.0; | 52 return isNegative ? 0.0 : -0.0; |
| 53 } | 53 } |
| 54 return 0.0 - this; | 54 return 0.0 - this; |
| 55 } | 55 } |
| 56 bool operator ==(other) { | 56 bool operator ==(other) { |
| 57 if (!(other is num)) return false; | 57 if (!(other is num)) return false; |
| 58 return _equal(other.toDouble()); | 58 return _equal(other.toDouble()); |
| 59 } | 59 } |
| 60 bool _equal(double other)native "Double_equal"; | 60 bool _equal(double other)native "Double_equal"; |
| 61 bool _equalToInteger(int other) native "Double_equalToInteger"; | 61 bool _equalToInteger(int other) native "Double_equalToInteger"; |
| 62 bool operator <(num other) { | 62 bool operator <(num other) { |
| (...skipping 23 matching lines...) Expand all Loading... |
| 86 } | 86 } |
| 87 double _moduloFromInteger(int other) { | 87 double _moduloFromInteger(int other) { |
| 88 return new _Double.fromInteger(other) % this; | 88 return new _Double.fromInteger(other) % this; |
| 89 } | 89 } |
| 90 double _remainderFromInteger(int other) { | 90 double _remainderFromInteger(int other) { |
| 91 return new _Double.fromInteger(other).remainder(this); | 91 return new _Double.fromInteger(other).remainder(this); |
| 92 } | 92 } |
| 93 bool _greaterThanFromInteger(int other) | 93 bool _greaterThanFromInteger(int other) |
| 94 native "Double_greaterThanFromInteger"; | 94 native "Double_greaterThanFromInteger"; |
| 95 | 95 |
| 96 bool isNegative() native "Double_isNegative"; | 96 bool get isNegative native "Double_getIsNegative"; |
| 97 bool isInfinite() native "Double_isInfinite"; | 97 bool get isInfinite native "Double_getIsInfinite"; |
| 98 bool isNaN() native "Double_isNaN"; | 98 bool get isNaN native "Double_getIsNaN"; |
| 99 | 99 |
| 100 double abs() { | 100 double abs() { |
| 101 // Handle negative 0.0. | 101 // Handle negative 0.0. |
| 102 if (this == 0.0) return 0.0; | 102 if (this == 0.0) return 0.0; |
| 103 return this < 0.0 ? -this : this; | 103 return this < 0.0 ? -this : this; |
| 104 } | 104 } |
| 105 | 105 |
| 106 double round() native "Double_round"; | 106 double round() native "Double_round"; |
| 107 double floor() native "Double_floor"; | 107 double floor() native "Double_floor"; |
| 108 double ceil () native "Double_ceil"; | 108 double ceil () native "Double_ceil"; |
| 109 double truncate() native "Double_truncate"; | 109 double truncate() native "Double_truncate"; |
| 110 int toInt() native "Double_toInt"; | 110 int toInt() native "Double_toInt"; |
| 111 double toDouble() { return this; } | 111 double toDouble() { return this; } |
| 112 | 112 |
| 113 double pow(num exponent) { | 113 double pow(num exponent) { |
| 114 if (exponent == 0) { | 114 if (exponent == 0) { |
| 115 return 1.0; // ECMA-262 15.8.2.13 | 115 return 1.0; // ECMA-262 15.8.2.13 |
| 116 } | 116 } |
| 117 // Throw NullPointerException if exponent is null. | 117 // Throw NullPointerException if exponent is null. |
| 118 double doubleExponent = exponent.toDouble(); | 118 double doubleExponent = exponent.toDouble(); |
| 119 if (isNaN() || exponent.isNaN()) { | 119 if (isNaN || exponent.isNaN) { |
| 120 return double.NAN; | 120 return double.NAN; |
| 121 } | 121 } |
| 122 return _pow(doubleExponent); | 122 return _pow(doubleExponent); |
| 123 } | 123 } |
| 124 double _pow(double exponent) native "Double_pow"; | 124 double _pow(double exponent) native "Double_pow"; |
| 125 | 125 |
| 126 String toStringAsFixed(int fractionDigits) { | 126 String toStringAsFixed(int fractionDigits) { |
| 127 // See ECMAScript-262, 15.7.4.5 for details. | 127 // See ECMAScript-262, 15.7.4.5 for details. |
| 128 | 128 |
| 129 // Step 2. | 129 // Step 2. |
| 130 if (fractionDigits < 0 || fractionDigits > 20) { | 130 if (fractionDigits < 0 || fractionDigits > 20) { |
| 131 // TODO(antonm): should be proper RangeError or Dart counterpart. | 131 // TODO(antonm): should be proper RangeError or Dart counterpart. |
| 132 throw "Range error"; | 132 throw "Range error"; |
| 133 } | 133 } |
| 134 | 134 |
| 135 // Step 3. | 135 // Step 3. |
| 136 double x = this; | 136 double x = this; |
| 137 | 137 |
| 138 // Step 4. | 138 // Step 4. |
| 139 if (isNaN()) return "NaN"; | 139 if (isNaN) return "NaN"; |
| 140 | 140 |
| 141 // Step 5 and 6 skipped. Will be dealt with by native function. | 141 // Step 5 and 6 skipped. Will be dealt with by native function. |
| 142 | 142 |
| 143 // Step 7. | 143 // Step 7. |
| 144 if (x >= 1e21 || x <= -1e21) { | 144 if (x >= 1e21 || x <= -1e21) { |
| 145 return x.toString(); | 145 return x.toString(); |
| 146 } | 146 } |
| 147 | 147 |
| 148 return _toStringAsFixed(fractionDigits); | 148 return _toStringAsFixed(fractionDigits); |
| 149 } | 149 } |
| 150 String _toStringAsFixed(int fractionDigits) native "Double_toStringAsFixed"; | 150 String _toStringAsFixed(int fractionDigits) native "Double_toStringAsFixed"; |
| 151 | 151 |
| 152 String toStringAsExponential(int fractionDigits) { | 152 String toStringAsExponential(int fractionDigits) { |
| 153 // See ECMAScript-262, 15.7.4.6 for details. | 153 // See ECMAScript-262, 15.7.4.6 for details. |
| 154 | 154 |
| 155 // The EcmaScript specification checks for NaN and Infinity before looking | 155 // The EcmaScript specification checks for NaN and Infinity before looking |
| 156 // at the fractionDigits. In Dart we are consistent with toStringAsFixed and | 156 // at the fractionDigits. In Dart we are consistent with toStringAsFixed and |
| 157 // look at the fractionDigits first. | 157 // look at the fractionDigits first. |
| 158 | 158 |
| 159 // Step 7. | 159 // Step 7. |
| 160 if (fractionDigits !== null && | 160 if (fractionDigits !== null && |
| 161 (fractionDigits < 0 || fractionDigits > 20)) { | 161 (fractionDigits < 0 || fractionDigits > 20)) { |
| 162 // TODO(antonm): should be proper RangeError or Dart counterpart. | 162 // TODO(antonm): should be proper RangeError or Dart counterpart. |
| 163 throw "Range error"; | 163 throw "Range error"; |
| 164 } | 164 } |
| 165 | 165 |
| 166 if (isNaN()) return "NaN"; | 166 if (isNaN) return "NaN"; |
| 167 if (this == double.INFINITY) return "Infinity"; | 167 if (this == double.INFINITY) return "Infinity"; |
| 168 if (this == -double.INFINITY) return "-Infinity"; | 168 if (this == -double.INFINITY) return "-Infinity"; |
| 169 | 169 |
| 170 // The dart function prints the shortest representation when fractionDigits | 170 // The dart function prints the shortest representation when fractionDigits |
| 171 // equals null. The native function wants -1 instead. | 171 // equals null. The native function wants -1 instead. |
| 172 fractionDigits = (fractionDigits === null) ? -1 : fractionDigits; | 172 fractionDigits = (fractionDigits === null) ? -1 : fractionDigits; |
| 173 | 173 |
| 174 return _toStringAsExponential(fractionDigits); | 174 return _toStringAsExponential(fractionDigits); |
| 175 } | 175 } |
| 176 String _toStringAsExponential(int fractionDigits) | 176 String _toStringAsExponential(int fractionDigits) |
| 177 native "Double_toStringAsExponential"; | 177 native "Double_toStringAsExponential"; |
| 178 | 178 |
| 179 String toStringAsPrecision(int precision) { | 179 String toStringAsPrecision(int precision) { |
| 180 // See ECMAScript-262, 15.7.4.7 for details. | 180 // See ECMAScript-262, 15.7.4.7 for details. |
| 181 | 181 |
| 182 // The EcmaScript specification checks for NaN and Infinity before looking | 182 // The EcmaScript specification checks for NaN and Infinity before looking |
| 183 // at the fractionDigits. In Dart we are consistent with toStringAsFixed and | 183 // at the fractionDigits. In Dart we are consistent with toStringAsFixed and |
| 184 // look at the fractionDigits first. | 184 // look at the fractionDigits first. |
| 185 | 185 |
| 186 // Step 8. | 186 // Step 8. |
| 187 if (precision < 1 || precision > 21) { | 187 if (precision < 1 || precision > 21) { |
| 188 // TODO(antonm): should be proper RangeError or Dart counterpart. | 188 // TODO(antonm): should be proper RangeError or Dart counterpart. |
| 189 throw "Range error"; | 189 throw "Range error"; |
| 190 } | 190 } |
| 191 | 191 |
| 192 if (isNaN()) return "NaN"; | 192 if (isNaN) return "NaN"; |
| 193 if (this == double.INFINITY) return "Infinity"; | 193 if (this == double.INFINITY) return "Infinity"; |
| 194 if (this == -double.INFINITY) return "-Infinity"; | 194 if (this == -double.INFINITY) return "-Infinity"; |
| 195 | 195 |
| 196 return _toStringAsPrecision(precision); | 196 return _toStringAsPrecision(precision); |
| 197 } | 197 } |
| 198 String _toStringAsPrecision(int fractionDigits) | 198 String _toStringAsPrecision(int fractionDigits) |
| 199 native "Double_toStringAsPrecision"; | 199 native "Double_toStringAsPrecision"; |
| 200 | 200 |
| 201 String toRadixString(int radix) { | 201 String toRadixString(int radix) { |
| 202 return toInt().toRadixString(radix); | 202 return toInt().toRadixString(radix); |
| 203 } | 203 } |
| 204 | 204 |
| 205 // Order is: NaN > Infinity > ... > 0.0 > -0.0 > ... > -Infinity. | 205 // Order is: NaN > Infinity > ... > 0.0 > -0.0 > ... > -Infinity. |
| 206 int compareTo(Comparable other) { | 206 int compareTo(Comparable other) { |
| 207 final int EQUAL = 0, LESS = -1, GREATER = 1; | 207 final int EQUAL = 0, LESS = -1, GREATER = 1; |
| 208 if (this < other) { | 208 if (this < other) { |
| 209 return LESS; | 209 return LESS; |
| 210 } else if (this > other) { | 210 } else if (this > other) { |
| 211 return GREATER; | 211 return GREATER; |
| 212 } else if (this == other) { | 212 } else if (this == other) { |
| 213 if (this == 0.0) { | 213 if (this == 0.0) { |
| 214 bool thisIsNegative = isNegative(); | 214 bool thisIsNegative = isNegative; |
| 215 bool otherIsNegative = other.isNegative(); | 215 bool otherIsNegative = other.isNegative; |
| 216 if (thisIsNegative == otherIsNegative) { | 216 if (thisIsNegative == otherIsNegative) { |
| 217 return EQUAL; | 217 return EQUAL; |
| 218 } | 218 } |
| 219 return thisIsNegative ? LESS : GREATER; | 219 return thisIsNegative ? LESS : GREATER; |
| 220 } else { | 220 } else { |
| 221 return EQUAL; | 221 return EQUAL; |
| 222 } | 222 } |
| 223 } else if (isNaN()) { | 223 } else if (isNaN) { |
| 224 return other.isNaN() ? EQUAL : GREATER; | 224 return other.isNaN ? EQUAL : GREATER; |
| 225 } else { | 225 } else { |
| 226 // Other is NaN. | 226 // Other is NaN. |
| 227 return LESS; | 227 return LESS; |
| 228 } | 228 } |
| 229 } | 229 } |
| 230 } | 230 } |
| OLD | NEW |