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

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

Issue 2005723004: Fraction class prototype and test (not to be committed). (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: work in progress Created 4 years, 5 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
« no previous file with comments | « runtime/lib/double.cc ('k') | runtime/lib/fraction.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 8
9 factory _Double.fromFraction(fraction value) {
10 return _DoubleFromFraction(value._numerator, value._denominator);
11 }
12 static _Double _DoubleFromFraction(int numerator, int denominator)
13 native "Double_doubleFromFraction";
14
9 Type get runtimeType => double; 15 Type get runtimeType => double;
10 16
11 // TODO: Make a stared static method for hashCode and _identityHashCode 17 // TODO: Make a stared static method for hashCode and _identityHashCode
12 // when semantics are corrected as described in: 18 // when semantics are corrected as described in:
13 // https://github.com/dart-lang/sdk/issues/2884 19 // https://github.com/dart-lang/sdk/issues/2884
14 int get hashCode => (isNaN || isInfinite) ? 0 : toInt(); 20 int get hashCode => (isNaN || isInfinite) ? 0 : toInt();
15 int get _identityHashCode => (isNaN || isInfinite) ? 0 : toInt(); 21 int get _identityHashCode => (isNaN || isInfinite) ? 0 : toInt();
16 22
17 double operator +(num other) { 23 double operator +(num other) {
18 return _add(other.toDouble()); 24 return _add(other.toDouble());
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
50 double _remainder(double other) native "Double_remainder"; 56 double _remainder(double other) native "Double_remainder";
51 57
52 double operator -() native "Double_flipSignBit"; 58 double operator -() native "Double_flipSignBit";
53 59
54 bool operator ==(other) { 60 bool operator ==(other) {
55 if (!(other is num)) return false; 61 if (!(other is num)) return false;
56 return _equal(other.toDouble()); 62 return _equal(other.toDouble());
57 } 63 }
58 bool _equal(double other) native "Double_equal"; 64 bool _equal(double other) native "Double_equal";
59 bool _equalToInteger(int other) native "Double_equalToInteger"; 65 bool _equalToInteger(int other) native "Double_equalToInteger";
66 bool _equalToFraction(fraction other) => _equal(other.toDouble());
60 bool operator <(num other) { 67 bool operator <(num other) {
61 return other > this; 68 return other > this;
62 } 69 }
63 bool operator >(num other) { 70 bool operator >(num other) {
64 return _greaterThan(other.toDouble()); 71 return _greaterThan(other.toDouble());
65 } 72 }
66 bool _greaterThan(double other) native "Double_greaterThan"; 73 bool _greaterThan(double other) native "Double_greaterThan";
67 bool operator >=(num other) { 74 bool operator >=(num other) {
68 return (this == other) || (this > other); 75 return (this == other) || (this > other);
69 } 76 }
(...skipping 14 matching lines...) Expand all
84 } 91 }
85 double _moduloFromInteger(int other) { 92 double _moduloFromInteger(int other) {
86 return new _Double.fromInteger(other)._modulo(this); 93 return new _Double.fromInteger(other)._modulo(this);
87 } 94 }
88 double _remainderFromInteger(int other) { 95 double _remainderFromInteger(int other) {
89 return new _Double.fromInteger(other)._remainder(this); 96 return new _Double.fromInteger(other)._remainder(this);
90 } 97 }
91 bool _greaterThanFromInteger(int other) 98 bool _greaterThanFromInteger(int other)
92 native "Double_greaterThanFromInteger"; 99 native "Double_greaterThanFromInteger";
93 100
101 double _addFromFraction(fraction other) {
102 return new _Double.fromFraction(other)._add(this);
103 }
104 double _subFromFraction(fraction other) {
105 return new _Double.fromFraction(other)._sub(this);
106 }
107 double _mulFromFraction(fraction other) {
108 return new _Double.fromFraction(other)._mul(this);
109 }
110 double _divFromFraction(fraction other) {
111 return new _Double.fromFraction(other)._div(this);
112 }
113 int _truncDivFromFraction(fraction other) {
114 return new _Double.fromFraction(other)._trunc_div(this);
115 }
116 double _moduloFromFraction(fraction other) {
117 return new _Double.fromFraction(other)._modulo(this);
118 }
119 double _remainderFromFraction(fraction other) {
120 return new _Double.fromFraction(other)._remainder(this);
121 }
122 bool _greaterThanFromFraction(fraction other) {
123 return new _Double.fromFraction(other)._greaterThan(this);
124 }
125
94 bool get isNegative native "Double_getIsNegative"; 126 bool get isNegative native "Double_getIsNegative";
95 bool get isInfinite native "Double_getIsInfinite"; 127 bool get isInfinite native "Double_getIsInfinite";
96 bool get isNaN native "Double_getIsNaN"; 128 bool get isNaN native "Double_getIsNaN";
97 bool get isFinite => !isInfinite && !isNaN; // Can be optimized. 129 bool get isFinite => !isInfinite && !isNaN; // Can be optimized.
98 130
99 double abs() { 131 double abs() {
100 // Handle negative 0.0. 132 // Handle negative 0.0.
101 if (this == 0.0) return 0.0; 133 if (this == 0.0) return 0.0;
102 return this < 0.0 ? -this : this; 134 return this < 0.0 ? -this : this;
103 } 135 }
(...skipping 25 matching lines...) Expand all
129 if (lowerLimit.compareTo(upperLimit) > 0) { 161 if (lowerLimit.compareTo(upperLimit) > 0) {
130 throw new ArgumentError(lowerLimit); 162 throw new ArgumentError(lowerLimit);
131 } 163 }
132 if (lowerLimit.isNaN) return lowerLimit; 164 if (lowerLimit.isNaN) return lowerLimit;
133 if (this.compareTo(lowerLimit) < 0) return lowerLimit; 165 if (this.compareTo(lowerLimit) < 0) return lowerLimit;
134 if (this.compareTo(upperLimit) > 0) return upperLimit; 166 if (this.compareTo(upperLimit) > 0) return upperLimit;
135 return this; 167 return this;
136 } 168 }
137 169
138 int toInt() native "Double_toInt"; 170 int toInt() native "Double_toInt";
139 num _toBigintOrDouble() { return this; } 171 num _toBigintIfInteger() { return this; }
172 num _toDoubleOrFraction() { return this; }
140 double toDouble() { return this; } 173 double toDouble() { return this; }
141 174
175 fraction toFraction() {
176 var fract = new List(2);
177 _DoubleToFraction(fract);
178 return new _Fraction(fract[0], fract[1]);
179 }
180 void _DoubleToFraction(List fract) native "Double_toFraction";
181
182 fraction toPercent() {
183 return new _Fraction((this*100).toInt(), 100);
184 }
185
142 static const int CACHE_SIZE_LOG2 = 3; 186 static const int CACHE_SIZE_LOG2 = 3;
143 static const int CACHE_LENGTH = 1 << (CACHE_SIZE_LOG2 + 1); 187 static const int CACHE_LENGTH = 1 << (CACHE_SIZE_LOG2 + 1);
144 static const int CACHE_MASK = CACHE_LENGTH - 1; 188 static const int CACHE_MASK = CACHE_LENGTH - 1;
145 // Each key (double) followed by its toString result. 189 // Each key (double) followed by its toString result.
146 static final List _cache = new List(CACHE_LENGTH); 190 static final List _cache = new List(CACHE_LENGTH);
147 static int _cacheEvictIndex = 0; 191 static int _cacheEvictIndex = 0;
148 192
149 String _toString() native "Double_toString"; 193 String _toString() native "Double_toString";
150 194
151 String toString() { 195 String toString() {
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after
271 return EQUAL; 315 return EQUAL;
272 } 316 }
273 } else if (isNaN) { 317 } else if (isNaN) {
274 return other.isNaN ? EQUAL : GREATER; 318 return other.isNaN ? EQUAL : GREATER;
275 } else { 319 } else {
276 // Other is NaN. 320 // Other is NaN.
277 return LESS; 321 return LESS;
278 } 322 }
279 } 323 }
280 } 324 }
OLDNEW
« no previous file with comments | « runtime/lib/double.cc ('k') | runtime/lib/fraction.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698