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

Side by Side Diff: runtime/lib/integers.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/fraction.dart ('k') | runtime/lib/mirrors.cc » ('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 abstract class _IntegerImplementation { 5 abstract class _IntegerImplementation {
6 // The Dart class _Bigint extending _IntegerImplementation requires a 6 // The Dart class _Bigint extending _IntegerImplementation requires a
7 // default constructor. 7 // default constructor.
8 8
9 Type get runtimeType => int; 9 Type get runtimeType => int;
10 10
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
85 if (result != null) return result; 85 if (result != null) return result;
86 return other._toBigint()._shlFromInt(this); 86 return other._toBigint()._shlFromInt(this);
87 } 87 }
88 bool operator <(num other) { 88 bool operator <(num other) {
89 return other > this; 89 return other > this;
90 } 90 }
91 bool operator >(num other) { 91 bool operator >(num other) {
92 return other._greaterThanFromInteger(this); 92 return other._greaterThanFromInteger(this);
93 } 93 }
94 bool operator >=(num other) { 94 bool operator >=(num other) {
95 return (this == other) || (this > other); 95 return (this == other) || (this > other);
96 } 96 }
97 bool operator <=(num other) { 97 bool operator <=(num other) {
98 return (this == other) || (this < other); 98 return (this == other) || (this < other);
99 } 99 }
100 bool _greaterThanFromInteger(int other) 100 bool _greaterThanFromInteger(int other)
101 native "Integer_greaterThanFromInteger"; 101 native "Integer_greaterThanFromInteger";
102 bool _greaterThanFromFraction(fraction other) {
103 return other._numerator > (other._denominator * this);
104 }
102 bool operator ==(other) { 105 bool operator ==(other) {
103 if (other is num) { 106 if (other is num) {
104 return other._equalToInteger(this); 107 return other._equalToInteger(this);
105 } 108 }
106 return false; 109 return false;
107 } 110 }
108 bool _equalToInteger(int other) native "Integer_equalToInteger"; 111 bool _equalToInteger(int other) native "Integer_equalToInteger";
112 bool _equalToFraction(fraction other) {
113 return (this * other._denominator) == other._numerator;
114 }
109 int abs() { 115 int abs() {
110 return this < 0 ? -this : this; 116 return this < 0 ? -this : this;
111 } 117 }
112 int get sign { 118 int get sign {
113 return (this > 0) ? 1 : (this < 0) ? -1 : 0; 119 return (this > 0) ? 1 : (this < 0) ? -1 : 0;
114 } 120 }
115 bool get isEven => ((this & 1) == 0); 121 bool get isEven => ((this & 1) == 0);
116 bool get isOdd => !isEven; 122 bool get isOdd => !isEven;
117 bool get isNaN => false; 123 bool get isNaN => false;
118 bool get isNegative => this < 0; 124 bool get isNegative => this < 0;
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
193 } 199 }
194 if (lowerLimit.isNaN) return lowerLimit; 200 if (lowerLimit.isNaN) return lowerLimit;
195 // Note that we don't need to care for -0.0 for the lower limit. 201 // Note that we don't need to care for -0.0 for the lower limit.
196 if (this < lowerLimit) return lowerLimit; 202 if (this < lowerLimit) return lowerLimit;
197 if (this.compareTo(upperLimit) > 0) return upperLimit; 203 if (this.compareTo(upperLimit) > 0) return upperLimit;
198 return this; 204 return this;
199 } 205 }
200 206
201 int toInt() { return this; } 207 int toInt() { return this; }
202 double toDouble() { return new _Double.fromInteger(this); } 208 double toDouble() { return new _Double.fromInteger(this); }
209 fraction toFraction() { return new _Fraction.fromInteger(this); }
210 fraction toPercent() { return new _Fraction._percent(this*100); }
203 _Bigint _toBigint() { return new _Bigint._fromInt(this); } 211 _Bigint _toBigint() { return new _Bigint._fromInt(this); }
204 num _toBigintOrDouble() { return _toBigint(); } 212 num _toBigintIfInteger() { return _toBigint(); }
213 num _toDoubleOrFraction() { return new _Fraction.fromInteger(this); }
205 214
206 String toStringAsFixed(int fractionDigits) { 215 String toStringAsFixed(int fractionDigits) {
207 return this.toDouble().toStringAsFixed(fractionDigits); 216 return this.toDouble().toStringAsFixed(fractionDigits);
208 } 217 }
209 String toStringAsExponential([int fractionDigits]) { 218 String toStringAsExponential([int fractionDigits]) {
210 return this.toDouble().toStringAsExponential(fractionDigits); 219 return this.toDouble().toStringAsExponential(fractionDigits);
211 } 220 }
212 String toStringAsPrecision(int precision) { 221 String toStringAsPrecision(int precision) {
213 return this.toDouble().toStringAsPrecision(precision); 222 return this.toDouble().toStringAsPrecision(precision);
214 } 223 }
(...skipping 403 matching lines...) Expand 10 before | Expand all | Expand 10 after
618 // Shift by mint exceeds range that can be handled by the VM. 627 // Shift by mint exceeds range that can be handled by the VM.
619 int _shrFromInt(int other) { 628 int _shrFromInt(int other) {
620 if (other < 0) { 629 if (other < 0) {
621 return -1; 630 return -1;
622 } else { 631 } else {
623 return 0; 632 return 0;
624 } 633 }
625 } 634 }
626 int _shlFromInt(int other) native "Mint_shlFromInt"; 635 int _shlFromInt(int other) native "Mint_shlFromInt";
627 } 636 }
OLDNEW
« no previous file with comments | « runtime/lib/fraction.dart ('k') | runtime/lib/mirrors.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698