For AI agents: A markdown version of this page is available at https://docs.datadoghq.com/security/code_security/static_analysis/static_analysis_rules/rust-code-quality/approx-constant.md. A documentation index is available at /llms.txt.

Float literal approximates a known mathematical constant

This product is not supported for your selected Datadog site. ().

Metadata

ID: rust-code-quality/approx-constant

Language: Rust

Severity: Error

Category: Error Prone

CWE: 682

Description

Using a hand-written approximation of a floating-point constant defined in std::f32::consts or std::f64::consts. The standard library constants carry the maximum precision available for the type so they should be used. For example std::f64::consts::PI

Non-Compliant Code Examples

// approximate PI
let a = 3.14;
// approximate E
let b = 2.7183_f64;

Compliant Code Examples

let a = std::f32::consts::PI;
let b = std::f64::consts::E;
let c = 3.1;
https://static.datadoghq.com/static/images/logos/github_avatar.svg https://static.datadoghq.com/static/images/logos/vscode_avatar.svg jetbrains

Seamless integrations. Try Datadog Code Security