Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cleanly dealing with rounding/accumulation errors

Tags:

c#

.net

rounding

I have an application where I am tracking volumes of fluid in µL. I'm currently using 'double' for storage of volumes throughout the system and this works fine, in most cases. However, when I start adding and subtracting large numbers of these volumes, various accumulation errors start creeping in. The errors are very small in magnitude, but they cause problems with threshold comparisons, where suddenly the volume is smaller than expected by a miniscule amount, causing validation failures. I understand this is a fairly common problem with performing accumulating floating point operations, but I'm wondering how best to address the issues. A few thoughts I've had:

  1. I can replace all my double references with integers and instead track everything in nL. This would definitely solve the problem, but it's a very invasive change. The system is not yet in production use, however, which means applying it now will be a lot easier than trying to apply it later.

  2. I can use Decimal instead of double. This is less invasive than changing to integers, but still requires fairly significant changes.

  3. I can require that all volume comparisons allow for a specified error tolerance. This is mostly what I'm doing right now, but it makes the comparison code uglier and it requires some code review to make sure nobody forgets to apply the pattern.

  4. I can perform rounding to a specified tolerance after each computation to prevent the error accumulation. This makes the comparisons cleaner, but now it has a similar problem everywhere that there are assignments.

For those who have also struggled with this problem, what solutions wound up being the cleanest to implement? Are there other gotchas I should know about when performing accumulating calculations?

like image 562
Dan Bryant Avatar asked Feb 25 '13 17:02

Dan Bryant


People also ask

How can rounding errors be resolved?

First, you can increase the number of decimal places displayed by formatting the cells to show more decimal places. This will ensure that the stored value is not rounded when it is displayed. Second, you can use the ROUND function in your formulas to explicitly round values to a certain number of decimal places.

How do you handle rounding?

Rules for roundingIdentify the place value you are rounding to. For example, for rounding currency to the nearest cent use 2 decimal places (hundredths). Look at the digit to the right of this place: If this number is less than 5 (0, 1, 2, 3, 4) you leave your last digit as it is.

How do you stop rounding errors in Excel?

You can frequently prevent floating point rounding errors from affecting your work by setting the Precision as displayed option before you apply a number format to your data. This option forces the value of each number in the worksheet to be at the precision that is displayed on the worksheet.

What is a rounding error in accounting?

What Is a Rounding Error? A rounding error, or round-off error, is a mathematical miscalculation or quantization error caused by altering a number to an integer or one with fewer decimals.


1 Answers

Ideally, you want to

  • use a data type that's not subject to rounding (approximation) error, and
  • round to the right number of decimal places
  • at the right time.

You don't need the great range of a double-precision float. You probably don't want to use integers. They're slightly faster, but using them is more complex. Use numeric or decimal. Numeric and decimal data types aren't subject to either rounding errors or to errors of approximation. But you still can't be careless or sloppy in your programming; assigning a numeric value to a variable of type double will bring the problem right back to you.

Exactly what the right number of decimal places and the right time mean are dependent on your application. The right number of decimal places might sometimes be more than the number of places you need to store as your final value. The right time can be influenced by the number and nature of intermediate calculations.

Sometimes, what people call rounding error is really approximation error. If you tell your computer to store the decimal value r in a floating-point variable or database column, it will actually store the closest floating-point approximation to r.

Canonical reference for FP arithmetic

like image 174
Mike Sherrill 'Cat Recall' Avatar answered Oct 02 '22 22:10

Mike Sherrill 'Cat Recall'