oxc-project/oxc · warning · OxcDiagnostic
Approximate value of `{method_name}` found.
Error message
Approximate value of `{method_name}` found. What it means
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.
Source
Thrown at crates/oxc_linter/src/rules/oxc/approx_constant.rs:14
use std::f64::consts as f64;
// Based on https://github.com/rust-lang/rust-clippy//blob/c9a43b18f11219fa70fe632b29518581fcd589c8/clippy_lints/src/approx_const.rs
// https://rust-lang.github.io/rust-clippy/master/#approx_constant
use oxc_ast::AstKind;
use oxc_diagnostics::OxcDiagnostic;
use oxc_macros::declare_oxc_lint;
use oxc_span::Span;
use oxc_str::static_ident;
use crate::{AstNode, context::LintContext, fixer::RuleFixer, rule::Rule};
fn approx_constant_diagnostic(span: Span, method_name: &str) -> OxcDiagnostic {
OxcDiagnostic::warn(format!("Approximate value of `{method_name}` found."))
.with_help(format!("Use `Math.{method_name}` instead."))
.with_label(span)
}
#[derive(Debug, Default, Clone)]
pub struct ApproxConstant;
declare_oxc_lint!(
/// ### What it does
///
/// Disallows the use of approximate constants, instead preferring the use
/// of the constants in the `Math` object.
///
/// ### Why is this bad?
///
/// Approximate constants are not as accurate as the constants in the `Math` object.
/// Using the `Math` constants improves code readability and accuracy.
/// See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/MathView on GitHub (pinned to e1e7af627c)
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
Example fix
// before const getArea = (radius) => 3.141 * radius * radius; // after const getArea = (radius) => Math.PI * radius * radius;
Defensive patterns
Strategy: validation
Validate before calling
// .oxlintrc.json
"rules": { "oxc/approx-constant": "warn" }
npx oxlint -c .oxlintrc.json --fix // applies Math.* replacements Prevention
- 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
When it happens
Trigger: 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).
Common situations: 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.
Related errors
- Math.min and Math.max combination leads to constant result
- Could not find the reuseWorker option in ${path}
- Fixed size allocators are only supported on 64-bit little-en
- Expected {expected} and instead saw {actual}
- Avoid calls to the `Array` constructor
AI-assisted analysis of oxc-project/oxc@e1e7af627c (2026-08-20).
Data as JSON: /api/errors/c6960f0de9d68842.
Report an issue: GitHub.