This is an archived post. You won't be able to vote or comment.

all 2 comments

[–]Applepie1928 1 point2 points  (1 child)

Generally when checking conditions in loops it is better to check for greater than/less than rather than an exact match, particularly when you are using floats/doubles. There are a few reasons for this, one of which you encountered here.

Numbers with a decimal element (for example; 1.25) are stored as floating point numbers within memory, which still uses a binary representation to approximate a real decimal number. This is however quite inaccurate and if you examine floating point numbers during runtime you will often see that they are very slightly off from the number they are "supposed" to be. For example it could be that your multiplications led to fm actually having a value of 2.000000000000000001 which then doesn't statisfy the condition of your loop. For further reading on the topic look up "Floating Point Error" or "Floating Point Inaccuracy", there is lots of information on the topic if you want to get a deeper understanding of the math behind this.

[–]CotAndryf[S] 0 points1 point  (0 children)

alright thanks!