浮点数到二进制 [英] Floating point number to binary

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

问题描述




我想使用C程序将浮点数(或十进制数字表示

123.456)转换为二进制表示法。有人可以帮我吗

用它吗?


谢谢

Gaurav

-

comp.lang.c.moderated - 审核地址: cl**@plethora.net

解决方案

Gaurav Verma写道:

我想转换浮点数(或十进制数字说
123.456 )使用C程序的二进制表示法。有人可以帮我吗?




我不认为你想要做的很清楚。例如,你认为小数点的二进制表示是什么?
?你是否想要学习一些东西,而不是实际上做*做某事?

如果是这样,你想学什么?


ve ********** @ gmail.com (Gaurav Verma)写道:

我想使用C程序将浮点数(或十进制数字表示
123.456)转换为二进制表示法。有人可以帮我吗?




如果你想要浮点数的基础IEEE表示

数你可以使用联合来访问各个字节。


例如联盟{double d; struct {unsigned char byte [8]; ieee; } $;


-

Brian Gough


网络理论有限公司,

出版GCC简介 --- http://www.network-theory.co.uk/ gcc /


Brian Gough写道:

ve ********** @ gmail.com (Gaurav Verma)写道:

我想要使用C程序将浮点数(或十进制数字表示为
123.456)转换为二进制表示法。有人可以用它来帮助我吗?



如果你想要一个浮点数的基础IEEE表示,你可以使用一个联合来访问各个字节。

例如联盟{double d; struct {unsigned char byte [8];} ieee; } u;




不,你不能,至少不可移植。你可以做的是设置一个

指针指向存储并通过它访问它。


#include< stdio.h>


int main(无效)

{

双d;

unsigned char * p;

unsigned int i;


d = 123.4; / *或其他* /

p =(unsigned char *)(& d);

for(i = 0; i< sizeof(double); i ++) {

printf("%2x",p [i]);

/ *用p [i] * /
$ b做其他事情$ b}

putchar(''\ n'');

返回0;

}


-

这是一个很好的答案。这是偏离主题的,它是不正确的,

并且它没有回答这个问题。 - 理查德希思菲尔德


我支持红袜队和击败洋基队的任何球队


Hi,

I want to convert a floating point number (or a decimal number say
123.456) into binary notation using a C program. Can somebody help me
out with it?

Thanks
Gaurav
--
comp.lang.c.moderated - moderation address: cl**@plethora.net

解决方案

Gaurav Verma writes:

I want to convert a floating point number (or a decimal number say
123.456) into binary notation using a C program. Can somebody help me
out with it?



I don''t think it is very clear what you are trying to do. For example, what
do you think the binary representation of a decimal point is? Are you
trying to learn something, as opposed to than actually *doing* something?
If so, what are you trying to learn?


ve**********@gmail.com (Gaurav Verma) writes:

I want to convert a floating point number (or a decimal number say
123.456) into binary notation using a C program. Can somebody help me
out with it?



If you want the underlying IEEE representation of a floating point
number you can use a union to access the individual bytes.

e.g. union { double d; struct { unsigned char byte[8]; } ieee ; } u;

--
Brian Gough

Network Theory Ltd,
Publishing "An Introduction to GCC" --- http://www.network-theory.co.uk/gcc/


Brian Gough wrote:

ve**********@gmail.com (Gaurav Verma) writes:

I want to convert a floating point number (or a decimal number say
123.456) into binary notation using a C program. Can somebody help
me out with it?



If you want the underlying IEEE representation of a floating point
number you can use a union to access the individual bytes.

e.g. union {double d; struct { unsigned char byte[8];} ieee ; } u;



No you can''t, at least not portably. What you can do is set up a
pointer to the storage and access it via that.

#include <stdio.h>

int main(void)
{
double d;
unsigned char *p;
unsigned int i;

d = 123.4; /* or whatever */
p = (unsigned char *)(&d);
for (i = 0; i < sizeof(double); i++) {
printf("%2x ", p[i]);
/* do other things with p[i] */
}
putchar(''\n'');
return 0;
}

--
"This is a wonderful answer. It''s off-topic, it''s incorrect,
and it doesn''t answer the question." -- Richard Heathfield

"I support the Red Sox and any team that beats the Yankees"


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

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