Opened 7 years ago

Closed 4 years ago

#11739 closed Bugs (duplicate)

operator / for point2 is hard coded to operate on type double

Reported by: c.d.glover@… Owned by: Stefan Seefeld
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 (3)

comment:1 by Mateusz Loskot, 6 years ago

Cc: mateusz@… added
Description: modified (diff)

comment:2 by Stefan Seefeld, 5 years ago

Owner: changed from Hailin Jin to Stefan Seefeld

comment:3 by Mateusz Loskot, 4 years ago

Resolution: duplicate
Status: newclosed
Note: See TracTickets for help on using tickets.