问题描述
基本上,我很好奇如何在不经过创建字符串的过程中保持 new BigDecimal(Double.toString(d))
。 p>
的文档是相当复杂(有趣的)。据我所知,该方法实际上并不返回由给定的double实际表示的数字的字符串表示,而是唯一标识给定双重的(近)最短实数的字符串表示
(我实际上并不需要这个,如果我这样做,我可能会经历一个字符串,我只是对这个算法感到好奇,发现这个短实数唯一地确定给定的双重值。)
(这是关于。)
请参阅:
和
实现:和
有关此主题的那些可能会提供解决方案的人感兴趣。如果您愿意在99.8%的案件中确定最短的字符串,则其(Grisu2)将在没有bignum的情况下工作,而在其余的案例中,它是准确但不是最短的字符串。
Basically, I'm curious on how to get hold of new BigDecimal(Double.toString(d))
without going through the process of creating a string.
The documentation for Double.toString
is quite complex (and interesting). As I understand it, the method does not actually return the string representation of the number actually represented by the given double, but the string representation of the (near by) shortest real number that uniquely identifies the given double.
(I don't actually need this. If I did, I'd probably go through a string anyway. I'm just curious about this algorithm that finds this "short" real number that uniquely determines the given double value.)
(This is a follow-up question on this question.)
See:How to print floating-point numbers accuratelyandHow to read floating point numbers accurately
implementations: here and here
There is a new publication on this subject that may be of interest to those looking at providing solutions. It (Grisu2) works without bignums if you are willing to settle for the shortest string in 99.8% of cases, and an accurate but not shortest string in the remaining cases.
这篇关于如何获得“最短”唯一确定给定双倍的BigDecimal的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!