你如何获得浮点序列中的下一个值? [英] How do you get the next value in the floating-point sequence?

查看:156
本文介绍了你如何获得浮点序列中的下一个值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Python是否提供了一个函数来获得浮点值的增加现有的浮点值的最低有效位?

我正在寻找对于类似于 std :: nextafter

要回答你的问题的第一部分:不,Python doesn不直接提供这个功能。但是编写一个Python函数很容易,假设IEEE 754是浮点的。



IEEE 754二进制浮点格式的设计非常巧妙,从一个浮点数到下一个浮点数就像增加位表示一样简单。这适用于 [0,infinity)范围内的任何数字,就是指数边界和子范围之间的数字。要生成覆盖整个浮点范围的 nextUp 版本,还需要处理负数,无穷大,nans和一个涉及负零的特殊情况。下面是符合IEEE 754标准的Python版本的 nextUp 函数。它涵盖了所有的角落案例。

 导入数学
导入结构

def next_up( x):
#NaNs和正无限映射到自己。
如果math.isnan(x)或(math.isinf(x)和x> 0):
return x

#0.0和-0.0都映射到最小+浮动。
if x == 0.0:
x = 0.0

n = struct.unpack('< q',struct.pack('< d',x))[0 ]
如果n> = 0:
n + = 1
else:
n - = 1
return struct.unpack('< d',struct.pack ('< q',n))[0]

nextDown nextAfter 然后看起来像这样。 (请注意, nextAfter 不是IEEE 754指定的函数,所以对于IEEE特殊值会发生什么情况有一点猜测。 Python的 decimal.Decimal 类的十进制算术标准是基于。)

  def next_down(x):
return -next_up(-x)
$ b $ def next_after(x,y):
#如果任一参数是NaN,则返回该参数。
#如果math.isnan(x):
返回x
如果math.isnan(y):
返回y $,则匹配decimal.Decimal
中的实现b
$ b if y == x:
return y
elif y> x:
return next_up(x)
else:
return next_down(x)


Does Python provide a function to get the floating-point value that results from incrementing the least significant bit of an existing floating-point value?

I'm looking for something similar to the std::nextafter function that was added in C++11.

解决方案

To answer the first part of your question: no, Python doesn't provide this functionality directly. But it's quite easy to write a Python function that does this, assuming IEEE 754 floating-point.

The IEEE 754 binary floating-point formats are rather cleverly designed so that moving from one floating-point number to the 'next' one is as simple as incrementing the bit representation. This works for any number in the range [0, infinity), right across exponent boundaries and subnormals. To produce a version of nextUp that covers the complete floating-point range, you also need to deal with negative numbers, infinities, nans, and one special case involving negative zero. Below is a standards compliant version of IEEE 754's nextUp function in Python. It covers all the corner cases.

import math
import struct

def next_up(x):
    # NaNs and positive infinity map to themselves.
    if math.isnan(x) or (math.isinf(x) and x > 0):
        return x

    # 0.0 and -0.0 both map to the smallest +ve float.
    if x == 0.0:
        x = 0.0

    n = struct.unpack('<q', struct.pack('<d', x))[0]
    if n >= 0:
        n += 1
    else:
        n -= 1
    return struct.unpack('<d', struct.pack('<q', n))[0]

The implementations of nextDown and nextAfter then look like this. (Note that nextAfter is not a function specified by IEEE 754, so there's a little bit of guesswork as to what should happen with IEEE special values. Here I'm following the IBM Decimal Arithmetic standard that Python's decimal.Decimal class is based on.)

def next_down(x):
    return -next_up(-x)

def next_after(x, y):
    # If either argument is a NaN, return that argument.
    # This matches the implementation in decimal.Decimal
    if math.isnan(x):
        return x
    if math.isnan(y):
        return y

    if y == x:
        return y
    elif y > x:
        return next_up(x)
    else:
        return next_down(x)

这篇关于你如何获得浮点序列中的下一个值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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