promote_type(type1, type2)

Determine a type big enough to hold values of each argument type without loss, whenever possible. In some cases, where no type exists to which both types can be promoted losslessly, some loss is tolerated; for example, promote_type(Int64,Float64) returns Float64 even though strictly, not all Int64 values can be represented exactly as Float64 values.


julia> promote_type(Int64, Float64)

In the Julia programming language, the promote_type function is used to determine a type that can hold values from two input types without loss, whenever possible. It returns the most general type that can encompass both input types.

Here are a few examples of how to use promote_type:

  1. Promote two integer types:

    julia> promote_type(Int8, Int16)

    In this example, promote_type returns Int32 as the most general type that can hold values from both Int8 and Int16 types.

  2. Promote an integer and a floating-point type:

    julia> promote_type(Int32, Float32)

    Here, promote_type determines that the most general type to hold values from both Int32 and Float32 types is Float64.

  3. Promote a complex and a real number type:
    julia> promote_type(ComplexF64, Float32)

    In this case, promote_type recognizes that the most general type to accommodate both ComplexF64 and Float32 types is ComplexF64.

It is important to note that in some cases, where no type exists that can hold values from both input types without loss, promote_type may return a type that allows for some loss of precision or information.

If no suitable type promotion is possible, an error will be thrown.

The promote_type function is particularly useful in generic programming scenarios where the resulting type needs to be determined dynamically based on the input types.

See Also

BigFloat, BigInt, Dict, eltype, fieldtype, Float32, Float64, IntSet, isa, isalnum, isalpha, isascii, iseltype, isequal, isgraph, isimmutable, isinteractive, isleaftype, isnull, ispunct, isspace, issubtype, keytype, Nullable, NullException, promote_type, typeintersect, typejoin, typemax, typemin, typeof, Val, valtype,

User Contributed Notes

Add a Note

The format of note supported is markdown, use triple backtick to start and end a code block.

*Required Field

Checking you are not a robot: