AI basics & verificationPractical AI workflows

Audit AI-written JavaScript equality with three floating-point boundary cases

Replacing an equality test with “difference less than EPSILON” can repair a small decimal example while breaking counts, identifiers or larger-number calculations. Define whether you are comparing an exact object or an approximate value first. The answers here are an editorial JavaScript reference, not outputs measured from an AI model.

Close-up of binary digits on a screen, illustrating finite-precision representation
Illustrative photo: Alexander Sinn · Unsplash License

The workflow at a glance

JavaScript Number uses finite precision. Strict equality, a task-defined approximate comparison and exact-integer handling are distinct routes. Number.EPSILON describes spacing near 1, not a universal tolerance across magnitudes and tasks, and it cannot restore information lost earlier in an operation.

Define what equality must preserve

MDN’s Number.EPSILON documentation explains precision near 1 and the effect of magnitude. This is not an automatic accuracy setting shared by pixels, lengths, counts or application rules. Define the type, unit and allowable error first.

These exercises use ordinary JavaScript Number. The input consists of three expressions and a mathematical reference; no AI product was called, compared or supplied with business data. Ask for execution results and mathematical meaning in separate fields.

Original review input

A: (0.1 + 0.2) === 0.3
B: Math.abs((1000.1 + 1000.2) - 2000.3) < Number.EPSILON
C: (1e16 + 1) - 1e16

Task: predict JavaScript Number results, explain the difference from exact arithmetic,
and state what comparison policy would be appropriate before proposing a code change.
Original task material; reference answers are supplied separately below.

The three cases expose different failures

A evaluates to false. Its computed sum is 0.30000000000000004 rather than exact decimal 0.3. In B the difference is approximately 2.2737367544323206e-13, larger than Number.EPSILON, so B is false too. Copying the small-value trick to another magnitude misses the scale of the error.

C returns 0, while exact integer arithmetic gives 1. In this Number expression, 1e16+1 has already lost the unit increment. A later comparison or display-formatting operation cannot reconstruct it.

Human reference: execution and meaning stay separate
CaseNumber resultDifference to explain
AfalseBinary decimal representation and strict equality
BfalseA fixed EPSILON does not adapt to magnitude
C0The unit increment is lost during addition

Let the task specify the tolerance

If this exercise explicitly accepts absolute error up to 1e-12 for approximate values, the two sides in A and in B can both be considered close enough under that contract. The tolerance is an editorial exercise setting, not universal measurement accuracy or a product guarantee. Reconsider it when units, magnitude or input precision change.

Counts and identifiers must not be merged merely for being close. Where exact integers are required, choose a suitable representation before arithmetic, such as constructing BigInt from a preserved integer string. Converting an already imprecise Number afterward cannot repair the lost information. Input types and subsequent serialization must be handled together.

Keep three contracts separate

Exact identity or count

Preserve the required information in the representation, then compare under an exact rule.

Approximate value

Use units, input precision and requirements to select an absolute or relative tolerance and state acceptance.

Display only

Decimal formatting controls presentation; it does not automatically repair the arithmetic.

Include counterexamples in the repair request

The prompt below asks for classification before code so that a project-wide replacement of every === is not mistaken for a fix. The failure specimen and correction are editorial constructions, not evidence of a model failing.

After running the reference cases, inspect the proposal. Did it change the input contract? Does its tolerance have a meaningful basis? Does it acknowledge that exact large integers need protection before calculation? If it merely adds EPSILON, ask why B and C remain unresolved.

A bounded review prompt with acceptance conditions

Review A, B and C above under JavaScript Number semantics.
Separate exact identity/counts, approximate values with a stated tolerance, and display formatting.
For the illustrative approximate-value task only, use absolute tolerance 1e-12.
Give expected results and explain the point where C loses information.
Do not replace every strict equality in a project. If an exact integer is required,
start from an original integer string and explain a suitable representation before arithmetic.
Label any unexecuted code as a proposal, not a tested result.
Editorial review material; no request to a model is implied.

A looks repaired, but the task has changed

Editorial failure specimen

Replace all equality with difference less than Number.EPSILON and format C’s zero to one decimal place.

Human correction direction

Separate the contracts: state 1e-12 for the illustrative approximate task, preserve exact integer input before arithmetic and format display separately.

Passing one decimal specimen does not establish correct identity checks or large-number operations.

Review data handoff alongside the code result

File handoff can change a numeric token’s type before calculation. See the JSON-to-Excel long-ID and numeric-text guide. Equality after the input has changed cannot establish the original object’s correctness.

Display precision is another layer; read the intermediate-rounding and final-display exercise. Keep original representation, calculation policy and display format separate rather than hiding different numbers behind identical formatted text.

Conditions for accepting an AI repair proposal

  • The input contract and units are stated.
  • A, B and C have separate execution results and explanations.
  • Approximate comparison uses a task-defined tolerance rather than a universal claim.
  • Exact integer inputs have not passed through a lossy conversion before arithmetic.
  • Unrun proposals and actual verification remain distinguishable.

Common questions

Can I compare fixed-decimal strings instead?

Only when the task actually compares a defined display string. Formatting rounds and changes type; it is not a general substitute for numeric equality or exact identity.

Can converting the result of 1e16+1 to BigInt restore the increment?

No. The Number addition has already lost it. Start from the original integer string or an exact-integer representation when exactness is required.

Does a correct explanation establish that the entire application is safe?

No. These are three specific boundaries. Review the real function’s input range, types, exceptions and later uses separately.

Further reading & sources

Newer articles on this topic

Continue with a newer article that refers to this guide or explainer.

Related articles