Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hi! I have a problem with my C program. I have an exercise (see below) and the code (see below as well). Now my problem

Hi!

I have a problem with my C program. I have an exercise (see below) and the code (see below as well).

Now my problem is that the "%b" conversion specifier is not implemented in the C standard and I have to write a function that prints the uint64_t values in binary form.

The only thing that is not working, is to print the mantissa in binary form. So something in my function void print_mantissa(uint64_t int_mantissa) {} has to be incorrect.

Can you help me to correct the mistake?

I compile with gcc -Wall -Wextra -Werror -Wpedantic -std=c18

Thank you very much in advance!

The exercise:

Create a program that outputs the IEEE 754 double-precision binary representation for a passed floating-point number. The floating point number is passed as a command line parameter, where a valid floating point number has the following parts: (1) optional plus or minus sign (2) a non-empty sequence of decimal digits which optionally contains a decimal point (3) optionally an e or E with an optional plus or minus sign and a non-empty sequence of decimal digits.

The user passes a floating point number as command line parameter. (1) If no parameter is passed, the program prints the text "usage: ./s09e02 " on the standard error output stderr and terminates with the return value EXIT_FAILURE. (2) If an error occurs when converting the string into a double precision floating point number or if the whole parameter cannot be transformed into a number, the error message "ERROR: is not a valid double! Extracted double component: Remaining: " is output on the standard error output stderr and the program terminates with the return value EXIT_FAILURE. The placeholder is replaced by the command line parameter, is replaced as a decimal number with six decimal places by the part of the command line parameter which could be successfully transformed into a floating point number, and is replaced by the remaining part which could no longer be transformed into a floating point number.

The text "Number: Binary representation: " is printed on the standard output, where the placeholder is replaced by the floating point number as decimal number in exponential representation with six decimal places, which was passed as command line parameter.

The sign (sign), exponent (exponent) and mantissa (mantissa) components of the floating point number are calculated and output in the format " " with three spaces before sign and one space each before exponent and mantissa on the standard output.

The text "sign exponent mantissa " is printed on the standard output.

The program is terminated with EXIT_SUCCESS.

Tip:

You can use a union consisting of a double and a uint64_t component to get the bit representation of a double precision floating point number.

The strtod function can be helpful in converting the passed parameter.

Keep exactly to the format of the examples and do not output any additional text!

The output should look like this:

./s09e02 4.1 Number: 4.100000e+00 Binary representation: 0 10000000001 0000011001100110011001100110011001100110011001100110 sign exponent mantissa

My code:

#include  #include  #include  #include  #define ERROR_MSG_LEN 100 union double_to_uint64 { double d; uint64_t u; }; void print_uint64_t_to_binary(uint64_t int_number) { int a[64]; int i; // loop to calculate and store the binary format for (i = 0; int_number > 0; i++) { a[i] = int_number % 2; int_number = int_number / 2; } // loop to print the binary format for (i = i - 1; i >= 0; i--) { printf("%d", a[i]); } } void print_mantissa(uint64_t int_mantissa) { int a[64]; int i; // loop to calculate and store the binary format for (i = 0; int_mantissa > 0; i++) { a[i] = int_mantissa % 2; int_mantissa = int_mantissa / 2; } // int array to int size_t length = sizeof(a) / sizeof(*a); int mantissa2 = int_mantissa; size_t n = 0; int val; while(n < length) { val = a[n]; while(val !=0) { val = val/10; mantissa2 = mantissa2 * 10; } mantissa2 = mantissa2 + a[n]; n++; } //print the mantissa with 52 places printf(" %0*d ", 52, mantissa2); } int main(int argc, char *argv[]) { if (argc != 2) { fprintf(stderr, "usage: %s  ", argv[0]); return EXIT_FAILURE; } char *endptr; double number = strtod(argv[1], &endptr); if (argv[1] == endptr || strlen(endptr) > 0) { char error_msg[ERROR_MSG_LEN]; snprintf(error_msg, ERROR_MSG_LEN, "ERROR: %s is not a valid double! Extracted double component: %.6f Remaining: %s", argv[1], number, endptr); fprintf(stderr, "%s ", error_msg); return EXIT_FAILURE; } union double_to_uint64 du; du.d = number; uint64_t bits = du.u; printf("Number: %.6e Binary representation: ", number); int sign = (bits >> 63) & 1; uint64_t exponent = (bits >> 52) & 0x7FF; uint64_t mantissa = bits & 0xFFFFFFFFFFFFF; printf(" %d ", sign); print_uint64_t_to_binary((exponent)); print_mantissa(mantissa); printf(" "); printf("sign exponent mantissa "); return EXIT_SUCCESS; } 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Transact SQL Cookbook Help For Database Programmers

Authors: Ales Spetic, Jonathan Gennick

1st Edition

1565927567, 978-1565927568

More Books

Students also viewed these Databases questions