RS.CLIPPY.SUBOPTIMAL_FLOPS

Usage of sub-optimal floating point operations

This checker is a Clippy lint created by The Rust Project Contributors. The documentation shown here is a copy of the original documentation for: suboptimal_flops. Copyright ©2025 The Rust Team. All rights reserved.

What it does

Looks for floating-point expressions that can be expressed using built-in methods to improve both accuracy and performance.

Why is this bad?

Negatively impacts accuracy and performance.

Example

use std::f32::consts::E;

let a = 3f32;
let _ = (2f32).powf(a);
let _ = E.powf(a);
let _ = a.powf(1.0 / 2.0);
let _ = a.log(2.0);
let _ = a.log(10.0);
let _ = a.log(E);
let _ = a.powf(2.0);
let _ = a * 2.0 + 4.0;
let _ = if a < 0.0 {
    -a
} else {
    a
};
let _ = if a < 0.0 {
    a
} else {
    -a
};

is better expressed as

use std::f32::consts::E;

let a = 3f32;
let _ = a.exp2();
let _ = a.exp();
let _ = a.sqrt();
let _ = a.log2();
let _ = a.log10();
let _ = a.ln();
let _ = a.powi(2);
let _ = a.mul_add(2.0, 4.0);
let _ = a.abs();
let _ = -a.abs();