Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

float(::Type) width inconsistency #52908

Open
nsajko opened this issue Jan 15, 2024 · 2 comments
Open

float(::Type) width inconsistency #52908

nsajko opened this issue Jan 15, 2024 · 2 comments
Labels
domain:bignums BigInt and BigFloat

Comments

@nsajko
Copy link
Contributor

nsajko commented Jan 15, 2024

float(Union{Int,BigInt}) being narrower than BigFloat seems like a bug.

julia> float(Union{Int,BigInt})
Float64

julia> float(BigInt)
BigFloat

julia> versioninfo()
Julia Version 1.11.0-DEV.1276
Commit fc6295df630 (2024-01-15 05:47 UTC)
Build Info:
  Official https://julialang.org/ release
Platform Info:
  OS: Linux (x86_64-linux-gnu)
  CPU: 8 × AMD Ryzen 3 5300U with Radeon Graphics
  WORD_SIZE: 64
  LLVM: libLLVM-16.0.6 (ORCJIT, znver2)
Threads: 1 default, 0 interactive, 1 GC (on 8 virtual cores)
Environment:
  JULIA_NUM_PRECOMPILE_TASKS = 3
  JULIA_PKG_PRECOMPILE_AUTO = 0

julia> @which float(Union{Bool,BigInt})
float(::Type{T}) where T<:Number
     @ Base float.jl:373

The method looks like this:

float(::Type{T}) where {T<:Number} = typeof(float(zero(T)))

The problem is that zero(T) for T that is an union doesn't behave as we would like here (xref. #34003):

julia> typeof(zero(Union{Int,BigInt}))
Int64
@nsajko nsajko changed the title float(::Type) inconsistency float(::Type) width inconsistency Jan 15, 2024
@nsajko nsajko added the domain:bignums BigInt and BigFloat label Jan 15, 2024
@mikmoore
Copy link
Contributor

Very tangentially related to #52528 (comment), where the insufficient generality of Base.zero(::Union) also causes a hiccup.

It looks like zero (and maybe several other functions like one, float, etc) should attempt to Base.promote_union on union types. E.g.,

julia> zero(Base.IEEEFloat)
ERROR: MethodError: no method matching Union{Float16, Float32, Float64}(::Int64)

julia> zero(Base.promote_union(Base.IEEEFloat)) |> typeof
Float64

This won't work for a Union that doesn't promote to a unique usable type (like ones that promote to Any), but it should work for many "sane" instances like the one here and in that other issue.

@nsajko
Copy link
Contributor Author

nsajko commented Jan 15, 2024

It looks like zero (and maybe several other functions like one, float, etc) should attempt to Base.promote_union on union types.

FTR this conflicts with the design proposed for zero in #34003. And lots of people put a 👍 on that proposal.

But I guess we could fix this issue using Base.promote_union!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
domain:bignums BigInt and BigFloat
Projects
None yet
Development

No branches or pull requests

2 participants