range(-8.00, 8.00, 0.05) acting very strange

后端 未结 3 456
迷失自我
迷失自我 2021-01-13 17:16

Does anyone know why range(-8.00, 8.00, 0.05) seems fail?

Array
(
[0] => -8
[1] => -7.95
[2] => -7.9
[3] => -7.85
[4] => -7.8
[5] => -7.75
         


        
3条回答
  •  逝去的感伤
    2021-01-13 17:44

    That's a known problem, hopefully already fixed. See bug #54459.

    Basically now instead of making consecutive floating point additions, the range function now increments an integer and multiplies it by the step.

    Basically, you know have:

    -8.00, -8.00 + 1 * 0.05, -8.00 + 2 * 0.05, ...
    

    instead of

    -8.00, -8.00 + 0.05, -8.00 + 0.05 + 0.05, ...
    

    You can see this makes a difference:

    $ php
    

    while:

    $ php -r 'echo sprintf("%.17f", .1*100);'
    10.00000000000000000
    

提交回复
热议问题