Floating-Point Numbers Aren’t Real

Floating-Point Numbers Aren’t Real

Chuck Allison

FLOATiNG-POiNT NUMBERS ARE NOT “REAL NUMBERS” in the mathemati- cal sense, even though they are called real in some programming languages, such as Pascal and Fortran. Real numbers have infinite precision and are there- fore continuous and nonlossy; floating-point numbers have limited precision, so they are finite, and they resemble “badly behaved” integers, because they’re not evenly spaced throughout their range.

To illustrate, assign 2147483647 (the largest signed 32-bit integer) to a 32-bit float variable (x, say), and print it. You’ll see 2147483648. Now print x-64. Still 2147483648. Now print x-65, and you’ll get 2147483520! Why? Because the spacing between adjacent floats in that range is 128, and floating-point opera- tions round to the nearest floating-point number.

IEEE floating-point numbers are fixed-precision numbers based on base-two scientific notation: 1.d1d2…dp 1 × 2e, where p is the precision (24 for float, 53 for double). The spacing between two consecutive numbers is 21–p+e, which can be safely approximated by ε|x|, where ε is the machine epsilon (21–p).

Knowing the spacing in the neighborhood of a floating-point number can help you avoid classic numerical blunders. For example, if you’re performing an iterative calculation, such as searching for the root of an equation, there’s no sense in asking for greater precision than the number system can give in the neighborhood of the answer. Make sure that the tolerance you request is no smaller than the spacing there, otherwise you’ll loop forever.

Since floating-point numbers are approximations of real numbers, there is inevi- tably a little error present. This error, called roundoff, can lead to surprising results.

??66 97 Things Every Programmer Should Know

?

???????????????When you subtract nearly equal numbers, for example, the most significant digits cancel one another out, so what was the least significant digit (where the roundoff error resides) gets promoted to the most significant position in the floating-point result, essentially contaminating any further related computa- tions (a phenomenon known as smearing). You need to look closely at your algorithms to prevent such catastrophic cancellation. To illustrate, consider solving the equation x2 – 100000x + 1 = 0 with the quadratic formula. Since the operands in the expression –b + sqrt(b2 – 4) are nearly equal in magnitude, you can instead compute the root r1 = –b – sqrt(b2 – 4), and then obtain r2 = 1/r1, since for any quadratic equation, ax2 + bx + c = 0, the roots satisfy r1r2 = c/a.

Smearing can occur in even more subtle ways. Suppose a library na?vely com- putes ex by the formula 1 + x + x2/2 + x3/3! + …. This works fine for positive x, but consider what happens when x is a large negative number. The even-powered terms result in large positive numbers, and subtracting the odd-powered mag- nitudes will not even affect the result. The problem here is that the roundoff in the large, positive terms is in a digit position of much greater significance than the true answer. The answer diverges toward positive infinity! The solution here is also simple: for negative x, compute ex = 1/e|x|.

It should go without saying that you shouldn’t use floating-point numbers for financial applications—that’s what decimal classes in languages like Python and C# are for. Floating-point numbers are intended for efficient scientific computation. But efficiency is worthless without accuracy, so remember the source of rounding errors, and code accordingly!

时间: 2024-11-05 18:46:31

Floating-Point Numbers Aren’t Real的相关文章

uva 1530 - Floating Point Numbers(数论)

题目链接:uva 1530 - Floating Point Numbers 题目大意:给出一个16位的二进制数,用来表示一个浮点数,第一位为符号,1~7位表示一个十进制的数s,e=63-s;剩下的8位为小数部分,默认整数部分为1,得到f,然后最后a=f*2^e,要求用科学计数法输出a. 解题思路:模拟就好了,注意0的情况特殊处理,以及科学计数法的整数部分不能为0. #include <cstdio> #include <cstring> #include <cmath>

HDU2289-Cup-二分

Cup Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 8946    Accepted Submission(s): 2747 Problem Description The WHU ACM Team has a big cup, with which every member drinks water. Now, we know th

UVA 11178 Morley&#39;s Theorem 计算几何

计算几何: 最基本的计算几何,差积  旋转 Morley's Theorem Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu Submit Status Description Problem D Morley's Theorem Input: Standard Input Output: Standard Output Morley's theorem states that that the line

D - I Think I Need a Houseboat(1.3.1)

Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Submit Status Description Fred Mapper is considering purchasing some land in Louisiana to build his house on. In the process of investigating the land, he learned that the s

Python基础(二)

Python基础(二) Python 运算符(算术运算.比较运算.赋值运算.逻辑运算.成员运算) 基本数据类型(数字.布尔值.字符串.列表.元组.字典.set集合) for 循环 enumrate range和xrange 编码与进制转换 Python 运算符 1.算术运算: 2.比较运算: 3.赋值运算: 4.逻辑运算:  5.成员运算: 基本数据类型 1.数字 int(整型) 在32位机器上,整数的位数为32位,取值范围为-2**31-2**31-1,即-2147483648-2147483

atof

atof 1. 函数名: atof 功 能: 把字符串转换成浮点数 名字来源:ascii to floating point numbers 的缩写 用 法: double atof(const char *nptr); #include <stdlib.h> #include <stdio.h> int main() { float f; char *str = "12345.67"; f = atof(str); printf("string =

Python之路【第二篇】:Python基础(一)

Python之路[第二篇]:Python基础(一) 入门知识拾遗 一.作用域 对于变量的作用域,执行声明并在内存中存在,该变量就可以在下面的代码中使用. 1 2 3 if 1==1:     name = 'wupeiqi' print  name 下面的结论对吗? 外层变量,可以被内层变量使用 内层变量,无法被外层变量使用 二.三元运算 1 result = 值1 if 条件 else 值2 如果条件为真:result = 值1如果条件为假:result = 值2 三.进制 二进制,01 八进

第二篇:python基础之数据类型与变量

数据类型 什么是数据类型? 程序的本质就是驱使计算机去处理各种状态的变化,这些状态分为很多种 例如英雄联盟游戏,一个人物角色有名字,钱,等级,装备等特性,大家第一时间会想到这么表示 名字:德玛西亚------------>字符串 钱:10000   ------------>数字 等级:15    ------------>数字 装备:鞋子,日炎斗篷,兰顿之兆---->列表 除此之外还有很多其他数据,处理不同的数据就需要定义不同的数据类型 基本数据类型: 一.数字(包括:整型,长整

python之旅2

python基础 1整数 查看整数类型的方法 >>> a = 1 >>> dir(a) ['__abs__', '__add__', '__and__', '__class__', '__cmp__', '__coerce__', '__delattr__', '__div__', '__divmod__', '__doc__', '__float__', '__floordiv__', '__format__', '__getattribute__', '__getn

URAL 1205 By the Underground or by Foot?(SPFA)

By the Underground or by Foot? Time limit: 1.0 secondMemory limit: 64 MB Imagine yourself in a big city. You want to get from point A to point B. To do that you may move by foot or use the underground. Moving by the underground is faster but you may