c++ - Perform rounding on integral type, but not fractional type -
in following function looking expression replace isintegral<t>.
the intention when t integral type add 0.5f before static_cast implictly floors value (and obtain rounded value), when t fractional type add nothing, , static_cast can reduce precision.
t interpolate( t const & prev, t const & next, float interpolation ) { float prevfloat = static_cast< float >( prev ); float nextfloat = static_cast< float >( next ); float result = prevfloat + ( (nextfloat-prevfloat) * interpolation ); return static_cast< t >( result + ( isintegral<t> ? 0.5f : 0.0f ); }
use std::numeric_limits<t>::is_integer (it's in <limits> header).
Comments
Post a Comment