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

show unit symbols for common units #54

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

jonasrauber
Copy link

I think it would be nice to see the unit symbol for common units, e.g. N for Newton instead of kg m s⁻².

julia> using SIUnits

julia> using SIUnits.ShortUnits

julia> 10J
10 J

julia> 10J/s
10 W

julia> kg*m/s
kg m s⁻¹

julia> kg*m/s^2
N

@Keno
Copy link
Owner

Keno commented Jan 10, 2015

I had thought about this, but was concerned about cases where there was two possible representations, e.g. J or N m. Thoughts?

@jonasrauber
Copy link
Author

We could display both, i.e. 10 J or N m, and/or provide the user with a way to overwrite this according to the context. Furthermore, this feature could be optional adding a switch to turn it on or off.

@tomasaschan
Copy link
Collaborator

I'd rather do this as a separate function call, than automatically, i.e. something like

julia> using SIUnits, SIUnits.ShortUnits
julia> 10N
10 kg m/s^2
julia> simplifyunits(10 N * m)
10 J
julia> simplifyunits(10 N * m; commons=[N,m])
10 N m

where my idea with the optional parameter (which could probably be better named) is we have a predefined list of "common" units to which we contract, but the user can override it to alter the behavior - e.g. to choose to display N m instead of J.

Doing it this way has the added benefit that it won't alter the behavior of existing code.

@jonasrauber
Copy link
Author

When I played around with this, I initially also did it using a separate function. However, it became kind of annoying to always call simplifyunits all the time, because in 99% of the cases, that's what I wanted. That's why I thought the user should be able to choose that units are always simplified when they are printed.

@tomasaschan
Copy link
Collaborator

@jonasrauber, sure, but how does the user get to choose which units are used? I see no functionality in this PR that allows that flexibility.

@jonasrauber
Copy link
Author

@tlycken no, you are right, that's currently not part of this PR. I thought someone might have a better idea to solve this problem. Do you know any other julia package that gives the user the choice to configure the output of show ?

@mbauman
Copy link
Collaborator

mbauman commented Jan 12, 2015

This is somewhat similar to the GRISU/show_shortest problem when displaying floats. It'd be really cool to automagically print the shortest representation of the units, but it seems like a traveling-salesman problem to me… especially once you start considering combinations of powers of derived units. But then showall could show the longer base SI representation.

For my purposes, though, I almost always use microvolts or milliamps, so something simple along these lines would make most of what I do more intuitive.

@ma-laforge
Copy link

Hi Keno/Everyone,

Along the same theme, I implemented facilities to display numeric values using SI prefixes (M, k, m, u, n, ...)

using SIUnits
using SIUnits.ShortUnits

f=100e6Hz # => 1.0e8 s-1
w=2pi*f # => 6.283185307179586e8 s-1

using NumericIO
println("Angular freq: ", Formatted(w.val,:SI,ndigits=4), " rad/s")
# => Angular freq: 628.3M rad/s

Question
Do you think this would be a good addition to the SIUnits module?

If so, the code can be found under (MIT-lic)
https://github.com/ma-laforge/NumericIO.jl

This was referenced Dec 20, 2016
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

Successfully merging this pull request may close these issues.

5 participants