{"record":{"id":"c6960f0de9d68842","repo":"oxc-project/oxc","slug":"approximate-value-of-method-name-found","errorCode":null,"errorMessage":"Approximate value of `{method_name}` found.","messagePattern":"Approximate value of `(.+?)` found\\.","errorType":"validation","errorClass":"OxcDiagnostic","httpStatus":null,"severity":"warning","filePath":"crates/oxc_linter/src/rules/oxc/approx_constant.rs","lineNumber":14,"sourceCode":"use std::f64::consts as f64;\n\n// Based on https://github.com/rust-lang/rust-clippy//blob/c9a43b18f11219fa70fe632b29518581fcd589c8/clippy_lints/src/approx_const.rs\n// https://rust-lang.github.io/rust-clippy/master/#approx_constant\nuse oxc_ast::AstKind;\nuse oxc_diagnostics::OxcDiagnostic;\nuse oxc_macros::declare_oxc_lint;\nuse oxc_span::Span;\nuse oxc_str::static_ident;\n\nuse crate::{AstNode, context::LintContext, fixer::RuleFixer, rule::Rule};\n\nfn approx_constant_diagnostic(span: Span, method_name: &str) -> OxcDiagnostic {\n    OxcDiagnostic::warn(format!(\"Approximate value of `{method_name}` found.\"))\n        .with_help(format!(\"Use `Math.{method_name}` instead.\"))\n        .with_label(span)\n}\n\n#[derive(Debug, Default, Clone)]\npub struct ApproxConstant;\n\ndeclare_oxc_lint!(\n    /// ### What it does\n    ///\n    /// Disallows the use of approximate constants, instead preferring the use\n    /// of the constants in the `Math` object.\n    ///\n    /// ### Why is this bad?\n    ///\n    /// Approximate constants are not as accurate as the constants in the `Math` object.\n    /// Using the `Math` constants improves code readability and accuracy.\n    /// See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math","sourceCodeStart":1,"sourceCodeEnd":32,"githubUrl":"https://github.com/oxc-project/oxc/blob/e1e7af627c8843ab64044ed466b128fcc21a035b/crates/oxc_linter/src/rules/oxc/approx_constant.rs#L1-L32","documentation":"Diagnostic from oxlint rule oxc/approx-constant (suspicious category, ported from Rust Clippy's approx_constant). It flags numeric literals that are truncated or rounded spellings of the eight built-in Math constants — E, LN10, LN2, LOG2E, LOG10E, PI, SQRT1_2, SQRT2 — and provides an autofix replacing the literal with the exact Math.* property, which is both more precise and self-documenting.","triggerScenarios":"Any NumericLiteral whose source text (value.to_string) is longer than 4 characters AND either the constant's decimal expansion starts with that text (truncated, e.g. 3.141592 for PI) or the text equals the constant formatted to the same digit count (rounded, e.g. 0.434294 for LOG10E). Short literals like 3.14 or 2.71 never fire (min_digits = 4). The autofix is suppressed when a binding named `Math` is in scope at the literal (shadowing check via scoping().find_binding).","commonSituations":"Math/geometry code hand-typed from textbooks; constants losing precision in port from C/Python snippets; the fix quietly no-ops in files that define their own Math variable, so a lingering warning there is expected.","solutions":["Accept the autofix: 'npx oxlint --fix' rewrites 3.141592 to Math.PI (and 0.707106 to Math.SQRT1_2, etc.)","Replace by hand when a fixer run isn't possible: use Math.E, Math.LN10, Math.LN2, Math.LOG2E, Math.LOG10E, Math.PI, Math.SQRT1_2, Math.SQRT2","If the truncated value is a deliberate approximation, keep it and disable inline: // oxlint-disable-next-line oxc/approx-constant"],"exampleFix":"// before\nconst getArea = (radius) => 3.141 * radius * radius;\n\n// after\nconst getArea = (radius) => Math.PI * radius * radius;","handlingStrategy":"validation","validationCode":"// .oxlintrc.json\n\"rules\": { \"oxc/approx-constant\": \"warn\" }\n\nnpx oxlint -c .oxlintrc.json --fix   // applies Math.* replacements","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Reach for Math.PI/E/LN2/... by default when writing math code instead of typing decimals from memory","Run oxlint --fix in pre-commit or CI so truncated constants are upgraded automatically","Name domain-specific constants (const GOLDEN_RATIO = ...) when you truly need a non-Math value"],"tags":["oxc","math","constants","precision","oxlint","autofix"],"backgroundTag":"hardcoded-math-constants","analyzedSha":"e1e7af627c8843ab64044ed466b128fcc21a035b","analyzedAt":"2026-08-20T07:01:07.079Z","contentChangedAt":"2026-08-20T07:01:07.079Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}