GithubHelp home page GithubHelp logo

unit_t as constexpr about units HOT 5 CLOSED

nholthaus avatar nholthaus commented on August 15, 2024
unit_t as constexpr

from units.

Comments (5)

nholthaus avatar nholthaus commented on August 15, 2024 1

A few things:

  • I assume you're working from branch 2.2.x? The master branch hasn't had any constexpr support pulled in yet.
  • Assuming that, I'm pretty sure it's not possible to do constexpr class assignment in c++11 because constexpr implies const. That requirement was relaxed in c++14.
  • c++11 also has a requirement that constexpr functions are written in a single return statement, which this library generally does not conform to, because it was also relaxed in c++14.
  • Currently, assignment operators in units aren't marked constexpr, and my hazy recollection is because if they were you wouldn't be able to compile units with c++11 at all because of the above-mentioned property of constexpr. That said, units is a c++14 library so I will reserve the right to change that on a whim in future versions.

Overall, my approach to compiling units with c++11 is this: if it works for your application, great :) If not, you may need a c++14 compiler. C++11 as a standard isn't really sufficient for a proper library implementation in my opinion.

workaround

I'm also pretty sure (that on branch 2.2.x) you could refactor that statement and have it compile as expected (using c++11). Try:

// not sure area is the right name for a unit of length
constexpr units::length::meter_t area(5);

Also, I think even with gcc 4.9.8 you can use the -std=c++1y to compile with c++14, but it looks like the relaxed constexpr requirements didn't make it in until gcc 5. Even so, that would be my recommendation when compiling with units, if possible.

workaround 2

Units has a type called unit_value_t which was intended for compile-time constant unit values for compilers without constexpr support (primarily visual studio 2013). It's basically a std::ratio that has a dimension associated with it. Check out the Pythagorean Theorem unit test and its Right Triangle Class to see how it works.

In many ways unit_value_t is actually better than constexpr, because it supports compile-time pow and sqrt, which <cmath> doesn't have. The only caveat is that since it's a ratio your values need to be rational numbers (or rational approximations). Usage would look something like:

using a = unit_value_t<meters, 5>;    // access this in a calculation with `a::value()` static function.

from units.

nholthaus avatar nholthaus commented on August 15, 2024 1

Glad you found one that works for you! I'm very familiar with PhysUnits-CT-Cpp11 and it's a great library, and Martin is well known for creating modern libraries with a wide range of compiler support.

It definitely looks like the assignment can be done in c++11 from your example so I'll still probably incorporate that into my next library version.

Aside from the occasional issue like this one, I think the choice between those libraries mostly comes down to your taste for the boost-like quantity syntax or the unit-centric syntax.

I implemented my original CT sqrt using a recursive Newton-Raphson method, but did not like the performance I was getting (lots of arithmetic overflow in the ratios), and ended up switching to a continued fraction method with a CT binary search to determine the initial conditions. I also think it's best to keep all conversions as rational values until the very end, since all conversion factors (minus PI) are rational anyway, and it minimizes floating-point rounding errors.

from units.

nholthaus avatar nholthaus commented on August 15, 2024 1

This is confirmed working in the v2.2.x branch and the soon-to-be v2.2.0 release. I think your testing must have been done on a copy of the master branch units.h which didn't include constexpr support prior to v2.2.0.

from units.

ax3l avatar ax3l commented on August 15, 2024

@nholthaus thank you for the feedback!

We tried out several (zero-overhead) unit systems yesterday, looking for one to perform conversions at CT, running with gcc 4.9 and C++11 for CUDA 7.5 compatibility, and actually found that PhysUnits-CT-Cpp11 allows what I was looking for:

#include "phys/units/quantity.hpp"
#include "phys/units/physical_constants.hpp"

using namespace phys::units;
using namespace phys::units::literals;

constexpr quantity<length_d, float> dx = 0.6e-6_m;

// we are in a device kernel now, and conversion can be performed as a C++11 constexpr:
__device__ void f()
{
    constexpr float dx_scaled = dx.to( unit_length );
    constexpr float Lx = 100. * dx;
    // ... do work
}

The unit_value_t examples you linked are pretty impressive for pre-constexpr, cool!

In case that is interesting for you, my colleague @psychocoderHPC found a stackoverflow answer yesterday with C++11 constexpr sqrt for floating point vars.

from units.

MikeGitb avatar MikeGitb commented on August 15, 2024

FYI: There is no assigment operator involved in the original example. Just the copy constructor.

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.