https://kotlinlang.org logo
#announcements
Title
# announcements
f

fangzhzh

07/25/2019, 9:02 PM
Hello, I have a math equation
val result = value * 100.0 / 255
Given that 100.0/255=0.392, is
val result = value * 0.392
better than the equation one in the performance of view.
I like the equation one because it keeps our math equation and is easier to read and understand. But still want to know whether these two have any performance different
m

mbonnin

07/25/2019, 9:19 PM
I'm always hoping the compiler optimizes that for me but I never really confirmed
k

karelpeeters

07/25/2019, 9:20 PM
CLang O3 doesn't optimize it, so it's probably not allowed.
Specifically for floating point you can't really do a lot of optimizations while exactly following IEEE floating point rules.
m

mbonnin

07/25/2019, 9:21 PM
Turns out if you put parentheses, the generated bytecode shows the compiler precomputes (100.0/255) and uses 0.39215686274509803 instead
k

karelpeeters

07/25/2019, 9:21 PM
Yeah, then it is allowed.
m

mbonnin

07/25/2019, 9:23 PM
So the multiplication takes precedence over the division up there ?
k

karelpeeters

07/25/2019, 9:24 PM
Not real precedence, they just work left to right. But basically yes, it's equivalent to
(value * 100.0) / 255
m

mbonnin

07/25/2019, 9:24 PM
Ah, thanks, I always wondered how this works
f

fangzhzh

07/26/2019, 1:16 AM
parentheses one is interesting