Stream: helpdesk (published)

Topic: Return type of division


view this post on Zulip Brenhin Keller (May 15 2021 at 00:59):

Is there a good way to obtain the type that would result from dividing two numbers of different types? promote_type basically gives you this for addition, but not so much for division since promote_type(Int64, Int64) == Int64 but dividing two ints gives a float.

view this post on Zulip Brenhin Keller (May 15 2021 at 01:00):

typeof(x/x) I suppose would work, but seems inelegant, and Base.return_type returns an array of types, which also seems inelegant

view this post on Zulip Mason Protter (May 15 2021 at 01:06):

One option is

julia> Base.promote_op(/, Int, Int)
Float64

view this post on Zulip Mason Protter (May 15 2021 at 01:06):

(This uses Core.Compiler.return_type under the hood)

view this post on Zulip Brenhin Keller (May 15 2021 at 01:17):

That looks perfect, thanks!


Last updated: Oct 02 2023 at 04:34 UTC