Git Product home page Git Product logo

Comments (7)

stefanseefeld avatar stefanseefeld commented on May 27, 2024

What is the possible range (set of valid types) of T ? I think I have written similar code in the past, where my assumption was that the divisor must be a wide floating point value for the operation to make sense, which would explain the second double argument. And then it would make sense to use simple type promotion to compute the return type, which may just be double, too, in this case.
(I'm just thinking aloud, trying to mentally reproduce why the code was written the way it is.)

from gil.

christian-henning avatar christian-henning commented on May 27, 2024

@mloskot Is there not a better point class in boost? Wouldn't that be better than using gil::point2?

from gil.

stefanseefeld avatar stefanseefeld commented on May 27, 2024

It's such a trivial class...
I'm not sure it would be worthwhile dragging in another dependency just for that. Besides, there are rather specific requirements for it (See http://boostorg.github.io/gil/html/design_guide.html#point) that I'm not sure any pre-existing class would meet without additional adapter code.
(And speaking of requirements: the /= signature using double actually comes from the concept itself.)

from gil.

mloskot avatar mloskot commented on May 27, 2024

I agree with @stefanseefeld that pulling Boost.Geometry or whatever as a dependency would not buy as anything really, but potential maintenance hassle.

I will look at implementing the idea of promoting the return type to best float-point type matching the operands.

from gil.

mloskot avatar mloskot commented on May 27, 2024

@stefanseefeld & @chhenning
How about replacing the current double-hardwired point<T>::operator/ with template function auto-promoting the base T of its point<T> result using std::common_type which I had no idea about until today :-)

template <typename T, typename D>
auto operator/(point<T> const& p, D d)
{
    static_assert(std::is_arithmetic<D>::value, "denominator is not arithmetic type");
    using numeric_type = typename std::common_type<T, D>::type;
    return point<numeric_type>(p.x / t, p.y / t);
}

Test:

point<int> p1;
static_assert(std::is_same<decltype((p1 / short{}).x), int>::value, "!int");
static_assert(std::is_same<decltype((p1 / int{}).x), int>::value, "!int");
static_assert(std::is_same<decltype((p1 / float{}).x), float>::value, "!float");
static_assert(std::is_same<decltype((p1 / double{}).x), double>::value, "!double");

point<float> p2;
static_assert(std::is_same<decltype((p2 / int{}).x), float>::value, "!float");
static_assert(std::is_same<decltype((p2 / float{}).x), float>::value, "!float");
static_assert(std::is_same<decltype((p2 / double{}).x), double>::value, "!double");

from gil.

stefanseefeld avatar stefanseefeld commented on May 27, 2024

Yeah, perfect !

from gil.

mloskot avatar mloskot commented on May 27, 2024

Fixed by #157

from gil.

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.