-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
make x^y for integer x,y return a float? #745
Comments
I wonder if we may have float arithmetic operations by default (this is the standard behavior in Matlab and R.). If we want integer arithmetic operations then we'll need to state it explicitly Present behavior (Julia Windows) 2^500 = 0 Proposed behavior (Julia Windows) 2^500 = 3.273390607896142e150 |
You do state it explicitly by using integer types. |
if, when x and y are ints, (x^y) returns type T by default, then (x*y) [and (x+y), (x-y), (x/y), ...] should return type T by default. It would be better to have the default be int x int --> float and introduce a complete set of recognizably similar operators to designate int x int --> int than to mix default return types for operations (and functions) 'of a kind'. The alternative would be having the default as int x int --> int for operations (and functions) 'of a kind', and using some floatifying notation when int x int --> float is desired. . |
Making integer arithmetic return a float is nuts because it means, among other things, that you can't write a simple loop using integer arithmetic. Adding two integers should and will produce an integer. |
" Making integer arithmetic return a float is nuts " -- true that 'x^y' is not always very different from, e.g. 'x_y' (for integers, x_x is For me, it is nicer and saves development time, when numeric ops with On Thu, Apr 26, 2012 at 4:23 PM, Stefan Karpinski <
|
That is, in fact, how things work at the moment except that |
+1 vote for respecting the equivalence. Why does zero result when 'x^y' overflows for integer x,y? On Thu, Apr 26, 2012 at 5:57 PM, Stefan Karpinski <
|
Because that's how integer arithmetic works on x86 (at least that's how the code that LLVM generates works). |
I agree with Stefan that + and / are not the same --- integers are closed under one and not the other. We should consider making |
I suspect that making |
|
Historically, matlab only had doubles, and added true integer data types much later. We want integers to be integers from the beginning, so an integer literal is of an integer type. I understand the "command line comfort" of wanting to get the "right answer" for any typed expression, but at the end of the day this is really just a minor nuisance. The portability question is a totally different one. It might be good to make |
I'm still in favor off having overflow throw an exception, but being able to disable the check through a command line flag |
Yes I totally agree. It would be much better if optionally overflow could throw an exception (since getting Inf it appears has its own drawbacks). |
Yes, having the ability to check for any kind of sketchy numerical thing is a good feature. The only question is what to enable by default, and we will make tradeoffs to balance performance and safety. At some point I'll check the cost of converting NaN to errors. We can add intrinsics for that and it will probably be worth it. Finding out exactly where a NaN (or overflow) arose could be really useful. And nobody wants to deal with |
Resolved: make |
For some reason this popped up on Github and reading "throw a domain error when x and y will cause overflow" just the remark: That is not the case currently and I keep running into this, because the notation |
I filed an issue #5573 |
We need to decide if
x^y
should return an int or a float when x and y are ints. Current behavior may be problematic and/or surprising. Since there are very few integer values ofx
andy
wherex^y
can be computed exactly with integers, maybe it should default to floating point. In which case we may want to introduceipow(x,y)
as a shorter name forpower_by_squaring
.The text was updated successfully, but these errors were encountered: