简体   繁体   English

如何将浮点数增加一定数量但仍始终保持在 1 以下?

[英]How to increase a float number in a certain amount but still always keeps it under 1?

floats = [0.3, 0.45, 0.98, 0.01]

I have a list of floats in the range of 0<n<1.我有一个 0<n<1 范围内的浮点数列表。 For certain numbers, for example, 0.98, I want to increase it so that it is even larger compared to other numbers.对于某些数字,例如 0.98,我想增加它,使其与其他数字相比更大。 For example, I can increase it by multiplying with a co-efficient >1.例如,我可以通过乘以系数 >1 来增加它。 For example:例如:

a = 1.4*0.98

But this makes 'a' exceed 1. I want to keep 'a' still in the range of 0<a<1, for example, 0.995.但这会使'a'超过1。我想让'a'仍然在0<a<1的范围内,例如0.995。 Is there a way to do that?有没有办法做到这一点?

For arguments sake lets's say that we want to repeatedly uplift each value by some factor.对于 arguments 而言,假设我们希望将每个值反复提升某个因素。 Let that factor be 10% of the difference between the value and 1. Let's do this repeatedly:让该因子为值与 1 之间差异的 10%。让我们重复执行此操作:

floats = [0.3, 0.45, 0.98, 0.01]

for _ in range(100):
    floats = [(1 - v) * 0.1 + v for v in floats]

print(floats)

Output: Output:

[0.9999814070207786, 0.9999853912306118, 0.9999994687720222, 0.9999737042151013]

Thus all values tend towards 1 but never* get there.因此,所有值都趋向于 1,但永远不会*到达那里。

*At some point I guess you could come to a point where this algorithm makes no change to the original value. *在某些时候,我猜你可能会达到这个算法不会改变原始值的地步。 The uplift factor would therefore need to be adjusted (maybe 1% or even less) depending on how often this is going to happen因此,需要根据这种情况发生的频率调整提升系数(可能为 1% 甚至更少)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM