在用浮点分开时如何保持小数的准确性
我正在从事一个项目,需要将一个非常大的64位长
值分开。我绝对不在乎整个数字结果,而只关心小数价值。问题在于,当将大长
除以一个小的64位double
浮点值时,由于需要存储整个数字,因此我会在浮点值中散布精度。
本质上,我要做的是:
double x = long_value / double_value % 1;
但是,如果不丢失精度,long_value
是。是否有一种编写此表达式的方法,以便整个数字被丢弃,并且浮点精度不会丢失?谢谢。
编辑:顺便说一句,我在这里试图投票所有这些有用的答案,但是我只是为此问题提出了这个帐户,您需要15个声誉才能投票
I am working on a project, and I need to divide a very large 64 bit long
value. I absolutely do not care about the whole number result, and only care about the decimal value. The problem is that when dividing a large long
with a small 64 bit double
floating point value, I loose accuracy in the floating point value due to it needing to store the whole numbers.
Essentially what I am trying to do is this:
double x = long_value / double_value % 1;
but without loosing precision the larger the long_value
is. Is there a way of writing this expression so that the whole numbers are discarded and floating point accuracy is not lost? Thanks.
EDIT: btw im out here trying to upvote all these helpful answers, but I just made this account for this question and you need 15 reputation to cast a vote
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您的语言提供了精确的
fmod
实现,则可以执行类似的操作:如果
long_value
不会精确地转换为double
值,则可以拆分它分为两半,fmod
单独将这些值添加在一起,并将该总和sum -double_value
bydouble_value
。如果
long_value
或double_value
为负,则可能需要考虑不同的案例,具体取决于fmod
的行为以及您期望的结果。If your language provides an exact
fmod
implementation you can do something like this:If
long_value
does not convert exactly to adouble
value, you could split it into two halves,fmod
them individually, add these values together and divide that sum orsum - double_value
bydouble_value
.If
long_value
ordouble_value
is negative you may also need to consider different cases depending on how yourfmod
behaves and what result you expect.long_value
与:然后您可以这样做:
long_value
is congruent to:Then you can do this:
您使用的语言是否具有大整数/大理性库?为了避免信息丢失,您必须在转换信息时将信息“分散”到更多内存中,以免失去对保留的零件。从本质上讲,这是一个大整数库为您做的。您可以使用此算法(我不知道您正在使用哪种语言,所以这只是伪代码:
Does the language you're using have a big integer/big rational library? To avoid loss of information, you'll have to "spread out" the information across more memory while you're transforming it so you don't lose the part you're interested in preserving. This is essentially what a big integer library would do for you. You could employ this algorithm (I don't know what language you're using so this is just pseudocode: