打印浮点数表示 [英] printing bit representation of floats

查看:67
本文介绍了打印浮点数表示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习在我的电脑上使用的浮点格式

(苹果mac所以powerpc)


我''我写了一个函数来打印浮点数中的位,看看

浮点数是如何表示的,我还有一个软件程序员的'b $ b计算器叫做BinCalc,它显示了任何数字的位数。


我的代码和计算器显示的位,对于

相同的值,不匹配。例如对于数字1.0,我的代码说:

10111111_00000000_00000000_10000000


和计算器说:

00111111_10000000_00000000_00000000

和值1.6我的代码打印:

10111111_11001101_11001100_11001100

和计算器说:

00111111_11001100_11001100_11001101

'用于打印浮点位的代码在下面。


第一个明显的区别是左位,高位。

那个'如果价值'是负面的还是正面的,那么这个标志

有点对吗?所以看起来我的代码确实有问题

因为上面两个例子中的值都不是负数但是我的代码中的两个打印输出都是左边的大多数位设置为1.并且你会认为软件计算器正在使用的格式与我的计算机使用的格式相同。
,所以两个陈述

应该算上我想到的。谁知道发生了什么?


谢谢,本。

#include< stdio.h>


void bitfloatprint(float f)

{

unsigned bytes = sizeof(float); //浮点数中的字节数

char bits; //将掩码移位的位数


while(bytes){//浮点数每个字节一个循环(HO到LO)

for (bits = 7; bits> = 0; bits--){

putchar(

(*((unsigned char *)& f + bytes)& 1<<> == 0?''0'':''1''

);

//在上面线投射,以便+1意味着加上

//一个字节而不是一个浮点数

}

if(bytes!= 1)

putchar(''_'');

bytes--;

}

putchar(''\ n'');

}


int main(无效)

{

float f = 1.0;

bitfloatprint(f);

返回0;

}

解决方案

< BLOCKQUOTE>啊!典型。对不起,忘了这个。在我发布之后就发现了这件事。

主循环不是从HO字节到LO字节,就像它应该是b / b
实际上是另一种方式。所以它应该是:

#include< stdio.h>


void bitfloatprint(float f)

{

unsigned字节= 0;

char位;


while(bytes< sizeof(float)){

for(bits = 7; bits> = 0; bits--){

putchar(

((*((unsigned char *)& f +字节))& 1<< bits)== 0?''0'':''1''

);

}

if(bytes!= 3)

putchar(''_'');

bytes ++;

}

putchar(''\ n'');

}


int main(无效)

{

浮动f = 1.6;

bitfloatprint(f);

返回0;

}


ben写道:

我正在学习在我的电脑上使用的浮点格式
(苹果mac所以powerpc)

我写了一个函数来打印浮点数,看看
是如何浮动的代表我也有一个名为BinCalc的软件程序员的计算器,它显示了任何数字的位。

我的代码和计算器显示的位,用于
相同的价值,不匹配。



/ *没有理由认为你的计算器和你的Apple

*有浮点数的相同表示。尝试使用

*代码输出。请注意,我的实现的输出是

*与您报告的内容非常不同。 * /

#include< stdio.h>

#include< string.h>


#define showhex( x,s)\

{\

unsigned char c [sizeof x]; \

size_t i; \

memcpy(c,& x,sizeof x); \

printf("%Lg(%s,size =%lu)as hex:\ n% 4s",\

(长双)x,s,\

(无符号长)sizeof x,""); \

for(i = 0; i< sizeof x; i ++)\

printf("%02x",c [i]); \

printf(" \ n"); \

}


int main(无效)

{

long double xl;

double x;

float xf;

xf = x = xl = 1.0;

printf([此实现的输出] \ n \ nn);

printf("对于1.0,Ben的程序会产生十六进制:

BF 00 00 80 \ n""和他的计算器:3F 00 00 80 \ n);

sho whex(xf," float");

showhex(x," double");

showhex(xl," long double");

xf = x = xl = 1.6;

printf(" \ n为1.6,Ben'的程序将以十六进制产生:"

BF CD CC CC \ n 和他的计算器:3F CC CC CD \ n;);

showhex(xf,float);

showhex(x,double, );

showhex(xl,long double);

返回0;

}


[此实现的输出]


对于1.0,Ben'的程序将以十六进制生成:BF 00 00 80

和他的计算器: 3F 00 00 80

1(浮动,尺寸= 4)为十六进制:

00 00 80 3f

1(双倍,尺寸= 8 )作为十六进制:
$ b $ 00 00 00 00 00 00 00 f0 3f

1(长双,大小= 12)为十六进制:

00 00 00 00 00 00 00 80 ff 3f 00 00

对于1.6,Ben'的程序将以十六进制产生:BF CD CC CC

和他的计算器:3F CC CC CD

1.6(浮点数,尺寸= 4)十六进制:

cd cc cc 3f

1.6(双倍,尺寸= 8)作为十六进制:

9a 99 99 99 99 99 f9 3f

1.6(长双倍,大小= 12)为十六进制:

00 d0 cc cc cc cc cc cc ff 3f 00 00


- -

"如果你想通过groups.google.com发布一个后续内容,请不要使用

破坏的回复链接在文章的底部。点击

" show options"在文章的顶部,然后点击

回复在文章标题的底部。 - Keith Thompson


文章< 36 ************* @ individual.net> ;, Martin Ambuhl

< ma ***** @ earthlink.net>写道:

ben写道:

我正在学习在我的电脑上使用的浮点格式
(apple mac所以powerpc)

我已经编写了一个函数来打印浮点中的位,看看如何表示浮点数,我还有一个软件程序员的
计算器叫BinCalc,显示任意数字的位。

我的代码和计算器显示的位,对于
相同的值,不匹配。

/ *没有理由认为您的计算器和Apple
*具有相同的浮点数表示。尝试
*代码输出。请注意,我的实现的输出与您报告的内容非常不同。 * /




Martin,

你可能已经看过
,我在代码中犯了一个错误并且是正确的

假设软件计算器的格式与

计算机的格式相同(这个计算器的设计确实有意义

用于在此计算机上进行编程,因此您希望他们将使用

相同的表示形式)。这是你的代码的输出:


我的输出:

3f 80 00 00

3f f0 00 00 00 00 00 00

3f f0 00 00 00 00 00 00


3f cc cc cd

3f f9 99 99 99 99 99 9a

3f f9 99 99 99 99 99 9a

您的输出:

00 00 80 3f

00 00 00 00 00 00 f0 3f
$ b $ 00 00 00 00 00 00 00 80 ff 3f 00 00


cd cc cc 3f

9a 99 99 99 99 99 f9 3f

00 d0 cc cc cc cc cc cc ff 3f 00 00


(注意十六进制值你我在计算器中注明了

1.0的价值:3F 00 00 80略有错误。它是3F 80 00

00来自计算器)


float和double版本之间的唯一区别是endianess

- 我的大,你的很少。但是很长的双倍'差不多说

最少。我得到了这个警告:警告:使用'long double''

类型;它的大小可能会在未来版本中发生变化。我正在使用稍微旧的

编译器(2002),所以也许现在已经改变了。谁知道呢。


非常感谢,本。


i''m learning about the floating point format that''s used on my computer
(apple mac so powerpc)

i''ve written a function to print out the bits in a float to see how
floats are represented and i also have a software programmer''s
calculator called BinCalc which shows the bits of whatever number.

the bits that my code and the bits that the calculator show, for the
same value, don''t match. for example for the number 1.0 my code says:
10111111_00000000_00000000_10000000

and the calculator says:
00111111_10000000_00000000_00000000
and for the value 1.6 my code prints:
10111111_11001101_11001100_11001100

and the calculator says:
00111111_11001100_11001100_11001101

the code that''s used to print the float bits is below.

the first obvious difference is the left bit, the high order bit.
that''s the bit that says if the value''s negative or posative, the sign
bit right? so it really looks like there''s something wrong with my code
becuase neither values in the above two examples are negative but both
print outs from my code has the left most bit set to 1. and you''d have
thought that the format that the software calculator is using would be
the same format that my computer''s using, so the two representations
should tally i''d have thought. anyone know what''s going on?

thanks, ben.
#include <stdio.h>

void bitfloatprint(float f)
{
unsigned bytes = sizeof(float); // number of bytes in a float
char bits; // number of bits to shift mask over by

while( bytes ) { // one loop per byte in the float (HO to LO)
for( bits = 7; bits >= 0; bits-- ) {
putchar(
( *((unsigned char *)&f + bytes) & 1 << bits ) == 0 ? ''0'' : ''1''
);
// casting in above line so that +1 means plus
// one byte rather than plus one float
}
if( bytes != 1 )
putchar(''_'');
bytes--;
}
putchar(''\n'');
}

int main(void)
{
float f = 1.0;
bitfloatprint(f);
return 0;
}

解决方案

ahh! typical. sorry, forget this. sussed it just after i posted. the
main while loop wasn''t going from HO byte to LO byte like it should
have been -- it was actually going the other way. so it should be:
#include <stdio.h>

void bitfloatprint(float f)
{
unsigned bytes = 0;
char bits;

while( bytes < sizeof(float) ) {
for( bits = 7; bits >= 0; bits-- ) {
putchar(
( (*((unsigned char *)&f + bytes)) & 1 << bits ) == 0 ? ''0'' : ''1''
);
}
if( bytes != 3 )
putchar(''_'');
bytes++;
}
putchar(''\n'');
}

int main(void)
{
float f = 1.6;
bitfloatprint(f);
return 0;
}


ben wrote:

i''m learning about the floating point format that''s used on my computer
(apple mac so powerpc)

i''ve written a function to print out the bits in a float to see how
floats are represented and i also have a software programmer''s
calculator called BinCalc which shows the bits of whatever number.

the bits that my code and the bits that the calculator show, for the
same value, don''t match.


/* There is no reason to suppose that your calculator and your Apple
* have the same representation for floating point numbers. Try the
* following code out. Note that the output of my implementation is
* very different from what you report. */
#include <stdio.h>
#include <string.h>

#define showhex(x, s)\
{\
unsigned char c[sizeof x];\
size_t i;\
memcpy(c,&x,sizeof x);\
printf("%Lg (%s, size = %lu) as hex: \n%4s",\
(long double)x, s, \
(unsigned long) sizeof x, "");\
for (i = 0; i < sizeof x; i++)\
printf("%02x ", c[i]);\
printf("\n");\
}

int main(void)
{
long double xl;
double x;
float xf;
xf = x = xl = 1.0;
printf("[Output for this implementation]\n\n");
printf("For 1.0, Ben''s program would yield in hex: "
"BF 00 00 80\n" "and his calculator: 3F 00 00 80\n");
showhex(xf, "float");
showhex(x, "double");
showhex(xl, "long double");
xf = x = xl = 1.6;
printf("\nFor 1.6, Ben''s program would yield in hex: "
"BF CD CC CC\n" "and his calculator: 3F CC CC CD\n");
showhex(xf, "float");
showhex(x, "double");
showhex(xl, "long double");
return 0;
}

[Output for this implementation]

For 1.0, Ben''s program would yield in hex: BF 00 00 80
and his calculator: 3F 00 00 80
1 (float, size = 4) as hex:
00 00 80 3f
1 (double, size = 8) as hex:
00 00 00 00 00 00 f0 3f
1 (long double, size = 12) as hex:
00 00 00 00 00 00 00 80 ff 3f 00 00

For 1.6, Ben''s program would yield in hex: BF CD CC CC
and his calculator: 3F CC CC CD
1.6 (float, size = 4) as hex:
cd cc cc 3f
1.6 (double, size = 8) as hex:
9a 99 99 99 99 99 f9 3f
1.6 (long double, size = 12) as hex:
00 d0 cc cc cc cc cc cc ff 3f 00 00

--
"If you want to post a followup via groups.google.com, don''t use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson


In article <36*************@individual.net>, Martin Ambuhl
<ma*****@earthlink.net> wrote:

ben wrote:

i''m learning about the floating point format that''s used on my computer
(apple mac so powerpc)

i''ve written a function to print out the bits in a float to see how
floats are represented and i also have a software programmer''s
calculator called BinCalc which shows the bits of whatever number.

the bits that my code and the bits that the calculator show, for the
same value, don''t match.

/* There is no reason to suppose that your calculator and your Apple
* have the same representation for floating point numbers. Try the
* following code out. Note that the output of my implementation is
* very different from what you report. */



Martin,

as you''ve probably seen, i made a mistake in the code and was correct
to assume that the softaware calculator''s format is the same as the
computer''s format (which does make sense as that calculator is designed
for programming on this computer, so you''d hope they''d be using the
same representation). here''s the output from your code:

my output:
3f 80 00 00
3f f0 00 00 00 00 00 00
3f f0 00 00 00 00 00 00

3f cc cc cd
3f f9 99 99 99 99 99 9a
3f f9 99 99 99 99 99 9a

your output:
00 00 80 3f
00 00 00 00 00 00 f0 3f
00 00 00 00 00 00 00 80 ff 3f 00 00

cd cc cc 3f
9a 99 99 99 99 99 f9 3f
00 d0 cc cc cc cc cc cc ff 3f 00 00

(note the hex value you''d noted in the print statement for the value of
1.0 from my calculator: 3F 00 00 80 was slightly wrong. it was 3F 80 00
00 from the calculator)

the only difference between the float and double versions is endianess
-- mine big, yours little. but long double''s pretty different to say
the least. i got this warning for that: warning: use of `long double''
type; its size may change in a future release. i''m using a slightly old
compiler (2002) so maybe that has changed by now. who knows.

thanks a lot, ben.


这篇关于打印浮点数表示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆