Rounding Functions in Phobos

starcanopy starcanopy at
Sun Aug 2 22:43:15 UTC 2020

Is there a reason why ceil, floor, round, and friends return 
floats instead of an integer? There are l' variants for some of 
these, but I'd imagine template implementations of these 
operations would be better. Too much bloat?

Naively, you'd have for ceil...

// Return type would be numeric but the concrete type will be 
whatever is smallest to fit the result.
auto ceil(Float)(Float x) if (isFloatingPoint!Float) { ... }


Integer ceil(Float, Integer = long)(Float x)
     if (isFloatingPoint!Float && isIntegral!Integer)
{ ... }

For reals that are too big for long, I'd guess (u)cent would need 
an implementation.

More information about the Digitalmars-d-learn mailing list