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

defining unit relative to prefixed unit causes problems #26

Closed
ggggggggg opened this issue Sep 27, 2016 · 1 comment
Closed

defining unit relative to prefixed unit causes problems #26

ggggggggg opened this issue Sep 27, 2016 · 1 comment
Labels

Comments

@ggggggggg
Copy link
Contributor

I defined a Liter unit based on 1000cm^3, and it seemed to work most of the time, but then failed on some operations. If I instead define it based on m^3, then it seems to work as expected. I added the following lines to Defaults.jl to test this:

@unit L     "L"         Liter       (1//1000)*m^3           true
@unit altL  "altL"      altLiter    1000*cm^3               true
@unit arb   "arb"       Arb         1nA                     true

And then did the following tests at the REPL that all work.

julia> a = 1u"altL"
1 altL
julia> a = 1u"maltL"
1 maltL
julia> dimension(a)
𝐋^3

But then trying to get back to a unitless number fails

julia> Float64(1u"altL/cm^3")
ERROR: DomainError:
Cannot raise an integer x to a negative power -n. 
Make x a float by adding a zero decimal (e.g. 2.0^-n instead of 2^-n), or write 1/x^n, float(x)^-n, or (x//1)^-n.
 in power_by_squaring(::Int64, ::Int64) at ./intfuncs.jl:118
 in basefactorhelper(::Float64, ::Int64, ::Int64, ::Rational{Int64}) at /Users/oneilg/.julia/v0.5/Unitful/src/Unitful.jl:268
 in map(::Unitful.#basefactor, ::Tuple{Unitful.Unit{:Meter},Unitful.Unit{:altLiter}}) at ./tuple.jl:93
 in basefactor(...) at /Users/oneilg/.julia/v0.5/Unitful/src/Unitful.jl:276
 in convfact(...) at /Users/oneilg/.julia/v0.5/Unitful/src/Conversion.jl:85
 in convert at /Users/oneilg/.julia/v0.5/Unitful/src/Conversion.jl:183 [inlined]
 in Float64(::Unitful.Quantity{Int64,Unitful.Dimensions{()},Unitful.Units{(Unitful.Unit{:Meter}(-2,-3//1),Unitful.Unit{:altLiter}(0,1//1)),Unitful.Dimensions{()}}}) at ./sysimg.jl:53

The version based on m^3 works for going to a unitless number

julia> Float64(1u"L/cm^3")
1000.0

And arb (based on nA) shows the same error as altL

julia> Float64(1u"arb/A")
ERROR: DomainError:
Cannot raise an integer x to a negative power -n. 
Make x a float by adding a zero decimal (e.g. 2.0^-n instead of 2^-n), or write 1/x^n, float(x)^-n, or (x//1)^-n.
 in power_by_squaring(::Int64, ::Int64) at ./intfuncs.jl:118
 in basefactorhelper(::Float64, ::Int64, ::Int64, ::Rational{Int64}) at /Users/oneilg/.julia/v0.5/Unitful/src/Unitful.jl:268
 in map(::Unitful.#basefactor, ::Tuple{Unitful.Unit{:Ampere},Unitful.Unit{:Arb}}) at ./tuple.jl:93
 in basefactor(...) at /Users/oneilg/.julia/v0.5/Unitful/src/Unitful.jl:276
 in convfact(...) at /Users/oneilg/.julia/v0.5/Unitful/src/Conversion.jl:85
 in convert at /Users/oneilg/.julia/v0.5/Unitful/src/Conversion.jl:183 [inlined]
 in Float64(::Unitful.Quantity{Int64,Unitful.Dimensions{()},Unitful.Units{(Unitful.Unit{:Ampere}(0,-1//1),Unitful.Unit{:Arb}(0,1//1)),Unitful.Dimensions{()}}}) at ./sysimg.jl:53
@ajkeller34
Copy link
Collaborator

Fixed on master and added a test for this condition. The fix will be included in next release or you can Pkg.checkout("Unitful") if you want to be on master.

mdavezac pushed a commit to mdavezac/Unitful.jl that referenced this issue Oct 30, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants