How to work out loading percents?

Blitz3D Forums/Blitz3D Programming/How to work out loading percents?

Crinkle(Posted 2011) [#1]
This sounds so simple and should be, but its not working for no reason i can fathom. My program is working its way through a file, i want to update the screen as its loading through it.

I have "LINES" set up to count the line the program is on, and "TOT_LINES" which just lets the function know how many lines the file has in total.

I am under the impression that to get the percentage as a decimal, i simply do:

LINES/TOT_LINES*100

However this is constantly returning 0.0, despite the fact that LINES and TOT_LINES are updating correctly. Where have i gone wrong?


Floyd(Posted 2011) [#2]
LINES/TOT_LINES*100 would be evaluated as first LINES/TOT_LINES and then multiply that by 100. If LINES and TOT_LINES are both integers then LINES/TOT_LINES will use integer division and evaluate to zero.

Try (100*LINES)/TOT_LINES, or using floating point numbers.


Crinkle(Posted 2011) [#3]
Thanks, i tried that (didn't wanna resort to floating point. It works now, somtimes i feel like such a noobar!


Kryzon(Posted 2011) [#4]
Working from Floyd's suggestion; getting a floating point result from that calculation is as simple as doing:

val = (100.0*LINES)/TOT_LINES

This makes Blitz3D return a float.


Adam Novagen(Posted 2011) [#5]
Never worry about "resorting" to floating point, Crinkle. For an equation like this, it's the only way you'll ever get the results. Floats are accurate to a perfectly "acceptable" degree, with a few discrepancies; it's only when you need scientific accuracy, or things like financing, that they need to be replaced with other methods.