h******3 发帖数: 351 | 1 Given a (decimal - e.g. 3.72) number that is passed in as a string, print
the binary representation. If the number can not be represented accurately
in binary, print “ERROR”
It's a really silly because I cant see the point.
3.72 -> 11.1001000 Is the purpose? |
v***n 发帖数: 5085 | |
c****p 发帖数: 6474 | 3 你这个是BCD,不是binary representation。
这师实际上是数制转换的问题。
比如说(0.5)10 = (0.1)2,
(0.625)10 = (0.101)2
……
这样。
【在 h******3 的大作中提到】![](/moin_static193/solenoid/img/up.png) : Given a (decimal - e.g. 3.72) number that is passed in as a string, print : the binary representation. If the number can not be represented accurately : in binary, print “ERROR” : It's a really silly because I cant see the point. : 3.72 -> 11.1001000 Is the purpose?
|
c****p 发帖数: 6474 | 4 3.72这个数就应该报error,因为不能被有限位二进制数精确表示。
【在 h******3 的大作中提到】![](/moin_static193/solenoid/img/up.png) : Given a (decimal - e.g. 3.72) number that is passed in as a string, print : the binary representation. If the number can not be represented accurately : in binary, print “ERROR” : It's a really silly because I cant see the point. : 3.72 -> 11.1001000 Is the purpose?
|
c*********t 发帖数: 2921 | 5 是不是先调用 float value = atof("3.72"),变成浮点数。
然后把浮点数value按照它在内存中的二进制打印出来?
就是把value弄成整数,然后写一个打印整数二进制的函数。
int i = *(int *)&value;
mybinprint(int i);
【在 h******3 的大作中提到】![](/moin_static193/solenoid/img/up.png) : Given a (decimal - e.g. 3.72) number that is passed in as a string, print : the binary representation. If the number can not be represented accurately : in binary, print “ERROR” : It's a really silly because I cant see the point. : 3.72 -> 11.1001000 Is the purpose?
|
c****p 发帖数: 6474 | 6 这是一个思路,但是涉及到浮点数的存储方式。
如果假设IEEE 754标准的话,需要:
1.算出指数(以决定小数点的位置),涉及位操作和减法。
2.算出底数,涉及位操作,并且要把hiden leading 1也考虑进来。
综上,把二进制形式表示出来还是有可能的。
但是很难解决报error的问题。
3.72这个数就应该报error的,而且存成浮点数的值就已经和3.72有误差了,
要从内存中的浮点数表示判断是不是精确表示很困难。
【在 c*********t 的大作中提到】![](/moin_static193/solenoid/img/up.png) : 是不是先调用 float value = atof("3.72"),变成浮点数。 : 然后把浮点数value按照它在内存中的二进制打印出来? : 就是把value弄成整数,然后写一个打印整数二进制的函数。 : int i = *(int *)&value; : mybinprint(int i);
|
s**x 发帖数: 405 | 7 represent the input as a rational number first (n / 10^k), then simplify by
eliminating the gcd. you have an accurate binary representation if and only
if the resulting denominator is a power of 2
【在 h******3 的大作中提到】![](/moin_static193/solenoid/img/up.png) : Given a (decimal - e.g. 3.72) number that is passed in as a string, print : the binary representation. If the number can not be represented accurately : in binary, print “ERROR” : It's a really silly because I cant see the point. : 3.72 -> 11.1001000 Is the purpose?
|
c****p 发帖数: 6474 | 8 我的想法:
背景知识:一个十进制有理数如果能被二进制精确表示(即可用有限位二进数表示),
那么它必须能够化简成n/2^k的形式(n,k为整数)。
(这个道理同一个有限十进制数能被表示成n/10^k的形式一样。)
所以有限位十进制小数未必能被表示成有限位二进制数,
比如0.1 = 1/10。
所以对于一个十进制小数(整数部分就不用说了),我们先把它表示成a/10^b的形式,比
如0.123 = 123/10^3,
对于字符串输入,很容易得到a和b;
然后求c = GCD(a,10^b),即两者最大公约数;
将a和10^b同除以c,并检查d = 10^b / c是否是2的整数幂;
若是,则将a/c输出;否则报ERROR;
if (d & (d-1))
{
printf("ERROR\n");
}
else
{
PRINT_BINARY_STRING(a/c);
}
【在 h******3 的大作中提到】![](/moin_static193/solenoid/img/up.png) : Given a (decimal - e.g. 3.72) number that is passed in as a string, print : the binary representation. If the number can not be represented accurately : in binary, print “ERROR” : It's a really silly because I cant see the point. : 3.72 -> 11.1001000 Is the purpose?
|
c****p 发帖数: 6474 | 9 hands....
by
only
【在 s**x 的大作中提到】![](/moin_static193/solenoid/img/up.png) : represent the input as a rational number first (n / 10^k), then simplify by : eliminating the gcd. you have an accurate binary representation if and only : if the resulting denominator is a power of 2
|