Thread View: comp.lang.c++
1 messages
1 total messages
Started by Bonita Montero
Mon, 23 Nov 2026 17:01
A minimal nextafter()
Author: Bonita Montero
Date: Mon, 23 Nov 2026 17:01
Date: Mon, 23 Nov 2026 17:01
35 lines
2274 bytes
2274 bytes
template<bool Up, typename Fp>
constexpr Fp fp_next( Fp v ) noexcept
{
using namespace std;
static_assert( is_floating_point_v<Fp> );
constexpr bool Dbl = is_same_v<Fp, double>;
using word = conditional_t<Dbl, uint64_t, uint32_t>;
using sword = make_signed_t<word>;
constexpr word
Sign = Dbl ? 1ull << 63 : 1u << 31,
Exp = Dbl ? 0x7FFull << 52 : 0xFFu << 23;
sword sBin = bit_cast<word>( v );
word uBin = sBin & ~Sign;
sword mxDiff = uBin - Exp;
if( mxDiff > 0 ) [[unlikely]] // NaN
return v;
if( !uBin ) [[unlikely]] // +/-0
sBin = Up ? 0 : Sign;
bool toInf = (sBin < 0) ^ Up;
if( !mxDiff && toInf ) [[unlikely]] // +/-Inf -> +/-Inf
return v;
sBin += toInf ? 1 : -1;
if( word exp = sBin & Exp; // result == inf / denormal
exp == Exp || !exp ) [[unlikely]]
{
if constexpr( (math_errhandling & MATH_ERREXCEPT) )
feraiseexcept( FE_INEXACT | (exp == Exp ? FE_OVERFLOW : FE_UNDERFLOW) );
if constexpr( (math_errhandling & MATH_ERRNO) )
errno = ERANGE;
}
return bit_cast<Fp>( sBin );
}
A bit different sicne the direction is a template bool.
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads