←back to thread

1371 points yett | 1 comments | | HN request time: 0.207s | source
Show context
rossant ◴[] No.43776470[source]
Am I the only one to be annoyed by this...?

while (this->m_fBladeAngle > 6.2831855) { this->m_fBladeAngle = this->m_fBladeAngle - 6.2831855; }

Like, "let's just write a while loop that could turn into an infinite loop coz I'm too lazy to do a division"

replies(4): >>43776533 #>>43776660 #>>43776679 #>>43776703 #
hoten ◴[] No.43776533[source]
for real. The author clearly never heard of fmod
replies(1): >>43779689 #
zerd ◴[] No.43779689[source]
fmod takes in the order of 30+ cycles, probably more in year 2003 CPUs, vs 1 for cmp, 1 for sub, 1 for jmp.
replies(1): >>43786576 #
1. hoten ◴[] No.43786576[source]
Sure the lower bound is nicer here. But when the tradeoff includes an unlimited upper bound it's not a very attractive option.

I guess the most robust code handling both performance and unexpected input would be one iteration of this (leveraging the assumption that angles are either always within the bounds, or had one frame of going out of bounds by a small amount); followed by a fmod if that assumption is just totally off.