OLD | NEW |
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, 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 hashCode() { | 8 int hashCode() { |
9 try { | 9 try { |
10 return toInt(); | 10 return toInt(); |
11 } catch (BadNumberFormatException e) { | 11 } catch (BadNumberFormatException e) { |
12 return 0; | 12 return 0; |
13 } | 13 } |
14 } | 14 } |
| 15 |
| 16 // Verified double conversion. |
| 17 static double _checkedToDouble(num value) { |
| 18 if (value is !num) { |
| 19 throw const IllegalArgumentException("num value expected"); |
| 20 } |
| 21 return value.toDouble(); |
| 22 } |
| 23 |
15 double operator +(num other) { | 24 double operator +(num other) { |
16 return add_(other.toDouble()); | 25 return add_(_checkedToDouble(other)); |
17 } | 26 } |
18 double add_(double other) native "Double_add"; | 27 double add_(double other) native "Double_add"; |
19 | 28 |
20 double operator -(num other) { | 29 double operator -(num other) { |
21 return sub_(other.toDouble()); | 30 return sub_(_checkedToDouble(other)); |
22 } | 31 } |
23 double sub_(double other) native "Double_sub"; | 32 double sub_(double other) native "Double_sub"; |
24 | 33 |
25 double operator *(num other) { | 34 double operator *(num other) { |
26 return mul_(other.toDouble()); | 35 return mul_(_checkedToDouble(other)); |
27 } | 36 } |
28 double mul_(double other) native "Double_mul"; | 37 double mul_(double other) native "Double_mul"; |
29 | 38 |
30 double operator ~/(num other) { | 39 double operator ~/(num other) { |
31 return trunc_div_(other.toDouble()); | 40 return trunc_div_(_checkedToDouble(other)); |
32 } | 41 } |
33 double trunc_div_(double other) native "Double_trunc_div"; | 42 double trunc_div_(double other) native "Double_trunc_div"; |
34 | 43 |
35 double operator /(num other) { | 44 double operator /(num other) { |
36 return div_(other.toDouble()); | 45 return div_(_checkedToDouble(other)); |
37 } | 46 } |
38 double div_(double other) native "Double_div"; | 47 double div_(double other) native "Double_div"; |
39 | 48 |
40 double operator %(num other) { | 49 double operator %(num other) { |
41 return modulo_(other.toDouble()); | 50 return modulo_(_checkedToDouble(other)); |
42 } | 51 } |
43 double modulo_(double other) native "Double_modulo"; | 52 double modulo_(double other) native "Double_modulo"; |
44 | 53 |
45 double remainder(num other) { | 54 double remainder(num other) { |
46 return remainder_(other.toDouble()); | 55 return remainder_(_checkedToDouble(other)); |
47 } | 56 } |
48 double remainder_(double other) native "Double_remainder"; | 57 double remainder_(double other) native "Double_remainder"; |
49 | 58 |
50 double operator negate() { | 59 double operator negate() { |
51 return 0.0 - this; | 60 return 0.0 - this; |
52 } | 61 } |
53 bool operator ==(other) { | 62 bool operator ==(other) { |
54 if (!(other is num)) return false; | 63 if (!(other is num)) return false; |
55 return equal_(other.toDouble()); | 64 return equal_(_checkedToDouble(other)); |
56 } | 65 } |
57 bool equal_(double other)native "Double_equal"; | 66 bool equal_(double other)native "Double_equal"; |
58 bool equalToInteger(int other) native "Double_equalToInteger"; | 67 bool equalToInteger(int other) native "Double_equalToInteger"; |
59 bool operator <(num other) { | 68 bool operator <(num other) { |
60 return other > this; | 69 return other > this; |
61 } | 70 } |
62 bool operator >(num other) { | 71 bool operator >(num other) { |
63 return greaterThan_(other.toDouble()); | 72 return greaterThan_(_checkedToDouble(other)); |
64 } | 73 } |
65 bool greaterThan_(double other) native "Double_greaterThan"; | 74 bool greaterThan_(double other) native "Double_greaterThan"; |
66 bool operator >=(num other) { | 75 bool operator >=(num other) { |
67 return (this == other) || (this > other); | 76 return (this == other) || (this > other); |
68 } | 77 } |
69 bool operator <=(num other) { | 78 bool operator <=(num other) { |
70 return (this == other) || (this < other); | 79 return (this == other) || (this < other); |
71 } | 80 } |
72 double addFromInteger(int other) { | 81 double addFromInteger(int other) { |
73 return new Double.fromInteger(other) + this; | 82 return new Double.fromInteger(other) + this; |
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
191 } | 200 } |
192 String toRadixString(int radix) { | 201 String toRadixString(int radix) { |
193 throw "Double.toRadixString unimplemented."; | 202 throw "Double.toRadixString unimplemented."; |
194 } | 203 } |
195 int compareTo(Comparable other) { | 204 int compareTo(Comparable other) { |
196 if (this == other) return 0; | 205 if (this == other) return 0; |
197 if (this < other) return -1; | 206 if (this < other) return -1; |
198 return 1; | 207 return 1; |
199 } | 208 } |
200 } | 209 } |
OLD | NEW |