Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <math.h>
- #include <stdbool.h>
- /*************************/
- void decToIEEE()
- {
- /* declare local variables */
- float decimal;
- int sign;
- int exponent;
- int mantissa = 0;
- int ieee = 0;
- /* prompt for floating point decimal number */
- printf("\nEnter Decimal Representation: ");
- scanf("%f", &decimal);
- /* Check for 0--if so, print result */
- if (decimal == 0.0)
- printf("\n\nSign: 0\nBiased Exponent: 00000000\nMantissa: 0000000000000000000000\nIEEE-754: 0000 0000\n\n");
- else {
- /* Print sign: if number>0, sign is 0, else 1 */
- sign = (decimal > 0) ? 0 : 1;
- printf("\n\nSign: %d\n", sign);
- /* take absolute value of number before generating significand */
- decimal = (decimal < 0) ? decimal*-1 : decimal;
- /* Normalize number:
- while number >2, divide by 2, increment exponent
- while number <1, multiply by 2, decrement exponent
- */
- exponent = 0;
- while(decimal >= 2) {
- decimal = decimal / 2.0;
- exponent++;
- }
- while(decimal < 1) {
- decimal *= 2.0;
- exponent--;
- }
- /* Bias exponent by 127 and print each bit in binary with 8-iteration for-loop*/
- exponent += 127;
- printf("Exponent: ");
- float temp_exp = exponent;
- for (int i = 0; i < 8; i++) {
- if (temp_exp > 128) {
- printf("1");
- temp_exp -= 128;
- }
- else
- printf("0");
- temp_exp *= 2;
- }
- /* Hide 1 and print significand in binary with 23-iteration for-loop*/
- decimal -= 1; // decrement to remove the 1
- printf("\nMantissa: ");
- float temp_dec = decimal;
- for (int i = 0; i < 23; i++) {
- if (temp_dec < 0.5) {
- printf("0");
- temp_dec *= 2;
- }
- else {
- printf("1");
- temp_dec = temp_dec*2 -1;
- }
- }
- /* Print IEEE-754 representation */
- ieee = sign * pow(2,31) + exponent*pow(2,23) + decimal*pow(2,23);
- printf("\nIEEE format: %x", ieee);
- }
- return;
- }
- /***********************************************************************/
- void ieeeToDec()
- {
- /* declare local variables */
- int ieee_number;
- /* prompt for IEEE-754 representation */
- printf("\nEnter IEEE-754 Representation: \n");
- scanf("%x", &ieee_number);
- /* check for special cases: 0, -0, +infinity, -infinity, NaN,
- if so, print and return */
- if (ieee_number == 0x00000000) // 0
- printf("Special case: +0\n");
- else if (ieee_number == 0x80000000) // -0
- printf("Special case: -0\n");
- else if(ieee_number == 0x7F800000) // + inf
- printf("Special case: Positive Infinity\n");
- else if(ieee_number == 0xFF800000) // - inf
- printf("Special case: Negative Infinity\n");
- else if ((ieee_number & 0x7FFFFFFF) > (0x7F800000)) // NaN, force the first number to be a 0
- printf("Special case: NaN\n");
- else {
- // FOR OTHER NUMBERS, CHECK POSITIVE OR NEGATIVE AND VALUES WITH MASKING: USING '&' WITH 0's
- /* Mask sign from number: if sign=0, print "+", else print "-" */
- /* Mask biased exponent and significand from number */
- int exp = ((ieee_number & 0x7FFFFFFF) >> 23) - 127;
- float mantissa = ((ieee_number << 9) * pow(2,-32));
- if (ieee_number >> 31 == 0) {
- printf("\nSign: +");
- }
- else {
- printf("\nSign: -");
- mantissa *= -1;
- }
- /* If biased exponent=0, number is denormalized with unbiased exponent of -126,
- print denormalized number as fraction * 2^(-126), return */
- if (exp == -127) {
- printf("\nUnbiased Exponent: -126\nDenormalized Decimal: %f*2^(-126)", mantissa);
- }
- else {
- /* Unbias exponent by subtracting 127 and print */
- printf("\nUnbiased Exponent: %d", exp);
- /* Add hidden 1 and print normalized decimal number */
- printf("\nNormalized decimal: %f", mantissa + 1);
- /* Print decimal number */
- printf("\nDecimal: %f", (mantissa + 1)*pow(2, exp));
- }
- }
- return;
- }
- int main()
- {
- /* declare local variables */
- int selection;
- bool exit = false;
- while(!exit) {
- printf("\n\nFloating-point conversion: \n__________________________\n");
- printf("1) Decimal to IEEE-754 conversion \n2) IEEE-754 to Decimal conversion \n3) Exit\n\nEnter Selection: ");
- scanf("%d", &selection);
- switch (selection) {
- case 1: decToIEEE(); break;
- case 2: ieeeToDec(); break;
- case 3: exit = true; break;
- }
- }
- /* until user chooses to quit, prompt for choice and select appropriate function */
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement