Floating Point Arithmetic error

前端 未结 2 1203
情歌与酒
情歌与酒 2021-01-18 11:27

I\'m using the following function to approximate the derivative of a function at a point:

def prime_x(f, x, h):

    if not f(x+h) == f(x) and not h == 0.0:          


        
2条回答
  •  南方客
    南方客 (楼主)
    2021-01-18 11:28

    When you subtract two numbers that are almost the same, the result has much less precision than either of the inputs. This reduces the precision of the overall result.

    Suppose you have the following two numbers, good to 15 decimal places:

      1.000000000000001
    - 1.000000000000000
    = 0.000000000000001
    

    See what happened? The result only has one good digit.

提交回复
热议问题