Pop-Up Thingie
Sidebar
Too Lazy BBS
Home
Forum
Files
Chat
Bulletins
Top doors
MOTD
BBS Stats
Radio
AvatarChat
Dark
Register
Log in
Username
Password
Sidebar
Forum
USENET
cplusplus
trunc(), floor() and ceil() handmade
From
Bonita Montero
@Bonita.Montero@gmail.com to
comp.lang.c++
on Wed Jul 22 19:47:52 2026
From Newsgroup: comp.lang.c++
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.
--- Synchronet 3.22a-Linux NewsLink 1.2
Who's Online
Recent Visitors
Sauron
Mon Aug 17 05:50:20 2026
from
Purgatory
via
Telnet
Sauron
Sun Aug 16 22:51:04 2026
from
Purgatory
via
Telnet
Rixter
Sun Aug 16 22:39:57 2026
from
Madison, Nc
via
Telnet
Rixter
Sun Aug 16 10:20:30 2026
from
Madison, Nc
via
Telnet
System Info
Sysop:
Amessyroom
Location:
Fayetteville, NC
Users:
74
Nodes:
6 (
1
/
5
)
Uptime:
51:11:13
Calls:
1,101
Calls today:
1
Files:
1,339
Messages:
276,013