what do you recommend for expressions in statements concerning e.g. performmace. Examples:
a = 1/2 * sin(2/3 * x) or better 1.0/2.0 * sin(2.0/3.0) or even 1//2 * sin(2//3 * x)
BTW: I don’t want to write 0.5 * sin (0.6666666 * x) or something like that in order to make them similar to the mathematical formula. I would prefer the first variant.
From my Fortran history I was used to avoid internal conversions, e.g. if result (variable a) was double precision (aka Float64) then it was written like 1.d0/2.d0 + sin(…) and not 1/2 * sin(…).
Any recommendations, any performance differences (of course not for a single statement, but if you have thousands of such terms it may matter)? I like to find a final (own) “style” for the re-implementation of some projects in Julia (from Fortran).
So it looks like the only hard-and-fast rule is to not use rational numbers. That makes sense because there aren’t hardware instructions for multiplying rationals.
Good point, because beside the performance an important (even more important) point to me is precision and preserving types, or at least not so do some unexpected. Indeed we have different results in :
Unfortunately, this performs the expensive divisions at runtime. What you want is something like the following, only less ugly:
f5(x::T) where T =(TT=float(T); TT(1/2)*sin(TT(2/3)*x))
That is, you want to preserve single-precision and double-precision, and you want to give the compiler liberty to replace division by multiplication with compile-time computed reciprocals. Then, you also want to work nice with AD types, and you want your code to be readable
No, that computes 2/3 in double precision and then converts to TT, which will lose accuracy if TT is greater precision (e.g. BigFloat). You need TT(2)/TT(3) or similar.
Some of the rational algorithms are fairly complicated (requiring a gcd etcetera), so it seems suboptimal to tie constant propagation to inlining. It seems like it would be better if we could mark // as pure to let it be evaluated at compile-time