使用对数来避免数值下溢的算术问题 [英] problem with arithmetic using logarthms to avoid numerical underflow

查看:88
本文介绍了使用对数来避免数值下溢的算术问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个分数列表;

A = [ 1/212, 5/212, 3/212, ... ]

B = [ 4/143, 7/143, 2/143, ... ].

如果我们定义 A' = a[0] * a[1] * a[2] * ...B' = b[0] * b[1]* b[2] * ...

我要计算A'/B'的值,

我的问题是 A 和 B 都很长,每个值都很小,所以计算乘积会很快导致数值下溢......

My trouble is A are B are both quite long and each value is small so calculating the product causes numerical underflow very quickly...

我了解通过对数将乘积转化为总和可以帮助我确定 A' 或 B' 中的哪个更大

I understand turning the product into a sum through logarithms can help me determine which of A' or B' is greater

max(log(a[0])+log(a[1])+..., log(b[0])+log(b[1])+...)

但我需要实际比率....

but i need the actual ratio....

我迄今为止最好的选择是将数字表示形式保留为分数,即 A = [ [1,212], [5,212], [3,212], ... ] 并实现我自己的算术,但是它变得很笨拙,我觉得有一种(简单的)对数方式我只是想念......

My best bet to date is to keep the number representations as fractions, ie A = [ [1,212], [5,212], [3,212], ... ] and implement my own arithmetic but it's getting clumsy and I have a feeling there is a (simple) way of logarithms I'm just missing....

A 和 B 的分子不是来自序列.对于这个问题,它们也可能是随机的.如果它有助于 A 中所有值的分母相同,那么 B 的所有分母也相同.

The numerators for A and B don't come from a sequence. They might as well be random for the purpose of this question. If it helps the denominators for all values in A are the same, as are all the denominators for B.

欢迎提出任何想法!

垫子

推荐答案

你可以用稍微不同的顺序来计算:

You could calculate it in a slightly different order:

A' / B' = a[0] / b[0] * a[1] / b[1] * a[2] / b[2] * ...

这篇关于使用对数来避免数值下溢的算术问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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