Opened 7 years ago

Last modified 4 years ago

#11739 closed Bugs

operator / for point2 is hard coded to operate on type double — at Version 1

Reported by: c.d.glover@… Owned by: Hailin Jin
Milestone: To Be Determined Component: gil USE GITHUB
Version: Boost 1.59.0 Severity: Problem
Keywords: Cc: mateusz@…

Description (last modified by Mateusz Loskot)

When using boost::gil::point2<float>, applying a division operation results in a point2<double>. This is undesirable.

The fix appears to be easy, change this code

template <typename T> BOOST_FORCEINLINE
point2<double> operator/(const point2<T>& p, double t)      { return t==0 ? point2<double>(0,0):point2<double>(p.x/t,p.y/t); }

to operate on T instead of double. But, that seems overly simple so I am trying to figure out why this code was written this way in the first place.

A possibly safer alternative is to cast the result back to point2<T>, ie;

template <typename T> BOOST_FORCEINLINE
point2<double> operator/(const point2<T>& p, double t)      { return t==0 ? point2<T>(0,0):point2<T>(T(p.x/t),T(p.y/t)); }

This change makes the / operator act the same as the /= operator, which is also hard coded to double, but because it's a member, doesn't yield a new type, so it's probably fine (other than the fact that I asked for floats and am getting double operations).

While we're at it, I also find it weird that this code is protecting against division by zero, but that's a different problem.

Change History (1)

comment:1 by Mateusz Loskot, 6 years ago

Cc: mateusz@… added
Description: modified (diff)
Note: See TracTickets for help on using tickets.