简体   繁体   English

用于将浮点格式化为十进制数字的算法

[英]Algorithm(s) for formatting floating point to decimal number

Recently, I've studied the IEEE 754-1985 standard. 最近,我研究了IEEE 754-1985标准。 After reading some materials on the web, I think I've grasped the basic ideas underlie floating point numbers. 在阅读了网上的一些资料之后,我认为我已经掌握了浮点数基础的基本思想。 While, I have a question that can not be solved by searching the web. 同时,我有一个无法通过搜索网络解决的问题。 That is, how the computer system convert the floating point number(in binary form) to its decimal equivalent and print it on the screen as a string for our reading? 也就是说,计算机系统如何将浮点数(二进制形式)转换为等效的十进制数,并将其作为字符串打印在屏幕上以供阅读?

To be clear, for example, In C/C++ language, we just write: 为了清楚起见,例如,在C / C ++语言中,我们只写:

...
float x,y,z;
x=3.14;
y=2.71;
z=x+y;
printf("%f",z);

we will get 5.85 as a result on the screen. 屏幕上将显示5.85。 But how computer(or just the C language and its compiler) convert x,y to IEEE binary floating point form and after the calculation by CPU, convert the IEEE form back to the decimal string form? 但是,计算机(或仅C语言及其编译器)如何将x,y转换为IEEE二进制浮点数形式,并在CPU计算后将IEEE形式转换回十进制字符串形式?

Is there some algorithms and for this conversion? 是否有一些算法可以进行这种转换? By the way, is it hardware or software that does this trick? 顺便说一句,这是硬件还是软件?

Thank you! 谢谢! :-) :-)

you should have a look at the glibc , especially the printf function. 您应该看看glibc ,尤其是printf函数。 this is where the authoritative answer for at least one popular implementation lies. 这是至少一种流行实施方式的权威答案所在。 i doubt that it is too readable, though. 我怀疑它太可读了。

it is definitely done in software. 它绝对是用软件完成的。

http://www.gnu.org/software/libc/download.html http://www.gnu.org/software/libc/download.html

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

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