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

Overflow in conversion #430

Open
aplavin opened this issue Mar 10, 2021 · 0 comments
Open

Overflow in conversion #430

aplavin opened this issue Mar 10, 2021 · 0 comments

Comments

@aplavin
Copy link
Contributor

aplavin commented Mar 10, 2021

I saw several old issues regarding overflow exceptions, but looks like they are considered solved. However, I experience the following:

using Unitful, UnitfulAstro

julia> 1.0u"GHz^2 * J * c^-2" |> u"Jy / arcsecond^2"
ERROR: OverflowError: 22468879468420441 * 51840 overflowed for type Int64

julia> uconvert(u"Jy / arcsecond^2", 1.0u"GHz^2 * J * c^-2")
ERROR: OverflowError: 22468879468420441 * 51840 overflowed for type Int64

julia> 1.0u"GHz^2 * J * c^-2" |> upreferred |> u"Jy / arcsecond^2"
2.6152205956835644e16 Jy ″^-2

That is, conversion only works with an intermediate upreferred. I couldn't find a similar example with default Unitful units without other packages, but it should be possible.

Is there a way to force floating-point conversion instead of exact ones?

Stacktrace points to this line: https://github.com/PainterQubits/Unitful.jl/blob/master/src/conversion.jl#L22, which even mentions overflow checking :) But it's not really clear how to fix the error.
Full error:

ERROR: OverflowError: 22468879468420441 * 51840 overflowed for type Int64
Stacktrace:
  [1] throw_overflowerr_binaryop(op::Symbol, x::Int64, y::Int64)
    @ Base.Checked ./checked.jl:154
  [2] checked_mul
    @ ./checked.jl:288 [inlined]
  [3] //(x::Rational{Int64}, y::Int64)
    @ Base ./rational.jl:65
  [4] #s57#154
    @ ~/.julia/packages/Unitful/JwSBO/src/conversion.jl:22 [inlined]
  [5] var"#s57#154"(::Any, s::Any, t::Any)
    @ Unitful ./none:0
  [6] (::Core.GeneratedFunctionStub)(::Any, ::Vararg{Any, N} where N)
    @ Core ./boot.jl:571
  [7] uconvert(a::Unitful.FreeUnits{(″^-2, Jy), 𝐌 𝐓^-2, nothing}, x::Quantity{Float64, 𝐌 𝐓^-2, Unitful.FreeUnits{(GHz^2, J, c^-2), 𝐌 𝐓^-2, nothing}})
    @ Unitful ~/.julia/packages/Unitful/JwSBO/src/conversion.jl:78
  [8] FreeUnits
    @ ~/.julia/packages/Unitful/JwSBO/src/types.jl:97 [inlined]
  [9] |>(x::Quantity{Float64, 𝐌 𝐓^-2, Unitful.FreeUnits{(GHz^2, J, c^-2), 𝐌 𝐓^-2, nothing}}, f::Unitful.FreeUnits{(″^-2, Jy), 𝐌 𝐓^-2, nothing})
    @ Base ./operators.jl:859
 [10] top-level scope
    @ REPL[5]:1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant