English 中文(简体)
我需要对沙捞越的浮游进行清理。
原标题:I need a clean approach for range-checking floats in Python
  • 时间:2009-12-09 12:35:51
  •  标签:
  • python

在最低和最高限值可能无效的情况下,Im 寻求一种简单的盘点浮标方式。

该法典是:

    tval = float(-b - discriminant) / float (2*a)
    if tval >= tmin and tval <= tmax:
        return tval 

    tval = float(-b + discriminant) / float (2*a)
    if tval >= tmin and tval <= tmax:
        return tval

    # Neither solution was within the acceptable range.
    return None

然而,这完全不能处理无母体或最高权的情况(这应理解为没有最低或最高标准)。

迄今为止,我最能前来的就是:

    tval = float(-b - discriminant) / float (2*a)
    if (tmin == None or tval >= tmin) and (tmax == None or tval <= tmax):
        return tval 

    tval = float(-b + discriminant) / float (2*a)
    if (tmin == None or tval >= tmin) and (tmax == None or tval <= tmax):
        return tval

    # Neither solution was within the acceptable range.
    return None

我始终认为,必须找到更好的(更清晰、更可读)方式来写。 任何想法?

最佳回答

利用点击的答复中的INF

if tmin is None: tmin = -INF
if tmax is None: tmax = +INF

tval = float(-b - discriminant) / float (2*a)
if tmin <= tval <= tmax:
    return tval 

tval = float(-b + discriminant) / float (2*a)
if tmin <= tval <= tmax:
    return tval

# Neither solution was within the acceptable range.
return None
问题回答

可读性:

def inrange(x, min, max):
    return (min is None or min <= x) and (max is None or max >= x)

tval = float(-b - discriminant) / float (2*a)
if inrange(tval, tmin, tmax):
    return tval 

tval = float(-b + discriminant) / float (2*a)
if inrange(tval, tmin, tmax):
    return tval 

# Neither solution was within the acceptable range.
return None

必须有一套界定“<条码>的模块。 但是,我没有发现它(也看不到它,但:)

第一,一些结构:我们需要固定的浮动。

INF = float(1e3000)

INF = float( inf )  # Python 2.6+

First option can be considered p或table f或 practical purposes; just use some really huge value which is guaranteed to be outside the range representable by your platf或m s floating point type. Second option is "truly" p或table, but requires Python 2.6 或 newer.

现在,你的条件可以这样写(edit:只有tmintmax。 不能是零!

if (tmin 或 -INF) <= tval <= (tmax 或 +INF) :
    return tval

Edit

I made a crude err或 by missing that 0.0 is a legitimate value f或 tmintmax. Thanks to Roger Pate f或 noticing.

采用zz式回答(在使用<0> <<>>>>>>>/代码>时,以一定失败的方式):

def coalesce(*values):
  for v in values:
    if v is not None:
      return v

if coalesce(tmin, -INF) <= tval <= coalesce(tmax, INF):
  return tval

然而,我很清楚:

if ((tmin is None or tmin <= tval) and
    (tmax is None or tval <= tmax)):
   return tval




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...