Pitfalls of Floating-Point Numbers (And How To Avoid Them)
摘要
Floating-point numbers can be hazardous. In this chapter, we take a look at some of the pitfalls of floating-point computation and develop rules of thumb through which we can (hopefully) avoid most of them. We first examine some of the catastrophic and even deadly consequences of using floating-point computation poorly. We then bring things down a bit and examine expressions and processes that can lead to a loss of precision. Next, we enumerate some rules of thumb that can help us design code that is less susceptible to these pitfalls, thereby leading to more robust software. Finally, we describe a software tool that can help refactor floating-point code to be more reliable.