Comments (6)
I am sure there are many ways to improve it, mostly a matter of which way to improve it. It is kind of a problem of infinite number of strings to generate a particular unit, then infinite number of ways to generate a string from that unit.
The trick is figuring out which way is optimum in some sense, and to do it in some way in somewhat efficient maintainable code.
My general thought I suppose is that if you have a way to make better output in the to_string
in a straightforward fashion I am very happy to take a PR or listen to specific suggestions, otherwise for specific applications with a few anomalies, the code does support custom units which can be used to generate specific strings for specific units that might come up in a particular application more regularly but the default output is not ideal.
from units.
I think custom units would make things too complicated for us because we have to handle units in a generic manner.
Can you point me at the relevant piece of code? Is there a description of the algorithm somewhere?
I can't promise that I can find a better way, but I am happy to take a look when I find the time.
from units.
I wish there was a single algorithm, but the reality is that the to_string
operation is more a collection of heuristics to break down the unit and piece together a string that produces the unit exactly if it were used again in unit_from_string
.
For example Line 330 in units.cpp defines an array that is looped through to test out various combinations of those units which are commonly used in combination with others. So if you dealt a lot with angstroms for example. I could see just adding a line to that array for angstroms, that would probably fix your above example. Obviously can't do that for every case, but as a 1 off might make sense.
At some point I want to add a way to add some user defined units into that loop for string generation but not sure when I will get to that.
Also what I meant for custom units is just adding a call to user defined units. This works well if there are just a couple strings or particular units you want the string formulated in a particular way. Not ideal if there are hundreds, but less than 5 probably makes sense. Then that particular unit will bypass all the other heuristics for string generation and just produce that string, all others would stay the same.
from units.
I added a test and capability
addUserDefinedUnit("angstrom", precise::distance::angstrom);
auto str = to_string(units::unit_from_string("us / angstrom^2"));
EXPECT_EQ(str, "us/angstrom^2");
str = to_string(units::unit_from_string("us / angstrom"));
EXPECT_EQ(str, "us/angstrom");
clearUserDefinedUnits();
Basically if you have a specific unit you want prioritized in string generation you can add it to addUserDefinedUnit
from units.
Sounds good, thanks!
This made me think, would it make sense to allow users to override / modify the default testUnits
? We discourage use of, among others, imperial units. So it might be nice to remove ft
, and Mi
from the formatting options.
That would require making testUnits
non-constexpr. Would that have a significant impact on performance?
from units.
I recently shifted the unit string definitions to a constexpr array. Previously it was getting constructed on a stack to make a const unordered_map. So the map itself isn't constexpr so I doubt changing it around would impact performance much.
With the constexpr array used to generate the map it still gets constructed into a const map, but the stack usage is much lower. The map itself it not constexpr, just const. Now that it is split out into a separate file (unit_conversion_maps.hpp) I have toyed with the idea of later on splitting that into a SI compliant part and all others. So then it could be a compile time switch(or possibly runtime options) to make it strictly SI compliant with no additional code or user control. The next step beyond that might be to make the map usage itself a pointer, and allow user modification. From some of the feedback having a pure SI mode and "all units" mode would cover a majority of user cases. The other option I am considering is a also loading the const map from a file in some way to maybe have a chance to support other languages in a little better fashion. But that could get a bit circular.
Anyway there is a bit of evolving of the library that will happen as I get time.
from units.
Related Issues (20)
- Issues with parsing single-letter prefixes such as for `atto` HOT 7
- CleanUnitString breaks on 0.2 and 0.3 HOT 1
- Your units conversion service seems to be down... HOT 11
- "kg m" is parsed as "kg" HOT 1
- Surprising parse results with `.` and `-` HOT 4
- Question about to_string() function HOT 8
- Format arbitrary unit nicely HOT 4
- Degrees celsius get converted to Kelvin in operations HOT 6
- Function to remove a single user defined unit HOT 2
- Try to start a PR for adding support for KPa, MPa, GPa but permission denied HOT 3
- Using to_string for scalars causes crashes HOT 2
- Use of uninitialised memory in unit maps HOT 7
- "Stress testing" units' string parsing HOT 8
- Favoring SI base units in string formatting? HOT 3
- to_string strange output HOT 4
- expression did not evaluate to a constant HOT 4
- Unary negation of measurements HOT 1
- Please consider to use the googletest package instead of requiring the googletest source tree
- Question: How to install units as a vendored library
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from units.