Error for 396^n

The power ^ operator is the one thing I would want to change in Julia, even power of 3 can be a problem, see linked issue. For a^b it’s rather unsafe, unless a and/or b are floating point numbers, or big integers, not even Int128 type is safe.

All math operators can overflow, even + (and * and -), except /, and this may come as a surprise, except to users of most languages, such as C and C++, i.e. fast languages.

julia> typemax(Int128)+1
-170141183460469231731687303715884105728

In some sense that IS a correct result, i.e. ALL operators (expect / since it divides with a Float64 result) return modular math results, and they are correct when defined that way, but unexpected definition for many or most users. And for power, I think maybe never a good definition, while ok when you do not actually overflow, as with all the other operators.

So why do I only want to change ^? Because the problem is more apparent there (it’s the one operator that is really dangerous, much more than the other), and with Int64 type (the default Int type on 64-platforms), +, -, are in practice not a worry, and * also rarely, though more often.

[If you divide by 2, you get the correct exact result at least half the time, but if you divide by e.g. 3, you never get the correct exact result, unless you use Rationals, and that is a problem for any language (except maybe Raku/Perl 6 that may default to rationals, or does it not?). And even the rationals need to be of the BigInt type to guard against overflow. So with math you are screwed in pretty much any language, unless you opt into non-defaults, take care. Why do I mention thi? Because correct math vs fast math is a trade-off in any language, even a trade-off between approximately correct and fast.]

2 Likes