Why does .5 become 1.0 when rounding? Shouldn't it be 0.5?
我正在尝试学习C。
我正在使用Mac xCode v.4.1.1
在MacBook Pro上(13英寸,2012年中)
处理器:2.9 GHz双核Intel Core i7
内存:8GB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | while (c < .9) { std::cin >> c; std::cout <<"\ c =" << c << '\ '; array.push_back(c); c = round ((c *10)/10); std::cout <<"After rounding, c =" << c << '\ '; } std::cout <<"\ Size of the vector \'array\' =" << array.size() << '\ '; for (int i = 0; i < array.size(); i ++ ) { std::cout <<"\ array[" << i <<"] =" << array[i] << std::endl; } |
我的结果:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | .1 c = 0.1 After rounding, c = 0 .2 c = 0.2 After rounding, c = 0 .3 c = 0.3 After rounding, c = 0 .4 c = 0.4 After rounding, c = 0 .5 c = 0.5 After rounding, c = 1 Size of the vector 'array' = 5 array[0] = 0.1 array[1] = 0.2 array[2] = 0.3 array[3] = 0.4 array[4] = 0.5 Program ended with exit code: 0 |
这就是
Computes the nearest integer value to arg (in floating-point format), rounding halfway cases away from zero, regardless of the current rounding mode.
您可能想看一下