# Type computation: numerical type with twice more bits

**URL:** <https://discourse.julialang.org/t/type-computation-numerical-type-with-twice-more-bits/77000>\
**Category:** General Usage\
**Tags:** numbers, integer-overflow\
**Created:** [February 23, 2022, 11:58pm UTC](https://discourse.julialang.org/t/type-computation-numerical-type-with-twice-more-bits/77000 "2022-02-23T23:58:56Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![greatpet](https://avatars.discourse-cdn.com/v4/letter/g/e495f1/32.png) [@greatpet](https://discourse.julialang.org/u/greatpet)\
**Post date:** [February 23, 2022, 11:58pm UTC](https://discourse.julialang.org/t/type-computation-numerical-type-with-twice-more-bits/77000/1 "2022-02-23T23:58:57Z")

</div>

I’m trying to write a generic function accepting two Integers as arguments. The function will need a local variable storing the product of the two integers. So if the functions arguments are of type Int16, the local variable needs to be of type Int32, and if the argumetns are of type Int32, then the local variables needs to be of type Int64. I don’t want BigInt, for efficiency reasons. Is there some function like larger\_type, behaving like larger\_type(Int16) = Int32, larger\_type(Int32) = Int64, etc.?

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [February 24, 2022, 12:03am UTC](https://discourse.julialang.org/t/type-computation-numerical-type-with-twice-more-bits/77000/2 "2022-02-24T00:03:58Z")

</div>

In most cases [widen](https://docs.julialang.org/en/v1/base/base/#Base.widen) should do what you want.

---

<div class="post-metadata">

**Author:** ![nchisholm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nchisholm/32/37414_2.png) [@nchisholm](https://discourse.julialang.org/u/nchisholm)\
**Post date:** [February 24, 2022, 3:02am UTC](https://discourse.julialang.org/t/type-computation-numerical-type-with-twice-more-bits/77000/3 "2022-02-24T03:02:59Z")

</div>

Adding to @DNF’s suggestion, your problem brings to mind the example in the manual of [outer-only constructors](https://docs.julialang.org/en/v1/manual/constructors/#Outer-only-constructors), which gives an example use of `widen` to achieve a similar goal.

---

<div class="post-metadata">

**Author:** ![Palli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/palli/32/3380_2.png) [@Palli](https://discourse.julialang.org/u/Palli)\
**Post date:** [February 24, 2022, 9:42am UTC](https://discourse.julialang.org/t/type-computation-numerical-type-with-twice-more-bits/77000/4 "2022-02-24T09:42:47Z")

</div>

> [@greatpet](#):
>
> I don’t want BigInt, for efficiency reasons.

Then note on this advice:

> [@DNF](#):
>
> In most cases [widen](https://docs.julialang.org/en/v1/base/base/#Base.widen) should do what you want.

I checked and it does widen up to Int128, but then to BigInt, and Float64 to BigFloat. Inherently there’s nothing wrong with going to Int256 or higher (or even just to Int64 before BigInt), it’s just that Julia doesn’t have those types, but a package does up to Int1024 if I recall.

It’s

> defined so that arithmetic operations + and - are guaranteed not to overflow nor lose precision for any combination of values that type x can hold.

actually you only need to widen by 1 bit for that, if you had such a type. For `a * b` you need a-plus-b-number-of-bits, or double them when same type, what widen does. `^` and `<<` are problematic operators.

That’s the short summary of my longer (not, yet, implemented proposal):

> [@Potential solution to the overflow problem; 64-bit considered harmful, 32- or 21-bit better](https://discourse.julialang.org/t/potential-solution-to-the-overflow-problem-64-bit-considered-harmful-32-or-21-bit-better/69918):
>
> We can implement FastIntegers.jl, faster than the current default Int64 and eliminate overflows from the language by leveraging Julia’s type system, and no hardware support is needed. As a package it of course doesn’t change the language itself, it would be a prototype, or actually very useful as such, but even better if integrated into the language. That could happen for Julia 2.0 but not if people are against the idea. If we consider even very simple functions: double(x) = 2x then it’s not …
