simplifyType static method
Returns the Number equivalent to n
having the simplest type that can
represent the value (in order): Integer, Double, Imaginary, or Complex.
If n
is already the most simple type possible, then it will be returned directly.
Precise Numbers always remain Precise.
Implementation
static Number simplifyType(Number n) {
if (n is Integer || n is Precise) return n;
if (n is Double) return n.isInteger ? Integer(n.toInt()) : n;
if (n is Imaginary) {
if (n.value is Precise) return n;
if (n.value.toDouble() == 0) return Integer(0);
if (n.value.isInteger && n.value is Double) {
return Imaginary(Integer(n.value.toInt()));
}
return n;
}
if (n is Complex) {
final realZero =
n.real.value == 0 && (n.real is! Precise || n.real == Precise.zero);
final imagZero = n.imag.value.value.toDouble() == 0 &&
(n.imag.value is! Precise || n.imag.value == Precise.zero);
if (realZero) {
if (imagZero) return simplifyType(n.real);
return Imaginary(simplifyType(n.imag.value));
} else if (imagZero) {
return simplifyType(n.real);
} else {
final simpleReal = simplifyType(n.real) as Real;
final simpleImag = simplifyType(n.imag.value);
if (identical(simpleReal, n.real) &&
identical(simpleImag, n.imag.value)) return n;
return Complex(simpleReal, Imaginary(simpleImag));
}
}
return n;
}