- Home /
Why is this loop infinite?
I was using this loop to accelerate the rigidbody to a certain "maxSpeed", but for some reason it was running infinitely.
while ( (rigidbody.velocity.sqrMagnitude) < (maxSpeed*maxSpeed)) {
print(rigidbody.velocity.sqrMagnitude+"|"+maxSpeed*maxSpeed);
rigidbody.velocity += (moveDir.normalized * 0.5 );
rigidbody.velocity = Vector3.ClampMagnitude(rigidbody.velocity,maxSpeed);
print(rigidbody.velocity.sqrMagnitude+"||"+maxSpeed*maxSpeed);
yield WaitForFixedUpdate();
print(rigidbody.velocity.sqrMagnitude+"|||"+maxSpeed*maxSpeed);
}
All the print statements print the same thing when the velocity reaches maxSpeed, which is 4||4.
After spending an hour trying to figure out what was wrong, I it changed from "sqrMagnitude" to "magnitude" and "maxSpeed*maxSpeed" to "maxSpeed" it worked fine.
Then later on I started using this if statement in another part of the code:
print(rigidbody.velocity.magnitude +"||||"+ maxSpeed); print(rigidbody.velocity); if ( rigidbody.velocity.magnitude == maxSpeed ) { state = "walk"; newState = 1; }
Again the print statement is printing the same number for magnitude and maxSpeed, but it's not working. "sqrMagnitude" isn't working either. Also, this code segment problem isn't happening all the time. It depends on how fast the rigidbody accelerated to "maxSpeed". Can someone explain to me how it can print the same number for "maxSpeed" and "rigidbody.velocity.magnitude" and not work. Also, explain to me why "sqrMagnitude" didn't work for the first problem, and then "magnitude" worked.
Answer by syclamoth · Sep 23, 2013 at 06:35 AM
Try using 'Mathf.Approximately' instead of the '==' operator. It takes small floating point values into account, and so will give you a more accurate result.
@Fattie, while technically true, that second statement is grossly unhelpful. An infinite loop doesn't mean "a loop that will never end", it means "a loop that will not ter$$anonymous$$ate unless forcibly exited"
The assertion that 'no loops are infinite' is about as useful for solving the halting problem as this cartoon, though. The term is good for describing certain patterns one finds when debugging software, despite the fact that it is technically untrue.
Actually man the assertion that "no loops are infinite" is a SPECTACULARLY GOOD pedagogical device to introduce thought on the topic; it immediately points out to beginners how, well, utterly poor the term is.
"The term is good for describing certain patterns one finds when debugging software, despite the fact that it is technically untrue."
(1) the term is not good at all. it's a very poorly chosen (in the sense of "the society of programmers over the few decades chose it communually") term - interestingly it, literally, could not be worse as a chosen term. As it is, literally, infinitely incorrect. (Ask any mathematician how far away a number is from infinity.) If I was describing the history of the use of that term I'd say "someone who actually wasn't very funny, but thought they were funny, and additionally knew almost nothing about computers {ie: the sort of programmer who lives only 'at their level of abstraction', you know when you're $$anonymous$$ching a 8 year old to program and they think perl interpreting 'is' the computer?} and zero about trivial math concepts, thought up this term."
The term is catchy but utterly incorrect. So yes, it's catchy, for sure. Like an advertising jingle.
(2) "The term is good for describing certain patterns one finds when debugging software" it's an extremely bad nickname for the issue at hand, man. it's not that funny, and it's hopelessly incorrect, and additionally it fundamentally "makes a way of thinking" that is the worst mistake any programmer can make, at any time - "inside an abstraction" thinking. To program is to meta-abstract. There's nothing else.
(3) "the fact that it is technically untrue" that's sort of like saying "congress lies" is "technically untrue". what does the technically qualifier mean? It's simply a "hopelessly, pathetically incorrect - but catchy - nickname that has spring up."
$$anonymous$$y thoughts on the matter :-)
Ahem - With respect to all you experienced users with '000s of karma between you - please keep (somewhat dubiously off-topic) discussions like this on Forums, not in comments in (answered) Answers!
However, there exist 'infinite loops' that are only finite because of the limitations of hardware / the boundary conditions of the universe. If we are talking about theoretical computers (as in, the kind that have unlimited memory and operate forever), then an infinite loop is a perfectly valid way of describing what happens when you fail to introduce an end condition for some function. So sure, if you're talking about actual computers, it is completely, categorically untrue- but for describing programs as pure mathematical functions (a perfectly valid way of modelling them, from a computer theory perspective), then infinite loops are as real as the concept of infinity.
To summarise- just because you can't count to infinity, doesn't mean it's not a useful 'number'.
Your answer
Follow this Question
Related Questions
Adding player velocity to projectile not working 0 Answers
Rigidbody magnitude 3 Answers
increase float when another one decreases 1 Answer
checking angular magnitude 1 Answer
Detecting When all rigidBodies have stopped moving 2 Answers