floating point calculations in awk

前端 未结 1 1357
野性不改
野性不改 2021-01-13 09:52

I am surprised with behaviour of awk while performing floating point calculations. It lead me to wrong calculation on table data.



        
相关标签:
1条回答
  • 2021-01-13 10:19

    The following section from the manual should help you understand the issue you're observing:

    15.1.1.2 Floating Point Numbers Are Not Abstract Numbers

    Unlike numbers in the abstract sense (such as what you studied in high school or college arithmetic), numbers stored in computers are limited in certain ways. They cannot represent an infinite number of digits, nor can they always represent things exactly. In particular, floating-point numbers cannot always represent values exactly. Here is an example:

     $ awk '{ printf("%010d\n", $1 * 100) }'
     515.79
     -| 0000051579
     515.80
     -| 0000051579
     515.81
     -| 0000051580
     515.82
     -| 0000051582
     Ctrl-d
    

    This shows that some values can be represented exactly, whereas others are only approximated. This is not a “bug” in awk, but simply an artifact of how computers represent numbers.


    A highly recommended reading:

    What every computer scientist should know about floating-point arithmetic

    0 讨论(0)
提交回复
热议问题