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 when doing upreferred(1u"ϵ0") #79

Closed
mdavezac opened this issue Apr 24, 2017 · 2 comments
Closed

Overflow when doing upreferred(1u"ϵ0") #79

mdavezac opened this issue Apr 24, 2017 · 2 comments

Comments

@mdavezac
Copy link
Contributor

julia> using Unitful

julia> upreferred(1u"ϵ0")
ERROR: OverflowError()
 in *(::Rational{Int64}, ::Rational{Int64}) at ./rational.jl:208
 in mapfoldl_impl(::Unitful.##7#9, ::Base.#*, ::Int64, ::Tuple{Tuple{Float64,Rational{Int64}},Tuple{Float64,Int64},Tuple{Float64,R
ational{Int64}}}, ::Int64) at ./reduce.jl:49
 in basefactor(::Unitful.FreeUnits{(Unitful.Unit{:Henry,Unitful.Dimensions{(Unitful.Dimension{:Current}(-2//1),Unitful.Dimension{:
Length}(2//1),Unitful.Dimension{:Mass}(1//1),Unitful.Dimension{:Time}(-2//1))}}(0,-1//1),Unitful.Unit{:Meter,Unitful.Dimensions{(U
nitful.Dimension{:Length}(1//1),)}}(0,1//1),Unitful.Unit{:SpeedOfLight,Unitful.Dimensions{(Unitful.Dimension{:Length}(1//1),Unitfu
l.Dimension{:Time}(-1//1))}}(0,-2//1)),Unitful.Dimensions{(Unitful.Dimension{:Current}(2//1),Unitful.Dimension{:Length}(-3//1),Uni
tful.Dimension{:Mass}(-1//1),Unitful.Dimension{:Time}(4//1))}}) at /Users/mdavezac/workspaces/crystal/julia/v0.5/Unitful/src/Unitf
ul.jl:453
 in convfact(...) at /Users/mdavezac/workspaces/crystal/julia/v0.5/Unitful/src/Conversion.jl:56
 in upreferred(::Unitful.Quantity{Float64,Unitful.Dimensions{(Unitful.Dimension{:Current}(2//1),Unitful.Dimension{:Length}(-3//1),
Unitful.Dimension{:Mass}(-1//1),Unitful.Dimension{:Time}(4//1))},Unitful.FreeUnits{(Unitful.Unit{:Henry,Unitful.Dimensions{(Unitfu
l.Dimension{:Current}(-2//1),Unitful.Dimension{:Length}(2//1),Unitful.Dimension{:Mass}(1//1),Unitful.Dimension{:Time}(-2//1))}}(0,
-1//1),Unitful.Unit{:Meter,Unitful.Dimensions{(Unitful.Dimension{:Length}(1//1),)}}(0,1//1),Unitful.Unit{:SpeedOfLight,Unitful.Dim
ensions{(Unitful.Dimension{:Length}(1//1),Unitful.Dimension{:Time}(-1//1))}}(0,-2//1)),Unitful.Dimensions{(Unitful.Dimension{:Curr
ent}(2//1),Unitful.Dimension{:Length}(-3//1),Unitful.Dimension{:Mass}(-1//1),Unitful.Dimension{:Time}(4//1))}}}) at /Users/mdaveza
c/workspaces/crystal/julia/v0.5/Unitful/src/User.jl:313

Trying to find where the overflow starts, I find:

julia> upreferred(1u"μ0*c^2")
-2.9634046534192514e9 A^-2 kg m^3 s^-4

Which is probably incorrect.

I'm finding a fair number of these overflow errors happening when trying to work in atomic units.

I did find that comment in the convfact about doing an overflow check :)

@mdavezac
Copy link
Contributor Author

Trying to work out where the overflow comes from

julia> Unitful.basefactor(unit(Unitful.μ0 * 1Unitful.c^2))
(1.0,-2358202494865994080)

So, already in basefactor

mdavezac pushed a commit to mdavezac/Unitful.jl that referenced this issue May 5, 2017
@mdavezac mdavezac mentioned this issue May 5, 2017
mdavezac pushed a commit to mdavezac/UnitfulHartree.jl that referenced this issue May 6, 2017
@ajkeller34
Copy link
Collaborator

I've fixed this with a recent commit—I was being inconsistent in my strategy for overflow detection. I'll tag a new version soon.

As I've implemented it now, there's still no option to do exact conversions in Unitful when the numbers grow bigger than Int64. I consider that more of a feature enhancement than a bug fix though (see also #81). The basefactor methods could probably be rewritten to widen the type rather than giving up and using floating-point. This would be more in line with your PR.

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

2 participants