Hello, I have a math equation `val result = value ...
# announcements
f
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
I'm always hoping the compiler optimizes that for me but I never really confirmed
k
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
Turns out if you put parentheses, the generated bytecode shows the compiler precomputes (100.0/255) and uses 0.39215686274509803 instead
k
Yeah, then it is allowed.
m
So the multiplication takes precedence over the division up there ?
k
Not real precedence, they just work left to right. But basically yes, it's equivalent to
(value * 100.0) / 255
m
Ah, thanks, I always wondered how this works
f
parentheses one is interesting