Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If it's just <code>pow()</code> giving you headaches, you can easily redefine it to return <code>NaN</code> under whatever circumstances you like.</p> <pre><code>def pow(x, y): return x ** y if x == x else float("NaN") </code></pre> <p>If <code>NaN</code> can be used as an exponent you'd also want to check for that; this raises a <code>ValueError</code> exception except when the base is 1 (apparently on the theory that 1 to any power, even one that's not a number, is 1).</p> <p>(And of course <code>pow()</code> actually takes three operands, the third optional, which omission I'll leave as an exercise...)</p> <p>Unfortunately the <code>**</code> operator has the same behavior, and there's no way to redefine that for built-in numeric types. A possibility to catch this is to write a subclass of <code>float</code> that implements <code>__pow__()</code> and <code>__rpow__()</code> and use that class for your <code>NaN</code> values. </p> <p>Python doesn't seem to provide access to any flags set by calculations; even if it did, it's something you'd have to check after each individual operation.</p> <p><strong>In fact, on further consideration,</strong> I think the best solution might be to simply use an instance of a dummy class for missing values. Python will choke on any operation you try to do with these values, raising an exception, and you can catch the exception and return a default value or whatever. There's no reason to proceed with the rest of the calculation if a needed value is missing, so an exception should be fine.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload