X.^2 - how can something so basic be so wrong

Working with Int16 data I’m getting negative values when squaring data:

a = -750;
a^2
562500

a = Int16(a);
a^2
-27324

I get that Int16(a)^2 produces a value that is out of the range of an Int16 but I don’t get why an error isn’t thrown when the solution is out of range or why the function doesn’t just return the maximum Int16 value (32767) like Matlab does. This behavior is very unintuitive.

Does anyone have a good reason for why Julia behaves in this way?

https://docs.julialang.org/en/v1/manual/faq/#faq-integer-arithmetic

7 Likes

Exactly what I was looking for… thanks!