简体   繁体   中英

Decimal to IEEE 754 Single-precision IEEE 754 code using C

We have an assignment in class to convert from Decimal to single precision using c and I'm completely lost.

This is the assignment:

The last part of this lab involves coding a short c algorithm. Every student must create a program that gets a scientific notation floating point number as an input and prints it´s IEE754 single precission value both in binary and hexadecimal.

The ouput of the program must be equal to the following : “Introduce a float point number in decimal (scientific notation): The decimal float 1.234500e-04 is 3901725B in hexadecimal (IEEE 754).

The #includes we have learnt so far are: stdio.h, math.h and string.h, so I don't think we are allowed to use any other includes. Also we havnt learned struct or union or any of that yet, because I saw those in other examples and I didn't understand anything.

I would really appreciate it if someone can guide me through making this code!

guide me through making this code!

As OP only requested a guide:

  1. gets a scientific notation floating point number

Research scanf() and the "%f" specifier

  1. prints it´s IEE754 single precision value in ... hexadecimal

Research type punning a float into uint32_t . Review Could copy unsigned int bit values as float but float value not returned to the caller function correctly and avoid pointer tricks. See What is the strict aliasing rule?

Research printf() and the "%x" specifier.

  1. prints ... in ... binary

There a many ways to Is there a printf converter to print in binary format? .
Some are very general (base, 2,3,10,16,36...)

The assignment you describe has three parts:

  1. Examine the characters of the numeral and convert them into a floating-point number.

  2. Print the value of the floating-point number.

  3. Print the bytes of a floating-point number.

For 1, it is not clear whether you are intended to write code to do that yourself or to use a library routine. For library routines, use scanf (to read from input) or sscanf (to read from an array of char ). If you are to do it yourself, then, at the level you describe, I do not expect an exact solution is required. In this case, you can: Identify the power of ten corresponding to each digit, multiply the digit by the power of ten, and sum the products. For example, with “1.234500e-04”, you would find the exponent, “-04”, convert it to an int , then multiply 1 by pow(10, -5) , multiply 2 by pow(10, -6) , and so on, and then add the products.

For 2, print using printf .

For 3, use memcpy to copy the bytes from the float into a uint32_t (look up the header <stdint.h> ) and print its bytes with printf (look up the header <inttypes.h> for the correct format specifier to use). Alternatively, use memcpy to copy the bytes from the float into an array of char , then print the char using printf with a format specifier that prints hexadecimal.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM