| OLD | NEW |
| 1 // Copyright (c) 2015, the Dartino project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, the Dartino 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.md file. | 3 // BSD-style license that can be found in the LICENSE.md file. |
| 4 | 4 |
| 5 part of dart.core_patch; | 5 part of dart.core_patch; |
| 6 | 6 |
| 7 class _DoubleImpl implements double { | 7 class _DoubleImpl implements double { |
| 8 int get hashCode => identityHashCode(this); | 8 int get hashCode => identityHashCode(this); |
| 9 | 9 |
| 10 @fletch.native external num operator -(); | 10 @dartino.native external num operator -(); |
| 11 | 11 |
| 12 int compareTo(num other) { | 12 int compareTo(num other) { |
| 13 if (this < other) return -1; | 13 if (this < other) return -1; |
| 14 if (this > other) return 1; | 14 if (this > other) return 1; |
| 15 if (this == other) { | 15 if (this == other) { |
| 16 if (this == 0.0) { | 16 if (this == 0.0) { |
| 17 var negative = isNegative; | 17 var negative = isNegative; |
| 18 if (negative == other.isNegative) return 0; | 18 if (negative == other.isNegative) return 0; |
| 19 return negative ? -1 : 1; | 19 return negative ? -1 : 1; |
| 20 } | 20 } |
| 21 return 0; | 21 return 0; |
| 22 } | 22 } |
| 23 if (isNaN) return other.isNaN ? 0 : 1; | 23 if (isNaN) return other.isNaN ? 0 : 1; |
| 24 return -1; | 24 return -1; |
| 25 } | 25 } |
| 26 | 26 |
| 27 @fletch.native num operator +(other) { | 27 @dartino.native num operator +(other) { |
| 28 // TODO(kasperl): Check error. | 28 // TODO(kasperl): Check error. |
| 29 return other._addFromDouble(this); | 29 return other._addFromDouble(this); |
| 30 } | 30 } |
| 31 | 31 |
| 32 @fletch.native num operator -(other) { | 32 @dartino.native num operator -(other) { |
| 33 // TODO(kasperl): Check error. | 33 // TODO(kasperl): Check error. |
| 34 return other._subFromDouble(this); | 34 return other._subFromDouble(this); |
| 35 } | 35 } |
| 36 | 36 |
| 37 @fletch.native num operator *(other) { | 37 @dartino.native num operator *(other) { |
| 38 // TODO(kasperl): Check error. | 38 // TODO(kasperl): Check error. |
| 39 return other._mulFromDouble(this); | 39 return other._mulFromDouble(this); |
| 40 } | 40 } |
| 41 | 41 |
| 42 @fletch.native num operator %(other) { | 42 @dartino.native num operator %(other) { |
| 43 // TODO(kasperl): Check error. | 43 // TODO(kasperl): Check error. |
| 44 return other._modFromDouble(this); | 44 return other._modFromDouble(this); |
| 45 } | 45 } |
| 46 | 46 |
| 47 @fletch.native num operator /(other) { | 47 @dartino.native num operator /(other) { |
| 48 // TODO(kasperl): Check error. | 48 // TODO(kasperl): Check error. |
| 49 return other._divFromDouble(this); | 49 return other._divFromDouble(this); |
| 50 } | 50 } |
| 51 | 51 |
| 52 @fletch.native num operator ~/(other) { | 52 @dartino.native num operator ~/(other) { |
| 53 switch (fletch.nativeError) { | 53 switch (dartino.nativeError) { |
| 54 case fletch.wrongArgumentType: | 54 case dartino.wrongArgumentType: |
| 55 return other._truncDivFromDouble(this); | 55 return other._truncDivFromDouble(this); |
| 56 case fletch.indexOutOfBounds: | 56 case dartino.indexOutOfBounds: |
| 57 throw new UnsupportedError("double.~/ $this"); | 57 throw new UnsupportedError("double.~/ $this"); |
| 58 } | 58 } |
| 59 } | 59 } |
| 60 | 60 |
| 61 @fletch.native bool operator ==(other) { | 61 @dartino.native bool operator ==(other) { |
| 62 if (other is! num) return false; | 62 if (other is! num) return false; |
| 63 // TODO(kasperl): Check error. | 63 // TODO(kasperl): Check error. |
| 64 return other._compareEqFromDouble(this); | 64 return other._compareEqFromDouble(this); |
| 65 } | 65 } |
| 66 | 66 |
| 67 @fletch.native bool operator <(other) { | 67 @dartino.native bool operator <(other) { |
| 68 // TODO(kasperl): Check error. | 68 // TODO(kasperl): Check error. |
| 69 return other._compareLtFromDouble(this); | 69 return other._compareLtFromDouble(this); |
| 70 } | 70 } |
| 71 | 71 |
| 72 @fletch.native bool operator <=(other) { | 72 @dartino.native bool operator <=(other) { |
| 73 // TODO(kasperl): Check error. | 73 // TODO(kasperl): Check error. |
| 74 return other._compareLeFromDouble(this); | 74 return other._compareLeFromDouble(this); |
| 75 } | 75 } |
| 76 | 76 |
| 77 @fletch.native bool operator >(other) { | 77 @dartino.native bool operator >(other) { |
| 78 // TODO(kasperl): Check error. | 78 // TODO(kasperl): Check error. |
| 79 return other._compareGtFromDouble(this); | 79 return other._compareGtFromDouble(this); |
| 80 } | 80 } |
| 81 | 81 |
| 82 @fletch.native bool operator >=(other) { | 82 @dartino.native bool operator >=(other) { |
| 83 // TODO(kasperl): Check error. | 83 // TODO(kasperl): Check error. |
| 84 return other._compareGeFromDouble(this); | 84 return other._compareGeFromDouble(this); |
| 85 } | 85 } |
| 86 | 86 |
| 87 double abs() { | 87 double abs() { |
| 88 if (this == 0.0) return 0.0; // -0.0 -> 0.0 | 88 if (this == 0.0) return 0.0; // -0.0 -> 0.0 |
| 89 return (this < 0.0) ? -this : this; | 89 return (this < 0.0) ? -this : this; |
| 90 } | 90 } |
| 91 | 91 |
| 92 @fletch.native double remainder(other) { | 92 @dartino.native double remainder(other) { |
| 93 return other._remainderFromDouble(this); | 93 return other._remainderFromDouble(this); |
| 94 } | 94 } |
| 95 | 95 |
| 96 @fletch.native int round() { | 96 @dartino.native int round() { |
| 97 throw new UnsupportedError("double.round $this"); | 97 throw new UnsupportedError("double.round $this"); |
| 98 } | 98 } |
| 99 | 99 |
| 100 @fletch.native int floor() { | 100 @dartino.native int floor() { |
| 101 throw new UnsupportedError("double.floor $this"); | 101 throw new UnsupportedError("double.floor $this"); |
| 102 } | 102 } |
| 103 | 103 |
| 104 @fletch.native int ceil() { | 104 @dartino.native int ceil() { |
| 105 throw new UnsupportedError("double.ceil $this"); | 105 throw new UnsupportedError("double.ceil $this"); |
| 106 } | 106 } |
| 107 | 107 |
| 108 @fletch.native int truncate() { | 108 @dartino.native int truncate() { |
| 109 throw new UnsupportedError("double.truncate $this"); | 109 throw new UnsupportedError("double.truncate $this"); |
| 110 } | 110 } |
| 111 | 111 |
| 112 @fletch.native external double roundToDouble(); | 112 @dartino.native external double roundToDouble(); |
| 113 @fletch.native external double floorToDouble(); | 113 @dartino.native external double floorToDouble(); |
| 114 @fletch.native external double ceilToDouble(); | 114 @dartino.native external double ceilToDouble(); |
| 115 @fletch.native external double truncateToDouble(); | 115 @dartino.native external double truncateToDouble(); |
| 116 | 116 |
| 117 @fletch.native external bool get isNaN; | 117 @dartino.native external bool get isNaN; |
| 118 @fletch.native external bool get isNegative; | 118 @dartino.native external bool get isNegative; |
| 119 | 119 |
| 120 bool get isFinite { | 120 bool get isFinite { |
| 121 return this != double.INFINITY && | 121 return this != double.INFINITY && |
| 122 this != -double.INFINITY && | 122 this != -double.INFINITY && |
| 123 !isNaN; | 123 !isNaN; |
| 124 } | 124 } |
| 125 | 125 |
| 126 bool get isInfinite { | 126 bool get isInfinite { |
| 127 return (this == double.INFINITY || | 127 return (this == double.INFINITY || |
| 128 this == -double.INFINITY) && !isNaN; | 128 this == -double.INFINITY) && !isNaN; |
| 129 } | 129 } |
| 130 | 130 |
| 131 double get sign { | 131 double get sign { |
| 132 if (this > 0.0) return 1.0; | 132 if (this > 0.0) return 1.0; |
| 133 if (this < 0.0) return -1.0; | 133 if (this < 0.0) return -1.0; |
| 134 return this; | 134 return this; |
| 135 } | 135 } |
| 136 | 136 |
| 137 num clamp(num lowerLimit, num upperLimit) { | 137 num clamp(num lowerLimit, num upperLimit) { |
| 138 throw new UnimplementedError("double.clamp"); | 138 throw new UnimplementedError("double.clamp"); |
| 139 } | 139 } |
| 140 | 140 |
| 141 double toDouble() => this; | 141 double toDouble() => this; |
| 142 | 142 |
| 143 int toInt() => truncate(); | 143 int toInt() => truncate(); |
| 144 | 144 |
| 145 num _toBigintOrDouble() => this; | 145 num _toBigintOrDouble() => this; |
| 146 | 146 |
| 147 @fletch.native external String toString(); | 147 @dartino.native external String toString(); |
| 148 | 148 |
| 149 String toStringAsExponential([int digits]) { | 149 String toStringAsExponential([int digits]) { |
| 150 if (digits == null) { | 150 if (digits == null) { |
| 151 digits = -1; | 151 digits = -1; |
| 152 } else { | 152 } else { |
| 153 if (digits is! int) throw new ArgumentError(); | 153 if (digits is! int) throw new ArgumentError(); |
| 154 if (digits < 0 || digits > 20) throw new RangeError.range(digits, 0, 20); | 154 if (digits < 0 || digits > 20) throw new RangeError.range(digits, 0, 20); |
| 155 } | 155 } |
| 156 if (isNaN) return "NaN"; | 156 if (isNaN) return "NaN"; |
| 157 if (this == double.INFINITY) return "Infinity"; | 157 if (this == double.INFINITY) return "Infinity"; |
| (...skipping 12 matching lines...) Expand all Loading... |
| 170 | 170 |
| 171 String toStringAsPrecision(int digits) { | 171 String toStringAsPrecision(int digits) { |
| 172 if (digits is! int) throw new ArgumentError(); | 172 if (digits is! int) throw new ArgumentError(); |
| 173 if (digits < 1 || digits > 21) throw new RangeError.range(digits, 1, 21); | 173 if (digits < 1 || digits > 21) throw new RangeError.range(digits, 1, 21); |
| 174 if (isNaN) return "NaN"; | 174 if (isNaN) return "NaN"; |
| 175 if (this == double.INFINITY) return "Infinity"; | 175 if (this == double.INFINITY) return "Infinity"; |
| 176 if (this == -double.INFINITY) return "-Infinity"; | 176 if (this == -double.INFINITY) return "-Infinity"; |
| 177 return _toStringAsPrecision(digits); | 177 return _toStringAsPrecision(digits); |
| 178 } | 178 } |
| 179 | 179 |
| 180 @fletch.native external String _toStringAsExponential(int digits); | 180 @dartino.native external String _toStringAsExponential(int digits); |
| 181 @fletch.native external String _toStringAsFixed(int digits); | 181 @dartino.native external String _toStringAsFixed(int digits); |
| 182 @fletch.native external String _toStringAsPrecision(int digits); | 182 @dartino.native external String _toStringAsPrecision(int digits); |
| 183 | 183 |
| 184 double _addFromInteger(int other) => other.toDouble() + this; | 184 double _addFromInteger(int other) => other.toDouble() + this; |
| 185 | 185 |
| 186 double _subFromInteger(int other) => other.toDouble() - this; | 186 double _subFromInteger(int other) => other.toDouble() - this; |
| 187 | 187 |
| 188 double _mulFromInteger(int other) => other.toDouble() * this; | 188 double _mulFromInteger(int other) => other.toDouble() * this; |
| 189 | 189 |
| 190 double _modFromInteger(int other) => other.toDouble() % this; | 190 double _modFromInteger(int other) => other.toDouble() % this; |
| 191 | 191 |
| 192 double _divFromInteger(int other) => other.toDouble() / this; | 192 double _divFromInteger(int other) => other.toDouble() / this; |
| 193 | 193 |
| 194 int _truncDivFromInteger(int other) => other.toDouble() ~/ this; | 194 int _truncDivFromInteger(int other) => other.toDouble() ~/ this; |
| 195 | 195 |
| 196 bool _compareEqFromInteger(int other) => other.toDouble() == this; | 196 bool _compareEqFromInteger(int other) => other.toDouble() == this; |
| 197 | 197 |
| 198 bool _compareLtFromInteger(int other) => other.toDouble() < this; | 198 bool _compareLtFromInteger(int other) => other.toDouble() < this; |
| 199 | 199 |
| 200 bool _compareLeFromInteger(int other) => other.toDouble() <= this; | 200 bool _compareLeFromInteger(int other) => other.toDouble() <= this; |
| 201 | 201 |
| 202 bool _compareGtFromInteger(int other) => other.toDouble() > this; | 202 bool _compareGtFromInteger(int other) => other.toDouble() > this; |
| 203 | 203 |
| 204 bool _compareGeFromInteger(int other) => other.toDouble() >= this; | 204 bool _compareGeFromInteger(int other) => other.toDouble() >= this; |
| 205 } | 205 } |
| OLD | NEW |