开发者

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

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
[6] => -7.7
[7] => -7.65
[8] => -7.6
[9] => -7.55
[10] => -7.5
[11] => -7.45
[12] => -7.4
[13] => -7.35
[14] => -7.3
[15] => -7.25
[16] => -7.2
[17] => -7.15
[18] => -7.1
[19] => -7.05
[20] => -7
[21] => -6.95
[22] => -6.9
[23] => -6.85
[24] => -6.8
[25] => -6.75
[26] => -6.7
[27] => -6.65
[28] => -6.6
[29] => -6.55
[30] => -6.5
[31] => -6.45
[32] => -6.4
[33] => -6.35
[34] => -6.3
[35] => -6.25
[36] => -6.2
[37] => 开发者_开发知识库-6.15
[38] => -6.1
[39] => -6.05
[40] => -6
[41] => -5.95
[42] => -5.9
[43] => -5.85
[44] => -5.8
[45] => -5.75
[46] => -5.7
[47] => -5.65
[48] => -5.6
[49] => -5.55
[50] => -5.5
[51] => -5.45
[52] => -5.4
[53] => -5.35
[54] => -5.3
[55] => -5.25
[56] => -5.2
[57] => -5.15
[58] => -5.1
[59] => -5.05
[60] => -5
[61] => -4.95
[62] => -4.9
[63] => -4.85
[64] => -4.8
[65] => -4.75
[66] => -4.7
[67] => -4.65
[68] => -4.6
[69] => -4.55
[70] => -4.5
[71] => -4.45
[72] => -4.4
[73] => -4.35
[74] => -4.3
[75] => -4.25
[76] => -4.2
[77] => -4.15
[78] => -4.1
[79] => -4.05
[80] => -4
[81] => -3.95
[82] => -3.9
[83] => -3.85
[84] => -3.8
[85] => -3.75
[86] => -3.7
[87] => -3.65
[88] => -3.6
[89] => -3.55
[90] => -3.5
[91] => -3.45
[92] => -3.4
[93] => -3.35
[94] => -3.3
[95] => -3.25
[96] => -3.2
[97] => -3.15
[98] => -3.1
[99] => -3.05
[100] => -3
[101] => -2.95
[102] => -2.9
[103] => -2.85
[104] => -2.8
[105] => -2.75
[106] => -2.7
[107] => -2.65
[108] => -2.6
[109] => -2.55
[110] => -2.5
[111] => -2.45
[112] => -2.4
[113] => -2.35
[114] => -2.3
[115] => -2.25
[116] => -2.2
[117] => -2.15
[118] => -2.1
[119] => -2.05
[120] => -2
[121] => -1.95
[122] => -1.9
[123] => -1.85
[124] => -1.8
[125] => -1.75
[126] => -1.7
[127] => -1.65
[128] => -1.6
[129] => -1.55
[130] => -1.5
[131] => -1.45
[132] => -1.4
[133] => -1.35
[134] => -1.3
[135] => -1.25
[136] => -1.2
[137] => -1.15
[138] => -1.1
[139] => -1.05
[140] => -1
[141] => -0.95000000000002
[142] => -0.90000000000002
[143] => -0.85000000000002
[144] => -0.80000000000002
[145] => -0.75000000000002
[146] => -0.70000000000002
[147] => -0.65000000000002
[148] => -0.60000000000002
[149] => -0.55000000000002
[150] => -0.50000000000002
[151] => -0.45000000000002
[152] => -0.40000000000002
[153] => -0.35000000000002
[154] => -0.30000000000002
[155] => -0.25000000000002
[156] => -0.20000000000002
[157] => -0.15000000000002
[158] => -0.10000000000002
[159] => -0.05000000000002
[160] => -2.0053403382292E-14
[161] => 0.04999999999998
[162] => 0.09999999999998
[163] => 0.14999999999998
[164] => 0.19999999999998
[165] => 0.24999999999998
[166] => 0.29999999999998
[167] => 0.34999999999998
[168] => 0.39999999999998
[169] => 0.44999999999998
[170] => 0.49999999999998
[171] => 0.54999999999998
[172] => 0.59999999999998
[173] => 0.64999999999998
[174] => 0.69999999999998
[175] => 0.74999999999998
[176] => 0.79999999999998
[177] => 0.84999999999998
[178] => 0.89999999999998
[179] => 0.94999999999998
[180] => 0.99999999999998
[181] => 1.05
[182] => 1.1
[183] => 1.15
[184] => 1.2
[185] => 1.25
[186] => 1.3
[187] => 1.35
[188] => 1.4
[189] => 1.45
[190] => 1.5
[191] => 1.55
[192] => 1.6
[193] => 1.65
[194] => 1.7
[195] => 1.75
[196] => 1.8
[197] => 1.85
[198] => 1.9
[199] => 1.95
[200] => 2
[201] => 2.05
[202] => 2.1
[203] => 2.15
[204] => 2.2
[205] => 2.25
[206] => 2.3
[207] => 2.35
[208] => 2.4
[209] => 2.45
[210] => 2.5
[211] => 2.55
[212] => 2.6
[213] => 2.65
[214] => 2.7
[215] => 2.75
[216] => 2.8
[217] => 2.85
[218] => 2.9
[219] => 2.95
[220] => 3
[221] => 3.05
[222] => 3.1
[223] => 3.15
[224] => 3.2
[225] => 3.25
[226] => 3.3
[227] => 3.35
[228] => 3.4
[229] => 3.45
[230] => 3.5
[231] => 3.55
[232] => 3.6
[233] => 3.65
[234] => 3.7
[235] => 3.75
[236] => 3.8
[237] => 3.85
[238] => 3.9
[239] => 3.95
[240] => 4
[241] => 4.05
[242] => 4.1
[243] => 4.15
[244] => 4.2
[245] => 4.25
[246] => 4.3
[247] => 4.35
[248] => 4.4
[249] => 4.45
[250] => 4.5
[251] => 4.55
[252] => 4.6
[253] => 4.65
[254] => 4.7
[255] => 4.75
[256] => 4.8
[257] => 4.85
[258] => 4.9
[259] => 4.95
[260] => 5
[261] => 5.05
[262] => 5.1
[263] => 5.15
[264] => 5.2
[265] => 5.25
[266] => 5.3
[267] => 5.35
[268] => 5.4
[269] => 5.45
[270] => 5.5
[271] => 5.55
[272] => 5.6
[273] => 5.65
[274] => 5.7
[275] => 5.75
[276] => 5.8
[277] => 5.85
[278] => 5.9
[279] => 5.95
[280] => 6
[281] => 6.05
[282] => 6.1
[283] => 6.15
[284] => 6.2
[285] => 6.25
[286] => 6.3
[287] => 6.35
[288] => 6.4
[289] => 6.45
[290] => 6.5
[291] => 6.55
[292] => 6.6
[293] => 6.65
[294] => 6.7
[295] => 6.75
[296] => 6.8
[297] => 6.85
[298] => 6.9
[299] => 6.95
[300] => 7
[301] => 7.05
[302] => 7.1
[303] => 7.15
[304] => 7.2
[305] => 7.25
[306] => 7.3
[307] => 7.35
[308] => 7.4
[309] => 7.45
[310] => 7.5
[311] => 7.55
[312] => 7.6
[313] => 7.65
[314] => 7.7
[315] => 7.75
[316] => 7.8
[317] => 7.85
[318] => 7.9
[319] => 7.95
[320] => 8
)


That would be because floating point numbers are not precise and not all numbers can be represented exactly using floats.


You may want to try a range between 800 and -800 with steps of 5 and then add in the decimal points / divide by 100 afterward.


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
<?php
for ($i = 0; $i < 9.99; $i += .1) {}
echo sprintf("%.17f", $i);
^D
9.99999999999998046

while:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜