Skip to content

Commit

Permalink
Add approximation comparision
Browse files Browse the repository at this point in the history
  • Loading branch information
jim-ec committed May 21, 2019
1 parent 5533142 commit 0d87fc5
Showing 1 changed file with 19 additions and 5 deletions.
24 changes: 19 additions & 5 deletions lib/src/angles_base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -111,19 +111,33 @@ class Angle implements Comparable<Angle> {
@override
String toString() => "${degrees.toStringAsFixed(1)}°";

@override
int compareTo(Angle other) => this == other ? 0 : this > other ? 1 : -1;

@override
int get hashCode => _storage.hashCode;

/// Compares this to [other].
bool operator <(final Angle other) => _storage < other._storage;

/// Compares this to [other].
bool operator >(final Angle other) => _storage > other._storage;

@override
int compareTo(Angle other) => this == other ? 0 : this > other ? 1 : -1;
/// Compares this to [other].
bool operator <=(final Angle other) => _storage <= other._storage;

@override
int get hashCode => _storage.hashCode;
/// Compares this to [other].
bool operator >=(final Angle other) => _storage >= other._storage;

/// Compares to another angle.
/// Since IEEE 754 floating points are approximations to real numbers, they cannot
/// be really tested for equality.
@override
bool operator ==(covariant Angle rhs) => _storage == rhs._storage;
bool operator ==(covariant final Angle rhs) => _storage == rhs._storage;

/// Checks if [rhs] is approximately this angle, +/- [range].
bool approximately(final Angle rhs, final double range) {
return rhs._storage >= _storage - range && rhs._storage <= _storage + range;
}

}

0 comments on commit 0d87fc5

Please sign in to comment.