mpusz / mp-units

The quantities and units library for C++
https://mpusz.github.io/mp-units/
MIT License
1.07k stars 85 forks source link

Unicode output for fractional exponents #59

Open mpusz opened 4 years ago

mpusz commented 4 years ago

Provide nice Unicode symbols for fractional exponents wherever possible.

using namespace units;
using namespace units::si::literals;

std::cout << sqrt(1m) << "\n";
oschonrock commented 4 years ago

which unicode code points? Couldn't obviously find superscript fractions. Or did you mean "radical" symbols like an actual square root. That would be most unusual? or did you just mean m^½ currently it prints: m^(1/2)

mpusz commented 4 years ago

I think that "m½" is self-explanatory and we might not need additional "^" sign but we can decide otherwise. Here is the info how to create any ratio: https://en.wikipedia.org/wiki/Unicode_subscripts_and_superscripts.

oschonrock commented 4 years ago

yes. I know about "composing fractions using sub/superscripts", but those are not "superscripts overall" and therefore only questionably suitable for representing an exponent?

In fact I think it's logical that fractional superscripts don't exist as a font glyph. The single digit superscripts are difficult enough to read at normal font sizes, so fractional superscripts would likely be unreadable below 20pt? Really to display this sort of thing properly we need a "1.5 x line height" (at least) layout object, and we clearly can't do that without outputting postscript or latex.

So you are suggesting, we abandon the idea of the these actually being exponents/superscripts?

and just write (effectively) "m * 1/2" ?

mpusz commented 4 years ago

So you are suggesting, we abandon the idea of the these actually being exponents/superscripts? and just write (effectively) "m * 1/2"?

No. If we are about to stay with ASCII I think that the current spelling "m^(1/2)" is better. It is that in one of the libraries I analyzed I saw they use unicodes for fractional exponents too and the resulting dimensions were easier to read (in my opinion).

oschonrock commented 4 years ago

That's a misunderstanding. I agree that the best ASCII is the status quo.

What I was trying to point out is that

to my eyes (and this is subjective) looks more like

m * (1/2)

than like

m^(1/2)

actually m½ doesn't look like anything very sensible to my eyes.

In my opinion writing non-trivial mathematical expressions nicely is not really possible "on a single line of text". That's why there are specialised languages for that. And a basic convention for how to use ASCII, which is not too pretty, but unambiguous.

i-ky commented 4 years ago

My understanding is that fractional exponents will usually appear in intermediate results and will be printed mostly for debugging purposes. I don't see much need in perfecting their looks.

mpusz commented 4 years ago

There are ony a few physical quantities that really use them. I was provided one of them some time ago but I lost it and do not remember the name of the quantity.

i-ky commented 4 years ago

I did some googling and it led me to this discussion (funny enough, about another physical units library) and there are a couple of examples:

  1. fracture toughness measured in Pa · m1/2
  2. amplitude spectral density involving Hz−1/2
oschonrock commented 4 years ago

@i-ky what you have there looks much better to my eyes!

superscript, but just simple '/' fraction...you can still see it's an exponent

unless someone objects I'll just implement that ... but not go overboard since it seems this is pretty niche

i-ky commented 4 years ago

@oschonrock what I'm using there is not plain Unicode, I've cheated using <sup>...</sup>. And although you can get superscript digits, there is no way to represent superscript solidus with Unicode.

oschonrock commented 4 years ago

@i-ky Damn, but somehow I smelled that.

Oh well, this can remain on low priority table then, given we are struggling to come up with a use case outside of "amplitude spectral density".

mpusz commented 4 years ago

Well, we can always cheat: https://stackoverflow.com/a/49325148 ;-)