Comments (2)
What units formatting approach is supposed to be?
A few different ones could be considered:
-
Simple one: Put only the quantity (i.e. value from
unit.count()
) into formatted string.
I.e.std::string s = fmt::format("The speed is {} kmph", 2_kmph); // s == "The speed is 2 kmph"
Which is simple in use and appears to be easy in implementation.
The obvious drawback here is that the information about dimensions is not used in formatting.
So, some useful information becomes unavailable in formatting string and it becomes possible to make a mistake in formatting string - write wrong dimensions. -
More complex one: Give ability to refer to both quantity and dimensions from formating string.
In that case special format specification mini language will be required.
Probably, something similar to Date and Time formatting could be taken.For example, like that:
std::string s = fmt::format("The speed is {:%q %d}", 2_kmph); // s == "The speed is 2 kmph"
Or it could be:
std::string s = fmt::format("The speed, {0:%d}: {0:%q}", 2_kmph); // s == "The speed, kmph: 2"
Where
%q
refers to quantity, and%d
refers to dimensions.I think, it might be also possible to make it compliant with standard fmt features. Though the syntax becomes complicated:
std::string s = fmt::format("The speed is {:%q:+.3f: %d:>6:}", 2_kmph); // s == "The speed is +2.000 kmph"
Format specifications are shown here enclosed in columns:
:+.3f:
and:>6:
.Terminating character
:
is necessary to detect the end of quantity/dimensions format specifier.The examples above do not pretend to be the only possible option of format string. They are more the ideas/proposals how could format look like. Just to express to power of it.
The exact format of units formatting string is an open question.
Another open question here is dimensions localization support. Most likely the library need to be extensible by user to provide dimensions string representations in any language other than English. -
The most complex one. Option (2) could be extended even more. To provide ability to have units conversions at formatting time.
It has a few interesting use cases. Though it could be considered later - if option (2) is accepted.
A design note:
As formatting feature might become complicated - ideally if it will be "plug-able" to the units library.
Its implementation might be located in a separate optional header.
i.e.
#include <units/fmt.h>
The rest of library should be able to work without formatting header included.
from mp-units.
Thanks Aliaksander for investigating this subject. As of now, I did not have time to work on this. However, I strongly believe that we should strive to create something similar to http://wg21.link/P1361.
from mp-units.
Related Issues (20)
- Change `mp-units/2.1.0` to `mp-units/2.0.0` in Conan installation steps? HOT 2
- Add support for easier math expressions in `mp-units/math.h` HOT 17
- Should we allow deriving from system entities? HOT 2
- No 2.1.0 in Conan center HOT 3
- ice_point definition is (very very slightly) incorrect
- Do we really need ASCII-only text output? HOT 13
- Text input support
- formatting/printing vector of quantities results in compile time error HOT 3
- Bazel build system support HOT 3
- Should conversions from the raw numerical value to a dimensionless quantity with a unit one be allowed? HOT 26
- Is mp-units targeting freestanding implementations HOT 6
- Provide `wchar_t` support for text output
- Effectively global unit names like 'm' are problematic HOT 35
- compilation time and how to improve it HOT 17
- Can't compile with localization disabled HOT 1
- Can't compile with exceptions disabled HOT 1
- Linker error with base units for "numerical_value_in()" HOT 4
- Clarify concepts and casting rules of quantity kinds and types HOT 6
- I miss `value_cast<ToQ>` and potentially `value_cast<ToQP>` HOT 11
- Version bump HOT 7
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 mp-units.