by via Bill Wagner: C# Development Blog | MySmartChannels on 7/9/2007 3:00:58 PM
I had a question last week about floating point accuracy. A customer was having some issues with a sampling algorithm that was supposed to grab timings every 0.0125 seconds. As he sampled data, he returned the elapsed time with every sample. The timing values in the returned structure weren't what he expected.
He was being bitten by the rounding that takes place in all floating point math.
Inside his code, he was adding 0.0125 to a running sum with every iteration.
Instead, to get avoid accumulating the error everytime, you need to keep track of the number of samples (an integer, with no rounding error), and calculate the elapsed time in one multiplication.
Here's a sample that demonstrates the difference:
static void Main(string[] args){ double interval = 0.0125; double sum = 0; double product = 0;
for (int i = 0; i < 100000; i++) { product = interval * (i+1); sum += interval; Console.WriteLine("Sumation: {0,15}, Product: {1,15}", sum, product); }}
Go ahead and run the code yourself to see the difference.
The lesson: There is always rounding in floating point math. Make sure you structure the operations so that you minimize any accumulated rounding error. In this case, it's a simple matter of replacing the repeated additions with a single multiplication. Your mileage may vary, but it's always important to remember these issues.
Original Post: Reader question about floating point
The content of the postings is owned by the respective author. CSharpFeeds is not responsible for the contents of the postings. This site is automatically generated and cannot be reviewed for abusive content. If you find abusive content on CSharpFeeds, please contact us. Designated trademarks and brands are the property of their respective owners. All rights reserved.