promote_type
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.
Examples
julia> promote_type(Int64, Float64)
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:
-
Promote two integer types:
julia> promote_type(Int8, Int16) Int32In this example,
promote_typereturnsInt32as the most general type that can hold values from bothInt8andInt16types. -
Promote an integer and a floating-point type:
julia> promote_type(Int32, Float32) Float64Here,
promote_typedetermines that the most general type to hold values from bothInt32andFloat32types isFloat64. - Promote a complex and a real number type:
julia> promote_type(ComplexF64, Float32) ComplexF64In this case,
promote_typerecognizes that the most general type to accommodate bothComplexF64andFloat32types isComplexF64.
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.