Dealing with floating point errors in .NET

后端 未结 3 1598
囚心锁ツ
囚心锁ツ 2020-12-18 00:03

I\'m working on a scientific computation & visualization project in C#/.NET, and we use doubles to represent all the physical quantities. Since floating-po

3条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-18 00:48

    This is not a problem unique to .NET. The strategy to reduce loss of precision is to re-order calculations so that you multiply large quantities times small quantities and add/subtract similiar sized quantities (without changing the nature of the calculation, obviously).

    In your example, rather than multiply 5 large quantities together and then divide by 5 large quantities, re-order to divide each large quantity by one of the divisors, and then multiply these 5 partial results.

    Of interest? (if you haven't already read): What Every Computer Scientist Should Know About Floating-Point Arithmetic

提交回复
热议问题