Ranter
Join devRant
Do all the things like
++ or  rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Comments

FastNop2865457dUsually, % is the modulo operator which gives the rest of an integer division. If the numerator is smaller than the divisor (both positive), the result is always identical with the numerator.

gitpush3412657d@FastNop in this case what do I need to do to get 3333? And not 10? I'm a bit confused here

gitpush3412657d@FastNop the numbers I gave are examples, what I'm trying to achieve, I have an audio file , and two params:
Current position and Duration, I want to get percentage completed of playing, I want to know percentage played of file 
cabbybaby192657d22 % 5 = 2
17 % 5 = 2
12 % 5 = 2
7 % 5 = 2
2 % 5 = 2
22 / 5 = 4 + 2/5
17 / 5 = 3 + 2/5
12 / 5 = 2 + 2/5
7 / 5 = 1 + 2/5
2 / 5 = 0 + 2/5
Shamelessly taken from https://stackoverflow.com/questions... 
24thDragon222557d(position/duraton) * 100
Make sure position and duration are floats or doubles. 
FastNop2865457d@gitpush Ahhh. In that case, you could either use floating point numbers instead of integers, or you could first multiply by 100 and then stay with integer division.
Like:
is_length_float / total_length_float * 100
Or:
(is_length_int * 100)/total_length_int
In the latter case, you need to check whether your integer range will always be large enough to hold 100*total_length. Otherwise, use float. 
gitpush3412657d@24thDragon that was always giving me 0 since position is always smaller than duration

netikras2401857d```
> 10%30
10
> 10/30
0.3333333333333333
>
```
```
jshell> (double)10%30
$1 ==> 10.0
jshell> (double)10/30
$2 ==> 0.3333333333333333
jshell>
```
```
echo "$((10%30))"
10
```
```
>>> 10%30
10
```
I mean they can't be all wrong, can they..? 
gitpush3412657d@24thDragon @FastNop so the point is to always use Float, my numbers are long, once converted to float I'll reach my goal right?

FastNop2865457d@gitpush Should work. Just remember to always do your tracking in int and only use the float conversion for the final display step. Otherwise, rounding errors with float operations can add up and give subtle bugs.
Oh, and you need to cast the ints to floats before doing the division of course, not after the result. Like (C syntax):
percentage = ((float) position) / ((float) total) * 100.0;
Also remember to check whether you even have a total length greater than 0. Otherwise, you will have a division by zero crash if the user puts in an audio file with length 0. 
gitpush3412657d@FastNop OK noted thank you for your help (and everyone else who commented on this rant)

magicMirror705757dWhat am I even looking at here?
sheesh.
At least make sure duration != 0.
integer, float, or anything numberish. 
Demolishun797857d@gitpush
Please don't try this either: percent == 100. Learn about checking if the float is in a small range close to the value 100. Now percent >= 100 would prob be okay. But only if your division and mult always produce a value > 100.0.
Not sure if I'm making a mistake or what, but in Kotlin:
10 % 30 = 10 ðŸ¤”
Shouldn't it be 33333...?
No matter what numbers I use it always returns the left hand side of the equation ðŸ¤”
random
is it a bug?
am i doing it wrong?