Pop-Up Thingie
Sidebar
d0p3 BBS
Home
Forum
Files
Dark
Register
Log in
Username
Password
Sidebar
Forum
Fidonet
COMP.LANG.C++
trunc(), floor() and ceil() handmade
From
Bonita Montero
@3:633/10 to
All
on Wed Jul 22 19:47:52 2026
double xtrunc( double v ) noexcept
{
static_assert( sizeof( double ) == 8 && numeric_limits<double>::is_iec559 );
constexpr uint64_t Sign = ~(~0ull >> 1);
constexpr int NoFract = 0x3FF + 52;
uint64_t
bin = bit_cast<uint64_t>( v ),
nsBin = bin & ~Sign;
int fractBits = NoFract - (int)(nsBin >> 52);
if( fractBits <= 0 ) [[unlikely]]
return v;
if( fractBits > 52 ) [[unlikely]]
return bit_cast<double>( bin & Sign );
bin &= ~0ull << fractBits;
return bit_cast<double>( bin );
}
double xfloor( double v ) noexcept
{
double truncd = xtrunc( v );
if( v - truncd == 0.0 ) [[unlikely]]
return v;
return truncd - (v >= 0.0 ? 0.0 : 1.0);
}
double xceil( double v ) noexcept
{
double truncd = xtrunc( v );
if( v - truncd == 0.0 ) [[unlikely]]
return v;
return truncd + (v >= 0.0 ? 1.0 : 0.0);
}
+/-Inf and +/-NaN also work.
--- PyGate Linux v1.5.18
* Origin: Dragon's Lair, PyGate NNTP<>Fido Gate (3:633/10)
Who's Online
Recent Visitors
RufusT
Tue May 19 12:56:22 2026
from
Dallas, TX
via
RLogin
Guest
Tue May 26 22:56:58 2026
from
Melbourne
via
Telnet
Guest
Sun Jun 14 06:31:07 2026
from
Ny
via
Telnet
Frostydev
Mon Jul 13 05:56:46 2026
from
Maryland
via
Raw
System Info
Sysop:
Tetrazocine
Location:
Melbourne, VIC, Australia
Users:
12
Nodes:
8 (
0
/
8
)
Uptime:
03:04:55
Calls:
220
Files:
21,513
Messages:
82,368