Open Tree-Yang opened 1 year ago
Is this a feature of the language? or is it a bug?
Can't say it's a feature, but as a matter of fact macros aren't composable, in general you can't stick two macros in front of each other and expect them to magically work, because macros can do arbitrary syntax manipulation and one macro may be changing under the feet of the other macro the expression that it sees.
Side note, @fastmath
doesn't do anything with .+=
anyway because macro expansion happens before lowering, see
julia> @macroexpand @fastmath a[1, 1:2:10] .+= 1.0
:(a[1, 1:2:10] .+= 1.0)
in general you can't stick two macros in front of each other and expect them to magically work.
Thank you for the reply. Then I will make sure macros are not used together in my codes.
As for why '@fastmath' appears in front of '@.', the reason is that I put '@fastmath' before the definition of the function, that is,
'@fastmath function myfunc()', and the '@.' macro is used inside the function.
Is this a feature of the language? or is it a bug?
Can't say it's a feature, but as a matter of fact macros aren't composable, in general you can't stick two macros in front of each other and expect them to magically work, because macros can do arbitrary syntax manipulation and one macro may be changing under the feet of the other macro the expression that it sees.
Side note,
@fastmath
doesn't do anything with broadcasting anyway, seejulia> @macroexpand @fastmath a[1, 1:2:10] .+= 1.0 :(a[1, 1:2:10] .+= 1.0)
Can't say it's a feature, but as a matter of fact macros aren't composable, in general you can't stick two macros in front of each other and expect them to magically work, because macros can do arbitrary syntax manipulation and one macro may be changing under the feet of the other macro the expression that it sees.
I think this is a bug:
julia> @macroexpand @fastmath @. a[1, 1:2:10] += 1.0
quote
#= fastmath.jl:116 =#
var"##298" .= a
#= fastmath.jl:117 =#
(var"##299", var"##300") .= (1, 1:2:10)
#= fastmath.jl:118 =#
var"##298"[var"##299", var"##300"] .= Base.FastMath.add_fast.(var"##298"[var"##299", var"##300"], 1.0)
end
If I understand correctly, @a @b x
passes @b x
to the macro a, which then tweaks it. In this particular case, it looks like fastmath just does Base.exprarray(make_fastmath(expr.head), Base.mapany(make_fastmath, expr.args))
, ie applies fastmath recursively to the expression. Shouldn't it expand any macros it encounters before going through? (and shouldn't all macros do this as good practice?)
I am a newcomer to Julia language. However, I found something strange about @fastmath and @. .
This problem happens when slices of matrices or vectors are involved. Please consider the following case:
However, the following code works:
When no slices are involved, this problem will disapper:
My platform is Julia 1.8.1 on Window 11 version 22H2 (OS Build 22621.1.1702). The same problem appears for Julia 1.8.5 on WSL 2 Ubuntu 20.04 LTS.
Is this a feature of the language? or is it a bug?