Perl/mysql floating point imprecision

前端 未结 1 996
无人共我
无人共我 2020-12-22 01:25

I\'m using Perl to communicate floating point numbers with a mysql database. I perform a multiplication in perl:

 $var = 0.001 * 3;

I then

相关标签:
1条回答
  • 2020-12-22 01:54

    "10.0 times 0.1 is hardly ever 1.0" -- Brian Kernighan, The Elements of Programming Style

    It is a known limitation of FLOAT and DOUBLE that they are imprecise numeric data types. This is built into the design of the IEEE 754 format. It affects all programming languages that store floating-point numbers using this format.

    MySQL documents this in this appendix: B.5.5.8 Problems with Floating-Point Values.

    PHP documents this in Warning: Floating point precision.

    If you want a scaled numeric data type in MySQL that avoids this rounding behavior, use DECIMAL.

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