简体   繁体   English

十进制到 IEEE 754 使用 C 的单精度 IEEE 754 代码

[英]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.我们在课堂上有一个作业,使用 c 从十进制转换为单精度,我完全迷失了。

This is the assignment:这是任务:

The last part of this lab involves coding a short c algorithm.本实验的最后一部分涉及编写一个简短的 c 算法。 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.每个学生都必须创建一个程序,将科学记数法浮点数作为输入并以二进制和十六进制打印它的 IEE754 单精度值。

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).程序的输出必须等于以下内容: “引入十进制浮点数(科学记数法):十进制浮点数 1.234500e-04 是十六进制的 3901725B(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.到目前为止我们学到的#includes 是:stdio.h、math.h 和string.h,所以我认为我们不允许使用任何其他包含。 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.此外,我们还没有学习structunion或其中任何一个,因为我在其他示例中看到了这些,但我什么都不明白。

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:由于 OP 只要求提供指南:

  1. gets a scientific notation floating point number得到一个科学记数法浮点数

Research scanf() and the "%f" specifier研究scanf()"%f"说明符

  1. prints it´s IEE754 single precision value in ... hexadecimal以 ... 十六进制打印它的 IEE754 单精度值

Research type punning a float into uint32_t .研究类型float转换为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.查看可以将 unsigned int 位值复制为浮点数,但浮点值未正确返回给调用者函数并避免指针技巧。 See What is the strict aliasing rule?请参阅什么是严格的别名规则?

Research printf() and the "%x" specifier.研究printf()"%x"说明符。

  1. prints ... in ... binary打印 ... in ... 二进制

There a many ways to Is there a printf converter to print in binary format?很多方法可以打印二进制格式的 printf 转换器吗? . .
Some are very general (base, 2,3,10,16,36...)有些非常一般(基数,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.对于 1,不清楚您是打算自己编写代码来执行此操作还是使用库例程。 For library routines, use scanf (to read from input) or sscanf (to read from an array of char ).对于库例程,使用scanf (从输入读取)或sscanf (从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.例如,对于“1.234500e-04”,您会找到指数“-04”,将其转换为int ,然后将 1 乘以pow(10, -5) ,将 2 乘以pow(10, -6) ,依此类推,然后添加产品。

For 2, print using printf .对于 2,使用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).对于 3,使用memcpy将字节从float复制到uint32_t (查找标头<stdint.h> )并使用printf打印其字节(查找标头<inttypes.h>以获取要使用的正确格式说明符) . 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.或者,使用memcpy将字节从float复制到char数组中,然后使用printf和打印十六进制的格式说明符打印char

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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