<br><br><div class="gmail_quote">On Sat, Nov 5, 2011 at 7:35 PM, Nathaniel Smith <span dir="ltr"><<a href="mailto:njs@pobox.com">njs@pobox.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<div class="im">On Sat, Nov 5, 2011 at 4:07 PM, Matthew Brett <<a href="mailto:matthew.brett@gmail.com">matthew.brett@gmail.com</a>> wrote:<br>
> Intel, gcc:<br>
> 4, -<a href="tel:2147483648" value="+12147483648">2147483648</a><br>
> PPC, gcc:<br>
> 4, <a href="tel:2147483647" value="+12147483647">2147483647</a><br>
><br>
> I think that's what you predicted.  Is it strange that the same<br>
> compiler gives different results?<br>
><br>
> It would be good if the behavior was the same across platforms - the<br>
> unexpected negative overflow caught me out at least.  An error sounds<br>
> sensible to me.  Would it cost lots of cycles?<br>
<br>
</div>C99 says (section F.4):<br>
<br>
"If the floating value is infinite or NaN or if the integral part of<br>
the floating value exceeds the range of the integer type, then the<br>
‘‘invalid’’ floating-point exception is raised and the resulting value<br>
is unspecified. Whether conversion of non-integer floating values<br>
whose integral part is within the range of the integer type raises the<br>
‘‘inexact’’ floating-point exception is unspecified."<br>
<br>
So it sounds like the compiler is allowed to return whatever nonsense<br>
it likes in this case. But, you should be able to cause this to raise<br>
an exception by fiddling with np.seterr.<br>
<br>
However, that doesn't seem to work for me with numpy 1.5.1 on x86-64 linux :-(<br>
<br>
>>> np.int32(np.float32(2**31))<br>
-<a href="tel:2147483648" value="+12147483648">2147483648</a><br>
>>> np.seterr(all="raise")<br>
>>> np.int32(np.float32(2**31))<br>
-<a href="tel:2147483648" value="+12147483648">2147483648</a><br>
<br>
I think this must be a numpy or compiler bug?<br>
<font color="#888888"><br></font></blockquote><div><br>I don't believe the floating point status is checked in the numpy conversion routines. That looks like a nice small project for someone interested in learning the numpy internals.<br>
<br>Chuck<br></div></div>