下浮? [英] Next float?

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

问题描述

Python中是否有一种简单,优雅的方法可以从

给出下一个浮点数?通过next float,我的意思是给出一个浮点x,我想要最小的

浮点数大于x。


如果我可以进入任何一个方向(即前一个浮动

以及下一个)。


数学学生注意:我知道没有 ;下一个实数,

但浮点数不是实数。

-

史蒂文

解决方案

2007年11月22日下午2:04,Steven D''Aprano

< st **** @ remove.this.cybersource.com.auwrote :


Python中是否有一种简单,优雅的方式可以从

给出下一个浮点数?通过next float,我的意思是给出一个浮点x,我想要最小的

浮点数大于x。


如果我可以进入任何一个方向(即前一个浮动

以及下一个)。



不是那么优雅,但是你可以使用ctypes来操作这些位

(假设机器使用IEEE 754双倍的Python浮点数,我'我不确定

如果在深奥的平台上保证这一点:


导入ctypes


def inc_float(f):

#获取一个指向浮点数据的int64指针

fv = ctypes.c_double(f)

pfv = ctypes。指针(fv)

piv = ctypes.cast(pfv,ctypes.POINTER(ctypes.c_uint64))


#检查NaN或无穷大,返回不变

v = piv.contents.value

如果不是〜(v&(11<< 52)):#exponent is all 1''s

返回f


如果v == 1<< 63:#-0,视为+0

v = 1

elif v& (1<< 63):#negative

v - = 1

else:#positive或+0

v + = 1


#设置int指针并返回已更改的浮点

piv.contents.value = v

return fv.value


def dec_float(f):

#获取一个指向浮点数据的int64指针

fv = ctypes.c_double(f)

pfv = ctypes.pointer(fv)

piv = ctypes.cast(pfv,ctypes.POINTER(ctypes.c_uint64))


#检查NaN或无穷大,返回不变

v = piv.contents.value

如果不是〜(v&(11<< 52)):#exponent is所有1'的

返回f


如果v == 0:#+ 0,则视为-0

v = (1 << 63)| 1

elif v& (1<< 63):#negative

v + = 1

else:#positive

v - = 1


#设置int指针并返回已更改的浮点

piv.contents.value = v

返回fv.value




" Steven D''Aprano" < st **** @ REMOVE.THIS.cybersource.com.auwrote in message

news:pa ******************** *@REMOVE.THIS.cybersour ce.com.au ...


Python中有一种简单,优雅的方式可以从

给一个?通过next float,我的意思是给出一个浮点x,我想要最小的

浮点数大于x。


如果我可以进入任何一个方向(即前一个浮动

以及下一个)。


数学学生注意:我知道没有 ;下一个实数,

但浮点数不是实数。


-

史蒂文


这里有一些函数来获取float的二进制表示。然后

只是操纵位(读者的练习):


导入结构


def f2b( f):

返回struct.unpack(''我',struct.pack(''f'',f))[0]


def b2f(b):

返回struct.unpack(''f'',struct.pack(''我',b))[0]


>> f2b(1.0)



1065353216


>> hex(f2b(1.0))



''0x3f800000''


>> b2f(0x3f800000)



1.0


>> b2f(0x3f800001)



1.0000001192092896


>> b2f(0x3f7fffff)



0.99999994039535522

-Mark


Steven D'' Aprano< st **** @ REMOVE.THIS.cybersource.com.auwrites:


Python中是否有一种简单,优雅的方式来获取下一个浮点数a

给一个?通过next float,我的意思是给出一个浮点x,我想要最小的

浮点大于x。



我认为你必须通过一点点的方式来做。但像bisection这样的东西可能会非常接近,因为x的表现很好:


def nextfloat(x):

dx =(x,x / 2.0)

而x + dx [1]!= x:

dx =(dx [1],dx [1] /2.0)

返回dx [0] + x


Is there a simple, elegant way in Python to get the next float from a
given one? By "next float", I mean given a float x, I want the smallest
float larger than x.

Bonus points if I can go in either direction (i.e. the "previous float"
as well as the next).

Note to maths pedants: I am aware that there is no "next real number",
but floats are not reals.
--
Steven

解决方案

On Nov 22, 2007 2:04 PM, Steven D''Aprano
<st****@remove.this.cybersource.com.auwrote:

Is there a simple, elegant way in Python to get the next float from a
given one? By "next float", I mean given a float x, I want the smallest
float larger than x.

Bonus points if I can go in either direction (i.e. the "previous float"
as well as the next).

Not so elegant, but you could use ctypes to manipulate the bits
(assumes machine uses IEEE 754 doubles for Python floats, I''m not sure
if that''s guaranteed on esoteric platforms):

import ctypes

def inc_float(f):
# Get an int64 pointer to the float data
fv = ctypes.c_double(f)
pfv = ctypes.pointer(fv)
piv = ctypes.cast(pfv, ctypes.POINTER(ctypes.c_uint64))

# Check for NaN or infinity, return unchanged
v = piv.contents.value
if not ~(v & (11 << 52)): # exponent is all 1''s
return f

if v == 1 << 63: # -0, treat as +0
v = 1
elif v & (1 << 63): # negative
v -= 1
else: # positive or +0
v += 1

# Set int pointer and return changed float
piv.contents.value = v
return fv.value

def dec_float(f):
# Get an int64 pointer to the float data
fv = ctypes.c_double(f)
pfv = ctypes.pointer(fv)
piv = ctypes.cast(pfv, ctypes.POINTER(ctypes.c_uint64))

# Check for NaN or infinity, return unchanged
v = piv.contents.value
if not ~(v & (11 << 52)): # exponent is all 1''s
return f

if v == 0: # +0, treat as -0
v = (1 << 63) | 1
elif v & (1 << 63): # negative
v += 1
else: # positive
v -= 1

# Set int pointer and return changed float
piv.contents.value = v
return fv.value



"Steven D''Aprano" <st****@REMOVE.THIS.cybersource.com.auwrote in message
news:pa*********************@REMOVE.THIS.cybersour ce.com.au...

Is there a simple, elegant way in Python to get the next float from a
given one? By "next float", I mean given a float x, I want the smallest
float larger than x.

Bonus points if I can go in either direction (i.e. the "previous float"
as well as the next).

Note to maths pedants: I am aware that there is no "next real number",
but floats are not reals.
--
Steven

Here''s some functions to get the binary representation of a float. Then
just manipulate the bits (an exercise for the reader):

import struct

def f2b(f):
return struct.unpack(''I'',struct.pack(''f'',f))[0]

def b2f(b):
return struct.unpack(''f'',struct.pack(''I'',b))[0]

>>f2b(1.0)

1065353216

>>hex(f2b(1.0))

''0x3f800000''

>>b2f(0x3f800000)

1.0

>>b2f(0x3f800001)

1.0000001192092896

>>b2f(0x3f7fffff)

0.99999994039535522

-Mark


Steven D''Aprano <st****@REMOVE.THIS.cybersource.com.auwrites:

Is there a simple, elegant way in Python to get the next float from a
given one? By "next float", I mean given a float x, I want the smallest
float larger than x.

I think you have to do it by bit twiddling. But something like bisection
search could come pretty close, for well-behaved values of x:

def nextfloat(x):
dx = (x, x/2.0)
while x+dx[1] != x:
dx = (dx[1], dx[1]/2.0)
return dx[0]+x


这篇关于下浮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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