#include "ieee754.h"
#include <stdio.h>
#include <math.h>
//This program convert a floating number to its binary representation
(IEEE754) in computer memory
int main()
{
long double f, binaryTotal, binaryFrac = 0.0, frac, fracFractor = 0.1;
long int integer, binaryInt = 0;
long int p = 0, rem, temp;
printf("\nEnter floating number: ");
scanf("%Lf",&f);
//separate the integer part from the input floating number
integer = (int)f;
//separate the fractional part from the input floating number
frac = f - integer;
//loop to convert integer part to binary
while(integer != 0)
{
rem = integer % 2;
binaryInt = binaryInt + rem *pow(10,p);
integer = integer/2;
p++;
}
//loop to convert fractional part to binary
while(frac != 0)
{
frac = frac * 2;
temp = frac;
binaryFrac = binaryFrac + fracFractor * temp;
if (temp == 1)
frac = frac - temp;
fracFractor = fracFractor/10 ;
}
binaryTotal = binaryInt + binaryFrac;
printf("binary equivalent = %Lf\n", binaryTotal);
}
부동 소수점을 이진 표현 (64 비트)으로 변환하려고합니다. 이 코드는 작동하지만 완벽하지는 않습니다. 예를 들어 나는 0.575를 변환 할 때, 그것은 나에게 0.100100을 제공하지만이 웹 사이트 http://www.exploringbinary.com/floating-point-converter/를 사용하여 변환을 할 때 문제가 내 코드 수를 절단하는 무슨 이해하는 데 0.1001001100110011001100110011001100110011001100110011C에서 부동 소수점을 이진 표현 프로그램으로 변환
오른쪽 출력해야한다. 아무도 그것을 고칠 수있는 방법을 도와 줄래? 도와 주셔서 감사합니다. 이 작업을하지 않을 이유
컴파일 같은 간단한 것을 제언한다. –
@MichaelWalz 경고가 없습니다. http : // ideone.com/aIShip – mch
@mike'.' :' "% .20Lf"'가'0.10010011001100110011'을 인쇄 한 후에 원하는 자리수를 써야합니다. – mch