L - Floating-Point Numbers

L - Floating-Point Numbers

Here Insert Picture Description
Here Insert Picture Description
Effect Title : Title us explain the floating point stored in a computer is divided into three parts, the first symbol represents a negative (0 positive, 1 negative), the second part is the mantissa M, third-order part is E code, binary representation in the computer by M and E, if the number of bits when different, they can represent a maximum value is different. We will form the subject requires exponential input decimal number into a binary representation, the mantissa M and the exponent determined E.

It draws on the ideas of others, to write down.
Here Insert Picture Description
Here Insert Picture Description
code show as below.

#include<bits/stdc++.h>
using namespace std; 
int i, j;
double a[10][31],m[10],e[31];
void init() 
{
	for (i = 0;i < 10;i++)
		m[i] = 1 - pow(0.5, (i + 1));//计算m 
	for (j = 1;j < 31;j++)
		e[j] = pow(2,j) - 1;//计算e 
	for (i = 0;i < 10;i++)
		for (j = 1;j < 31;j++)
			a[i][j] = log10(m[i]) + e[j] * log10(2);//枚举 
}
int main()
{
	init();
	double A,t;//A为t的小数部分,t为十进制转化为二进制取对数后的值 
	int B;//B为t的整数部分 
	char s[25];
	while(cin>>s)
	{
		for (i = 0;;i++)
			if (s[i] == 'e')
			{
				s[i] = ' ';
				break;
			}//将e置空是为了方便读入A和B
		sscanf(s, "%lf %d", &A, &B);
		if (!A&&!B) break;
		t = log10(A)+B;
		for (i = 0;i < 10;i++)
			for (j = 1;j < 31;j++)
				if (fabs(a[i][j] - t) < 1e-4) goto loop;//允许实际误差<10^(1e-4)
		loop:
			cout<<i<<" "<<j<<endl; 
	}
	return 0;
}
Published 18 original articles · won praise 14 · views 370

Guess you like

Origin blog.csdn.net/weixin_45750972/article/details/103213918