help with floating variables
Blitz3D Forums/Blitz3D Beginners Area/help with floating variables
| ||
Hello, I wrote a code to detect accuracy, but it won't show numbers less than one. enemys_hit = 1 bullets_shot = 2 accuracy# = (enemys_hit/bullets_shot) Print accuracy# WaitKey It seems like it would work but it doesn't! What did I do wrong? |
| ||
Your dividing two INT's. You won't get a float result like that. Change the enemys_hit to a float, and it will work :o) |
| ||
I've noticed that you have to that even when doing a fraction like 1/2, which would return a 0. You have to put 1.0/2 or 1/2.0 or 1.0/2.0 |
| ||
Hi you have to force one or another (enemys_hit or bullets_shot ) to be float: enemys_hit = 1 bullets_shot = 2 accuracy1# = Float(enemys_hit)/bullets_shot accuracy2# = enemys_hit/Float(bullets_shot) Print accuracy1+", "+accuracy2 WaitKey enemys_hit / bullets_shot : will give you 0.0 because both are integers and the quotient is integer 0, accuracy is a float, and print shows you as 0.0 accuracy# = float(enemys_hit / bullets_shot) will give you the same 0.0 result, because blitz firs do the division, resulting an integer 0, and then converts it to float: 0.0 when mixing integers and floats we all have to take care! best regards Juan edit: two were writing at the time I do the post!, so I came third |
| ||
Even changing enemys_hit = 1.0 will produce the incorrect result. You need to use the # to change the variable to a float. However, this will work: accuracy# = (Float(enemys_hit)/Float(bullets_shot)) |
| ||
thanks, I get it now. |