Difference among approximatelyEqual and essentiallyEqual in The art of computer programming

前端 未结 3 1224
南笙
南笙 2021-02-19 19:37

I get this code snippet from some where else. According to the webmaster, the code is picked from The art of computer programming by Knuth

Since I do not have a copy of

3条回答
  •  攒了一身酷
    2021-02-19 19:42

    To give an example:

    double a = 95.1, b = 100.0;
    assert( approximatelyEqual( a, b, 0.05 ) );
    assert( !essentiallyEqual( a, b, 0.05 ) );
    

    That is, with epsilon being a 5%, 95.1 is approximately 100, as it falls within the 5% margin of the 100 value (biggest). On the other hand, 95.1 is not essentially 100, as 100 is not within a 5% difference from 95.1 (smallest value).

提交回复
热议问题