我的浮点数到字符串的转换 [英] My float to string conversion

查看:132
本文介绍了我的浮点数到字符串的转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好

我正在尝试编写自己的"ftoa"函数,作为性能CString类(CFString)的一部分.由于我的目标是性能,因此我无法使用sprintf()或任何其他标准方法来完成这项工作.实际上,我应该使函数的性能与sprintf()和其他标准解决方案(例如CString :: Format())相匹配.

我实际上快完成了,但是最后一击似乎是最难的! :)
IEEE-754标准强加了一些情况,这些情况需要根据二进制指数的值将值提取到字符串中的方法不同.它们是:

Hi All

I''m trying to write my own "ftoa" function, as a part of my performance CString class (CFString). Since I aim performance I can''t use sprintf() or any other standard method to do the job. Actually I''m supposed to match the performance of my function against sprintf() and others standard solutions like CString::Format().

I''m actually almost done, but the last push seems to be the hardest! :)
The IEEE-754 standard imposes a few cases which require different approach in order to extract the value into string, according to the binary exponent''s value. They are:

if(exp > 31)
{
    //NO solution
}
else if(exp < -31)
{
    //Solved. Works twise more slowlly than sprintf().
    //Don''t know how to imrove it.
}
else if(exp < 31 && exp >= 23)
{
    //Solved. Works twise faster than sprintf().
}
else if(exp < 23 && exp >= 0)
{
    //Solved. twise faster.
}
else if(exp < 0 && exp >= -23)
{
    //Solved. twise faster.
}



这是我的脚趾甲的整体结构(尽管它的身体完全在组装中实现).

现在我的问题是如何用exp>解决问题. 31? :)
我已经对IEEE-754标准进行了很多研究,但是一些细节对我来说却还不清楚.
我发现只有两个有用的示例对我有所帮助,但他们的目标是仅完成工作,不考虑性能问题以及功能非常有限.例如,一个根本不处理(exp> 31)和(exp <-31)的情况,另一个甚至更糟! :laugh:
我什至尝试调试sprintf()本身以对其进行反汇编,但是代码对我来说太混乱了. :)
当然,如果您用Google搜索如何将float转换为字符串",几乎所有答案都是->使用sprintf()

因此,考虑到我花了任何时间的帮助,我们将不胜感激! :)



This is the overall structure of my ftoa (although it''s body is entirely implemened in assembly).

Now my question is how to solve the case with exp > 31? :)
I''ve done a lot of research on IEEE-754 standard but some details never got quite clear to me.
I''ve found just two useful examples which helped me but their goal is to merely do the job, no performance issues taken into account, and very limited functionality. For example, the one doesn''t process the cases (exp > 31) and (exp < -31) at all, the other is even worse! :laugh:
I even tried to debug sprintf() itself looking into its disassembly but the code is way too messy for me. :)
And of course if you google "How to convert float to string" almost all the answers will be -> use sprintf()

So considering the amount of time I''ve spent any help will be greatly appreciated! :)

推荐答案

尝试看看 ^ ]和 _fcvt(CRT) [ ^ ]功能:它们完全可以完成您正在从事的工作.

它们比sprintf快,因为它们不需要解析 format字符串.

您还可以对其进行反汇编,以获取有关操作方法的想法(使用 RELEASE 版本):sprintf的代码很复杂,因为该函数具有可变数量的参数 ,要解析的 format字符串以及很多要处理的数据类型和格式. _ecvt_fcvt的反汇编可能更容易.
Try having a look to the _ecvt (CRT)[^] and _fcvt (CRT)[^] functions: they make exactly the job you are working on.

They are faster than sprintf because they don''t need to parse the format string.

You can also get their disassembly to get an idea on how to do (use the RELEASE version): the code of sprintf is complicated because that function has variable number of arguments, a format string to parse, and a lot of data-types and formats to handle. Probably the _ecvt and _fcvt disassembly is easier.


这篇关于我的浮点数到字符串的转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆