Comments (6)
To be honest my static_math library was merely a toy project: I hardly know anything about numerical analysis, Taylor series and all those things, and most of the difficult parts have been contributed by other people; I wouldn't be able to fix bugs in those functions. For a quality implementations of constexpr
math functions I would rather look at the math parts of Bolero Murakami's Sprout library (which also contains many more things, but the math part can probably be safely extracted).
I've looked at your code and it does indeed work with ADL nowadays, but mostly because ADL is a bit too greedy: it currently looks in the associated namespace of a class (and the associated namespaces of its base classes) for a function with the appropriate name. This is fine, but be careful: if P0934 makes it into a future standard revision, ADL will only look in a the associated namespace of a class (or base classes) for functions that explicitly name the class (or base class) and won't pick unconstrained templates in the associated namespaces. The proposal probably won't make it into the standard because it breaks stuff that works today (including your ADL-found math functions), but it also solves several problems due to ADL being too greedy :/
from units.
Honestly, I guess I just never use ADL because I pretty much exclusively use fully qualified namespaces in my code, and I didn't put thought into it. I think it would be a great addition.
I was worried about ambiguities during the original design, but I don't think there actually are many, if any (unless the committee later decides to template <cmath>
or something).
Not being an ADL expert, is the fact that the units themselves are separated into namespaces (e.g. units::length
, units::time
) also going to prevent ADL from determining the correct math functions if they were defined in the units
namespace?
from units.
Honestly I'm not sure how it works with nested namespaces. I guess that if all your unit classes inherit from a base class and you place your math functions in the base class namespace and make them take the base class, then it may work.
But I'd have to read more about ADL and check whether this is true. I'm totally unsure about whether it would actually work.
from units.
it shouldn't be too hard for me to prototype and find out. I'll give it a shot.
from units.
I remembered about this issue and thought about this a bit: when an unqualified call to a function is found, ADL will look up in the namespace of the argument to find a function with the appropriate name, but also in the namespaces of the base classes of the argument.
Basically this means that you could have a hidden namespace with a base_unit
type and all the math functions taking base_unit
in the same namespace. Every unit could then inherit from base_unit
to benefit from ADL-found math functions. More realistically, it is clear that only a subset of math functions works for a given unit (hence the static assertions on unit categories of the current design), so you would need to have more fine-grained hidden unit bases for every unit categories so that ADL only works when a math function is applied to a unit that is supposed to work with it.
This solution would probably work well for the units defined in the library, but I'm not sure about user-defined ones; they probably would have to perform more work by themselves. That said, users already can't bypass the static assertions in the current math functions unless they make their units satisfy the traits (and those traits probably don't always cover their own unit categories), so the ADL solution would actually be more scalable despite requiring some work from user-defined unit implementers. I still think that making generic algorithms that find math functions via ADL work with unit is a problem worth solving :)
from units.
@Morwenn So I implemented this in my 3.0 dev branch and it's working really nicely, good call on your part. It's actually really making me want to improve the math section further, namely by supporting as many constexpr
math functions as possible. Is your smath library in a good place to pull in as a dependency to enable that?
Right now I'm planning to give up on the single-header approach here since the build systems have matured a lot and it's still bogging down the compile time.
from units.
Related Issues (20)
- 2.3.2 Fails to compile HOT 2
- Conan package out of date HOT 2
- Undefined references to .name() and .abbreviation() HOT 1
- Incorrect enable_if condition for operator+ HOT 1
- Request for branch and pull request permissions HOT 5
- percent_t FROM double and TO double are different HOT 9
- 2.3.3 does not compile
- Add a way to specify units when "downcasting" to numeric type HOT 2
- Support the MSFS SDK HOT 2
- Empty base class optimization for MSVC
- Does not compile with GCC 12 HOT 1
- raw() and value() is error prone HOT 4
- [Bug] i386 (32-bit) fails to compile
- Shouldn't the naming of units::torque::foot_pound be changed
- v2 -> v3 porting HOT 2
- Need help implementing resistance as a new custom unit. HOT 2
- unit conversion emits a surprisingly high amount of instructions
- Compilation under MINGW
- Math functions not compatible with percent HOT 1
- Status of the project HOT 14
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.