1
gitpush
57d

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 🤔

Comments
  • 10
    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.
  • 0
    @Fast-Nop in this case what do I need to do to get 3333? And not 10? I'm a bit confused here
  • 20
    @gitpush You could add 3323 to 10 to get 3333.
  • 2
    @Fast-Nop Or multiply it with 333.3
  • 0
    @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
  • 6
    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...
  • 2
    (position/duraton) * 100

    Make sure position and duration are floats or doubles.
  • 1
    @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.
  • 0
    @24th-Dragon that was always giving me 0 since position is always smaller than duration
  • 1
    @gitpush thats because you are using integers. Cast them to float or double
  • 2
    ```
    > 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..?
  • 0
    @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?
  • 1
    @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.
  • 1
    @Fast-Nop OK noted thank you for your help (and everyone else who commented on this rant)
  • 1
    @gitpush I just added the total length check. ^^
  • 1
    @Fast-Nop haha thanks man :D
  • 4
    What am I even looking at here?
    sheesh.

    At least make sure duration != 0.
    integer, float, or anything number-ish.
  • 6
    Sounds like an first-day school question.
  • 2
    @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.
Add Comment