numbersBetween function
Evaluates the count of numbers between two double numbers
The second number is included in the number, thus two equal numbers evaluate
to zero and two neighbor numbers evaluate to one. Therefore, what is returned
is actually the count of numbers between plus 1. Throws ArgumentError
if a
is double.infinity
or double.negativeInfinity
. Throws ArgumentError
if a
is double.nan
. Throws ArgumentError if b
is double.infinity
or double.negativeInfinity
. Throws ArgumentError if b
is double.nan
.
Implementation
int numbersBetween(double a, double b) {
if (a.isNaN || a.isInfinite) {
throw ArgumentError.value(a, 'a', 'Value can\'t be NaN or infinity');
}
if (b.isNaN || b.isInfinite) {
throw ArgumentError.value(b, 'b', 'Value can\'t be NaN or infinity');
}
// Calculate the ulps for the maximum and minimum values
// Note that these can overflow
int intA = asDirectionalInt64(a);
int intB = asDirectionalInt64(b);
// Now find the number of values between the two doubles. This should not overflow
// given that there are more long values than there are double values
return (a >= b) ? (intA - intB) : (intB - intA);
}