使用对数来避免数值下溢的算术问题

编程入门 行业动态 更新时间:2024-10-11 17:19:31
本文介绍了使用对数来避免数值下溢的算术问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有两个分数列表;

说 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] * ...

更多推荐

使用对数来避免数值下溢的算术问题

本文发布于:2023-10-24 20:53:12,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1524976.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:对数   算术   数值

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!