utils.dart 805 Bytes
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

Ian Hickson's avatar
Ian Hickson committed
5 6
/// Whether two doubles are within a given distance of each other.
///
7 8 9
/// The `epsilon` argument must be positive and not null.
/// The `a` and `b` arguments may be null. A null value is only considered
/// near-equal to another null value.
10
bool nearEqual(double? a, double? b, double epsilon) {
11
  assert(epsilon != null);
12
  assert(epsilon >= 0.0);
13 14
  if (a == null || b == null)
    return a == b;
15
  return (a > (b - epsilon)) && (a < (b + epsilon)) || a == b;
16
}
17

Ian Hickson's avatar
Ian Hickson committed
18 19 20
/// Whether a double is within a given distance of zero.
///
/// The epsilon argument must be positive.
21
bool nearZero(double a, double epsilon) => nearEqual(a, 0.0, epsilon);