Skip to content
  • Home
  • About
  • No Access
  • Privacy Policy
Bits of .NET
Bits of .NET
  • Asp.Net Core
  • C#
  • SQL
  • JavaScript
  • CSS
  • About
  • ErcanOPAK.com
  • No Access
  • Privacy Policy

Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

  • Asp.Net Core
  • C#
  • SQL
  • JavaScript
  • CSS
  • About
  • ErcanOPAK.com
  • No Access
  • Privacy Policy
AI

The AI Prompt That Flags Floating-Point Equality Checks Before They Cause a Flaky Test or a Wrong Total

- 15.10.26 - ErcanOPAK

🤖 The Prompt That Catches == on Floating-Point Numbers

Comparing two floating-point values with == works right up until the moment two numbers that are mathematically equal end up represented with a tiny binary rounding difference – and then a test that passed a thousand times starts failing intermittently, or a running total that should match a displayed value is off by a fraction of a cent. These comparisons are easy to write without thinking twice and easy to miss in a normal code review, because the code reads as obviously correct.

🔎 The Prompt

Review the pasted code for direct equality comparisons
(==, Equals, or language-equivalent) involving float,
double, or decimal values (or their equivalents in this
language) that were produced by a calculation rather than
read as a fixed literal. For each one:

1. Quote the exact comparison and the line it's on.

2. Explain specifically why this comparison can fail even
   when the two values are mathematically equal - trace
   through where the rounding difference would actually
   come from in this specific calculation.

3. Suggest the correct fix for this case specifically:
   either an epsilon/tolerance-based comparison with a
   concrete suggested tolerance, or switching the
   underlying type to a fixed-point/decimal type if the
   values represent money or another exact quantity.

4. Separately list any comparisons against a fixed literal
   (checking if a value equals exactly 0 or 1, for example)
   that are actually fine as written, so I know what NOT
   to change.

Code to review:
[paste the code here]

✅ Fix: Why This Prompt Works

  • Asking it to trace where the rounding difference would specifically come from, rather than giving a generic warning about floating point, forces a real answer tied to the actual calculation in front of you – that’s what separates a comparison that’s genuinely risky from one that happens to use a float type but is actually safe.
  • Explicitly asking for the cases that are fine as written is the detail that keeps this prompt useful rather than alarming – a blanket ‘never compare floats with ==’ rule would flag plenty of harmless comparisons against sentinel values like 0 or -1 right alongside the real risks.
  • Requesting a concrete suggested tolerance instead of a generic ‘use an epsilon’ gives you something you can actually paste into the fix, rather than a reminder you still have to go research the right magnitude for.

⚠️ Why This Is Easy to Miss

  • This isn’t a lint rule you can just turn on everywhere – some floating-point equality checks against literal sentinel values are completely correct, and a reviewer (human or automated) that flags all of them equally just trains people to ignore the warning.
  • For anything representing money specifically, the right fix usually isn’t a tolerance at all – it’s switching to a decimal or fixed-point type from the start, since even a well-chosen epsilon is a workaround for a type choice that shouldn’t have been float in the first place.

Two floating-point numbers that are mathematically equal aren’t always bit-for-bit equal – == doesn’t know the difference, and a rounding error that small is exactly the kind of thing worth having a second pass look for.

— AI Power User

Related posts:

AI Prompt: Optimize Slow SQL Queries with AI Analysis

AI Prompt: Optimize Slow SQL Queries with AI Analysis

The AI Prompt That Finds Every Place Your API Returns a 200 Status on a Failed Operation

The AI Prompt That Finds Every Place Your API Returns a 200 Status on a Failed Operation

AI Prompt — SQL Performance Autopsy

AI Prompt — SQL Performance Autopsy

Views: 5

Post navigation

Git: Fix git log –follow That Silently Loses a File’s History After a Rename Git Didn’t Detect
The AI Prompt That Turns Your Pet’s Vet Records Into a Single Vaccination and Medication Reminder Timeline

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

November 2026
M T W T F S S
 1
2345678
9101112131415
16171819202122
23242526272829
30  
« Oct    

Most Viewed Posts

  • How to make theater mode the default for Youtube (1,055)
  • Get the User Name and Domain Name from an Email Address in SQL (981)
  • How to add default value for Entity Framework migrations for DateTime and Bool (977)
  • Get the First and Last Word from a String or Sentence in SQL (858)
  • How to select distinct rows in a datatable in C# (854)
  • How to enable, disable and check if Service Broker is enabled on a database in SQL Server (653)
  • Add Constraint to SQL Table to ensure email contains @ (609)
  • Average of all values in a column that are not zero in SQL (570)
  • How to use Map Mode for Vertical Scroll Mode in Visual Studio (547)
  • Find numbers with more than two decimal places in SQL (475)

Recent Posts

  • C#: Fix a ConditionalWeakTable Entry Vanishing Too Soon
  • Docker: Fix a Stale COPY Layer After Editing .dockerignore
  • Photoshop: Fix a Pattern Overlay Scaling Wrong on a High-DPI File
  • SQL Server: Fix a Computed Column Index Skipped by Implicit Cast
  • HTML5: Fix a required Input Inside a Hidden Tab Blocking Submit
  • SQL Server: Fix ROW_NUMBER Ties Reordering Between Identical Runs
  • C#: Fix an Enum HasFlag Returning True for the Zero Value
  • WordPress: Fix Featured Images Vanishing After a CDN Migration
  • Photoshop: Fix Content-Aware Fill Smearing a Repeating Pattern
  • Asp.Net Core: Fix Antiforgery Tokens Rejected Behind a Reverse Proxy

Most Viewed Posts

  • How to make theater mode the default for Youtube (1,055)
  • Get the User Name and Domain Name from an Email Address in SQL (981)
  • How to add default value for Entity Framework migrations for DateTime and Bool (977)
  • Get the First and Last Word from a String or Sentence in SQL (858)
  • How to select distinct rows in a datatable in C# (854)

Recent Posts

  • C#: Fix a ConditionalWeakTable Entry Vanishing Too Soon
  • Docker: Fix a Stale COPY Layer After Editing .dockerignore
  • Photoshop: Fix a Pattern Overlay Scaling Wrong on a High-DPI File
  • SQL Server: Fix a Computed Column Index Skipped by Implicit Cast
  • HTML5: Fix a required Input Inside a Hidden Tab Blocking Submit

Social

  • ErcanOPAK.com
  • GoodReads
  • LetterBoxD
  • Linkedin
  • The Blog
  • Twitter
© 2026 Bits of .NET | Built with Xblog Plus free WordPress theme by wpthemespace.com