#include <stdio.h>
int main (int argc, char *argv[]) {
float f = 2.03F;
unsigned char *c;
c = (unsigned char *) &f;
for (int i = 0; i < 4; ++i)
printf("%d ", c[i]);
printf("\n");
double d = 2.03;
c = (unsigned char *) &d;
for (int i = 0; i < 8; ++i)
printf("%d ", c[i]);
printf("\n");
}
3条答案
按热度按时间vsnjm48y1#
Same results from C:
Output:
or Perl:
That's how floating point numbers are represented internally. See Floating-point aritmetic .
A nice demo to experiment with the values here .
mcvgt66p2#
在Erlang中,float二进制的默认大小是64,因此输出存储为8 X 8 = 64字节。当float的默认大小为32时,它存储为4 X 8 = 32字节。下面的代码显示了如何从字节中提取位。
上述输出中的前8位是“0,1,0,0,0,0,0,0”,当转换为整数时给出以下输出。
类似地,接下来的8位给出0,第三组8位给出61,等等。
ajsxfq5m3#
which is why you don't just get:
I am new to erlang
Then the bit syntax for floating point numbers is the least of your worries. If you understand it for integers, then move on.