Ruby .ceil and .floor

Everything is returned correctly. puts 8/3.ceil == 2 #=> true, because 8/3 returns an Integer, 2 puts 8/3.floor == 2 #=> true, because 8/3 returns an Integer, 2 puts 2.67.ceil == 2 #=> false, because 2.67.ceil is 3 puts 2.67.floor == 2 #=> true, because 2.67.floor is 2 To make things of more sense here, … Read more

Efficient integer floor function in C++

Casting to int is notoriously slow. Maybe you’ve been living under a rock since x86-64, or otherwise missed that this hasn’t been true for a while on x86. 🙂 SSE/SSE2 have an instruction to convert with truncation (instead of the default rounding mode). The ISA supports this operation efficiently precisely because conversion with C semantics … Read more

Round minute down to nearest quarter hour

$seconds = time(); $rounded_seconds = round($seconds / (15 * 60)) * (15 * 60); echo “Original: ” . date(‘H:i’, $seconds) . “\n”; echo “Rounded: ” . date(‘H:i’, $rounded_seconds) . “\n”; This example gets the current time and rounds it to the nearest quarter and prints both the original and the rounded time. PS: If you … Read more

tech