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
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
Usually, % 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.
-
@Fast-Nop in this case what do I need to do to get 3333? And not 10? I'm a bit confused here
-
@Fast-Nop 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 -
22 % 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... -
@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. -
@24th-Dragon that was always giving me 0 since position is always smaller than duration
-
```
> 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..? -
@24th-Dragon @Fast-Nop so the point is to always use Float, my numbers are long, once converted to float I'll reach my goal right?
-
@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. -
@Fast-Nop OK noted thank you for your help (and everyone else who commented on this rant)
-
What am I even looking at here?
sheesh.
At least make sure duration != 0.
integer, float, or anything number-ish. -
@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?