GithubHelp home page GithubHelp logo

Math functions and ADL about units HOT 6 OPEN

nholthaus avatar nholthaus commented on August 15, 2024
Math functions and ADL

from units.

Comments (6)

Morwenn avatar Morwenn commented on August 15, 2024 1

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.

nholthaus avatar nholthaus commented on August 15, 2024

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.

Morwenn avatar Morwenn commented on August 15, 2024

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.

nholthaus avatar nholthaus commented on August 15, 2024

it shouldn't be too hard for me to prototype and find out. I'll give it a shot.

from units.

Morwenn avatar Morwenn commented on August 15, 2024

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.

nholthaus avatar nholthaus commented on August 15, 2024

@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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.