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

return type of float(x) #14

Open
stevengj opened this issue Mar 10, 2014 · 6 comments
Open

return type of float(x) #14

stevengj opened this issue Mar 10, 2014 · 6 comments

Comments

@stevengj
Copy link
Collaborator

I would tend to argue that this should preserve the units, even though SIQuantity is not a subtype of FloatingPoint. e.g. float(1V) should give 1.0V.

Not only is the current behavior somewhat counter-intuitive, but it also makes type-stable promotion code just way too tricky to write in a way that preserves the units. (See e.g. SciML/ODE.jl#26, but this also shows up in norm computation and probably other places.)

@ivarne
Copy link

ivarne commented Mar 10, 2014

Excited to see what others think about this issue. cc: @StefanKarpinski

@StefanKarpinski
Copy link
Contributor

See also JuliaLang/julia#1470.

@stevengj
Copy link
Collaborator Author

I definitely agree that e.g. convert(Float64, x::SIQuantity) (which is not defined, by the way), should strip the units. But as pointed out in the thread Stefan cites, float already returns types that are not <: FloatingPoint (e.g. for array arguments) when it makes sense in context.

@Keno
Copy link
Owner

Keno commented Mar 10, 2014

I explicitly did not define the convert method, because I didn't want to the units to ever by explicitly stripped. We tend to allow convert only when the values agree, e.g.:

julia> convert(Int,1.5)
ERROR: InexactError()
 in convert at int.jl:271

This of course raises the question of how do get the value of a unitful quantity in general .val is ugly an not useful in generic code. Right now the way to do this is basically to call float64 (that's what Gadfly does for example). I can see the point that one would like float to do the conversion to floating point but preserve the units, I'm just not sure whether we can make the behavior of float and float64 that different. I'm open to suggestion in any case.

@stevengj
Copy link
Collaborator Author

Ugh, that is a pickle. Having float(x) keep units, while float64(x) strips units, would provide more-or-less the necessarily functionality, but would be a bit ugly.

The problem is that conversion of x to a dimensionful floating-point type is also an important and useful operation too, and there isn't a good way to do it right now. Of course, one can do x * 1.0 but then that will promote single to double precision unnecessarily.

@tomasaschan
Copy link
Collaborator

Bear with me if this is already implemented somewhere, but I couldn't find it (I don't have Julia installed on this computer, so I couldn't check with methods if something like that already exists, but I didn't find it by skimming the package source...):

Why not just have a value(x::SIQuantity) (and a corresponding unit(x::SIQuantity) that return the unitless value (unit) of an SIQuantity?

If one then wants floating point values, specifically, one would have to do e.g. float(value(x)) if x is not SIQuantity{FloatingPoint} already. It's a breaking change if e.g. Gadfly uses float64 to strip units, but it would allow us to write unit-preserving variants of float() et al while still supporting explicit stripping of units.

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

5 participants