如何获得“最短”唯一确定给定双倍的BigDecimal [英] How to get "shortest" BigDecimal that uniquely determines a given double

查看:177
本文介绍了如何获得“最短”唯一确定给定双倍的BigDecimal的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,我很好奇如何在不经过创建字符串的过程中保持 new BigDecimal(Double.toString(d))。 p>

Double.toString 的文档是相当复杂(有趣的)。据我所知,该方法实际上并不返回由给定的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 accurately and How 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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