Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(71)

Side by Side Diff: runtime/lib/double.dart

Issue 11748016: Make ~/, round, ceil, floor, truncate return ints. Remove toInt. (Closed) Base URL: https://dart.googlecode.com/svn/experimental/lib_v2/dart
Patch Set: Checked mode fixes. Created 7 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 truncate();
11 } on FormatException catch (e) { 11 } on FormatException catch (e) {
12 return 0; 12 return 0;
13 } 13 }
14 } 14 }
15 double operator +(num other) { 15 double operator +(num other) {
16 return _add(other.toDouble()); 16 return _add(other.toDouble());
17 } 17 }
18 double _add(double other) native "Double_add"; 18 double _add(double other) native "Double_add";
19 19
20 double operator -(num other) { 20 double operator -(num other) {
21 return _sub(other.toDouble()); 21 return _sub(other.toDouble());
22 } 22 }
23 double _sub(double other) native "Double_sub"; 23 double _sub(double other) native "Double_sub";
24 24
25 double operator *(num other) { 25 double operator *(num other) {
26 return _mul(other.toDouble()); 26 return _mul(other.toDouble());
27 } 27 }
28 double _mul(double other) native "Double_mul"; 28 double _mul(double other) native "Double_mul";
29 29
30 double operator ~/(num other) { 30 int operator ~/(num other) {
31 return _trunc_div(other.toDouble()); 31 return _trunc_div(other.toDouble());
32 } 32 }
33 double _trunc_div(double other) native "Double_trunc_div"; 33 int _trunc_div(double other) native "Double_trunc_div";
34 34
35 double operator /(num other) { 35 double operator /(num other) {
36 return _div(other.toDouble()); 36 return _div(other.toDouble());
37 } 37 }
38 double _div(double other) native "Double_div"; 38 double _div(double other) native "Double_div";
39 39
40 double operator %(num other) { 40 double operator %(num other) {
41 return _modulo(other.toDouble()); 41 return _modulo(other.toDouble());
42 } 42 }
43 double _modulo(double other) native "Double_modulo"; 43 double _modulo(double other) native "Double_modulo";
(...skipping 30 matching lines...) Expand all
74 } 74 }
75 double _addFromInteger(int other) { 75 double _addFromInteger(int other) {
76 return new _Double.fromInteger(other) + this; 76 return new _Double.fromInteger(other) + this;
77 } 77 }
78 double _subFromInteger(int other) { 78 double _subFromInteger(int other) {
79 return new _Double.fromInteger(other) - this; 79 return new _Double.fromInteger(other) - this;
80 } 80 }
81 double _mulFromInteger(int other) { 81 double _mulFromInteger(int other) {
82 return new _Double.fromInteger(other) * this; 82 return new _Double.fromInteger(other) * this;
83 } 83 }
84 double _truncDivFromInteger(int other) { 84 int _truncDivFromInteger(int other) {
85 return new _Double.fromInteger(other) ~/ this; 85 return new _Double.fromInteger(other) ~/ this;
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 get isNegative native "Double_getIsNegative"; 96 bool get isNegative native "Double_getIsNegative";
97 bool get isInfinite native "Double_getIsInfinite"; 97 bool get isInfinite native "Double_getIsInfinite";
98 bool get isNaN native "Double_getIsNaN"; 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 int round() native "Double_round";
107 double floor() native "Double_floor"; 107 int floor() native "Double_floor";
108 double ceil () native "Double_ceil"; 108 int ceil () native "Double_ceil";
109 double truncate() native "Double_truncate"; 109 int truncate() native "Double_truncate";
110 110
111 num clamp(num lowerLimit, num upperLimit) { 111 num clamp(num lowerLimit, num upperLimit) {
112 if (lowerLimit is! num) throw new ArgumentError(lowerLimit); 112 if (lowerLimit is! num) throw new ArgumentError(lowerLimit);
113 if (upperLimit is! num) throw new ArgumentError(upperLimit); 113 if (upperLimit is! num) throw new ArgumentError(upperLimit);
114 114
115 if (lowerLimit.compareTo(upperLimit) > 0) { 115 if (lowerLimit.compareTo(upperLimit) > 0) {
116 throw new ArgumentError(lowerLimit); 116 throw new ArgumentError(lowerLimit);
117 } 117 }
118 if (lowerLimit.isNaN) return lowerLimit; 118 if (lowerLimit.isNaN) return lowerLimit;
119 if (this.compareTo(lowerLimit) < 0) return lowerLimit; 119 if (this.compareTo(lowerLimit) < 0) return lowerLimit;
120 if (this.compareTo(upperLimit) > 0) return upperLimit; 120 if (this.compareTo(upperLimit) > 0) return upperLimit;
121 return this; 121 return this;
122 } 122 }
123 123
124 int toInt() native "Double_toInt";
125 double toDouble() { return this; } 124 double toDouble() { return this; }
126 125
127 double pow(num exponent) { 126 double pow(num exponent) {
128 if (exponent == 0) { 127 if (exponent == 0) {
129 return 1.0; // ECMA-262 15.8.2.13 128 return 1.0; // ECMA-262 15.8.2.13
130 } 129 }
131 if (exponent is! num) { 130 if (exponent is! num) {
132 throw new ArgumentError(null); 131 throw new ArgumentError(null);
133 } 132 }
134 double doubleExponent = exponent.toDouble(); 133 double doubleExponent = exponent.toDouble();
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after
239 return EQUAL; 238 return EQUAL;
240 } 239 }
241 } else if (isNaN) { 240 } else if (isNaN) {
242 return other.isNaN ? EQUAL : GREATER; 241 return other.isNaN ? EQUAL : GREATER;
243 } else { 242 } else {
244 // Other is NaN. 243 // Other is NaN.
245 return LESS; 244 return LESS;
246 } 245 }
247 } 246 }
248 } 247 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698