magnitude function
Returns the magnitude of the number.
Implementation
int magnitude(double value) {
// Can't do this with zero because the 10-log of zero doesn't exist.
if (value.compareTo(0.0) == 0) {
return 0;
}
// Note that we need the absolute value of the input because Log10 doesn't
// work for negative numbers (obviously).
double m = log10(value.abs());
var truncated = m.truncate();
// To get the right number we need to know if the value is negative or positive
// truncating a positive number will always give use the correct magnitude
// truncating a negative number will give us a magnitude that is off by 1 (unless integer)
return m < 0 && truncated != m ? truncated - 1 : truncated;
}