2012-03-14 150 views

回答

2
floor(0.53124 * 16 + 0.5)/16 
floor(0.46875 * 16 + 0.5)/16 

floor(x * 16 + 0.5)/16 
1

我想,你可以用16乘以由16小白代码中调用round(double x)和除法:

double x; 
x=x*16; 
x=round(x); 
x=x/16; 

和一行代码:

double x; 
x=round(x*16)/16; 
+0

我不知道该怎么做()在C中。( – user1128265 2012-03-14 02:44:10

+0

#include 2012-03-14 08:14:43

1

C代码:

answer = (int) ((x + 1.0/32.0) * 16)/16.0; 

Python验证:

>>> int(((.53124 + 1.0/32) * 16))/16.0 
0.5 
>>> int(((.46875 + 1.0/32) * 16))/16.0 
0.5 
>>>