Description
Last summer I worked with MATLAB code that was copied-down from equations directly, when floating point math allows for a lot of optimisations that aren't always obvious, like exp_m1
. Here are a few lints I thought of that specifically apply to floats:
a * b + c => a.mul_add(b, c)
x.powf(y) => x.powi(y)
(wheny
is an integer)x.powi(2) => x * x
2.powf(x) => x.exp2()
x.logN() / y.logN() => x.logbase(y)
x.logbase(E) => x.log()
x.logbase(10) => x.log10()
x.logbase(2) => x.log2()
.x * PI / 180 => x.to_radians()
x * 180 / PI => x.to_degrees()
x.powf(1/3) => x.cbrt()
(not equivalent, but almost certainly what was meant)x.powf(1/2) => x.sqrt()
x.exp() - 1 => x.exp_m1()
(x + 1).log() => x.log_1p()
sqrt(x * x + y * y) => x.hypot(y)
(I see this one used a lot)
Obviously we can't catch every case, and these would be best as hints rather than suggestions, but it'd be nice to at least suggest these options to the user so that they're aware that they exist.
I think that the scope of this should be limited to just suggesting builtin functions for now because the list of mathematical identities is way too large for clippy to incorporate. People who do things like x.log() + y.log()
instead of (xy).log()
can simplify their equations themselves, but people who don't know that x.hypot(y)
exists for example should be made aware.