dyc0113

浮点数转换成二进制

char *ToBinary(char *buffer,float x)
{
	int k=0;
	if(x<0)
	{
		buffer[k++]=\'-\';
		x=-x;
	}
	int a=(int)x;
	float b=x-a;
	char temp[40];
	int i=0;
	if(a==0)
		temp[i++]=\'0\';
	while(a!=0)
	{
		temp[i++]=a%2+48;
		a=a/2;
	}
	temp[i]=\'\0\';
	i=i-1;
	while(i>=0)buffer[k++]=temp[i--];
	if(b==0)
	{
		buffer[k]=\'\0\';
		return buffer;
	}
	buffer[k++]=\'.\';
	for(i=0;i<23;i++)
	{
		int t=b*2;
		buffer[k++]=t+48;
		b=b*2-t;
	}
	if(b!=0)
		cout<<"ERROR"<<endl;
	buffer[k++]=\'\0\';
	return buffer;
}

  

分类:

技术点:

相关文章:

  • 2021-10-10
  • 2021-09-27
  • 2021-09-27
  • 2021-09-27
  • 2021-11-21
  • 2021-07-13
  • 2021-09-27
猜你喜欢
  • 2021-11-28
  • 2021-12-02
  • 2021-09-27
  • 2021-11-02
  • 2021-09-27
相关资源
相似解决方案