Home Image-Updated-Review Radim Marek: Same rows, different SUM

Radim Marek: Same rows, different SUM

0
Computer Server
Source: commons

LAHORE, June 28 — A software developer’s deep dive into floating-point arithmetic has turned up a surprisingly stubborn problem that, as he puts it, “everyone knows” about yet still manages to cause real-world trouble. Radim Marek’s analysis, titled “Same rows, different SUM,” examines why database queries that look identical can return different totals. The trouble, Marek explains, is not the old warning about storing money as a double-precision number.

That rule, he says, is so well drilled that it has stopped being interesting. The real problem often starts elsewhere, in the quieter corners of how computers handle numbers with decimal points.

Marek’s work focuses on the floating-point data type. He describes how even simple arithmetic can produce results that surprise careful programmers. For example, adding 0.1 and 0.2 in floating-point arithmetic does not yield exactly 0.3.

The computer gets close, but not exact. This is not a bug, Marek notes — it is a fundamental feature of the IEEE 754 standard that nearly every modern computer uses.

The encouraging part of Marek’s analysis is that he does not just identify the problem. He walks through concrete examples, showing how the same database query, run on the same data, can produce different sums depending on the order in which rows are processed. The order matters because floating-point addition is not associative — (a + b) + c is not always the same as a + (b + c) when fractions are involved.

Early findings suggest that many developers who think they have accounted for floating-point issues may still be vulnerable. Marek demonstrates this with a series of test cases. In one scenario, a query that sums a column of numbers returns one value when sorted by one column and a different value when sorted by another.

The difference is small — often in the last few decimal places — but in financial or scientific applications, such discrepancies can cascade. Patients should, in a manner of speaking, check their assumptions.

For software that handles money, Marek recommends using fixed-point decimal types instead of floating-point. For scientific work, he suggests careful rounding strategies or accepting that small errors are inevitable and planning for them. The analysis also touches on how different database systems handle these issues.

Some databases, Marek notes, have built-in safeguards. Others leave it to the developer to choose the right data type.

The key takeaway, he writes, is that “the same rows” do not guarantee “the same sum” unless the developer has explicitly controlled for floating-point behavior. Marek’s work serves as a reminder that even well-known problems can still catch people out. The rule about not storing money as a double is common knowledge, but the deeper issues with floating-point arithmetic — the ones that cause the same query to return different results — are less widely understood.

His advice for anyone working with numerical data: test your assumptions, know your data types, and when in doubt, talk to your database administrator.