我该怎么办双字符串只使用MATH.H转换? [英] How do I convert double to string using only math.h?

查看:115
本文介绍了我该怎么办双字符串只使用MATH.H转换?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想双转换为字符串在本地NT应用程序,即只依赖于 ntdll.dll中的应用程序。不幸的是,NTDLL的版本 vsnprintf 不支持%F 等,迫使我实现我自己的转换

I am trying to convert a double to a string in a native NT application, i.e. an application that only depends on ntdll.dll. Unfortunately, ntdll's version of vsnprintf does not support %f et al., forcing me to implement the conversion on my own.

上述 ntdll.dll中出口只有少数的文件math.h 函数(地板 CEIL 日志 POW ,...)。不过,我有理由相信,我可以根据需要实现任意的不可用文件math.h 功能。

The aforementioned ntdll.dll exports only a few of the math.h functions (floor, ceil, log, pow, ...). However, I am reasonably sure that I can implement any of the unavailable math.h functions if necessary.

有是浮点转换在GNU的libc中的一个实现,但code是非常密集,难以融为一体prehent(GNU的缩进风格并没有在这里帮助)。

There is an implementation of floating point conversion in GNU's libc, but the code is extremely dense and difficult to comprehent (the GNU indentation style does not help here).

我已经通过规范化数(即乘以/除以10,数,直到它在间隔 [1,10)),然后生成执行转换每一个数字由与 MODF 切割组成部分关闭,由10这工作,但有precision的损失(只有第15位小数部分乘以是正确的)。 precision的损失,当然,固有的算法

I've already implemented the conversion by normalizing the number (i.e. multiplying/dividing the number by 10 until it's in the interval [1, 10)) and then generating each digit by cutting the integral part off with modf and multiplying the fractional part by 10. This works, but there is a loss of precision (only the first 15 digits are correct). The loss of precision is, of course, inherent to the algorithm.

我与17位解决,但一种算法,将能够正确地生成的数字任意数量将是preferred

I'd settle with 17 digits, but an algorithm that would be able to generate an arbitrary number of digits correctly would be preferred.

能否请您提出一个算法或指向我一个很好的资源?

Could you please suggest an algorithm or point me to a good resource?

推荐答案

大量的调查研究后,我发现一篇题为的打印浮点数快速,准确的。它采用精确合理的算法,以避免precision损失。它引用了一个年纪大一点的纸:如何打印浮点数准确,该然而,似乎需要ACM订阅访问。

After a lot of research, I found a paper titled Printing Floating-Point Numbers Quickly and Accurately. It uses exact rational arithmetic to avoid precision loss. It cites a little older paper: How to Print Floating-Point Numbers Accurately, which however seems to require ACM subscription to access.

由于前文于2006年再版,我倾向于认为,这仍然是当前。确切的理性算术(需要动态分配)似乎是一个必要之恶。

Since the former paper was reprinted in 2006, I am inclined to believe that it is still current. The exact rational arithmetic (which requires dynamic allocation) seems to be a necessary evil.

这篇关于我该怎么办双字符串只使用MATH.H转换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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