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

**URL:** <https://discourse.julialang.org/t/x-2-how-can-something-so-basic-be-so-wrong/76052>\
**Category:** New to Julia\
**Tags:** question, integer-overflow\
**Created:** [February 9, 2022, 12:26am UTC](https://discourse.julialang.org/t/x-2-how-can-something-so-basic-be-so-wrong/76052 "2022-02-09T00:26:43Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![alex-s-gardner](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alex-s-gardner/32/30210_2.png) [@alex-s-gardner](https://discourse.julialang.org/u/alex-s-gardner)\
**Post date:** [February 9, 2022, 12:26am UTC](https://discourse.julialang.org/t/x-2-how-can-something-so-basic-be-so-wrong/76052/1 "2022-02-09T00:26:43Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)\
**Post date:** [February 9, 2022, 12:28am UTC](https://discourse.julialang.org/t/x-2-how-can-something-so-basic-be-so-wrong/76052/2 "2022-02-09T00:28:22Z")

</div>

[https://docs.julialang.org/en/v1/manual/faq/#faq-integer-arithmetic](https://docs.julialang.org/en/v1/manual/faq/#faq-integer-arithmetic)

---

<div class="post-metadata">

**Author:** ![alex-s-gardner](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alex-s-gardner/32/30210_2.png) [@alex-s-gardner](https://discourse.julialang.org/u/alex-s-gardner)\
**Post date:** [February 9, 2022, 12:31am UTC](https://discourse.julialang.org/t/x-2-how-can-something-so-basic-be-so-wrong/76052/3 "2022-02-09T00:31:12Z")

</div>

Exactly what I was looking for… thanks!
