发生错误%不允许

I have written a program for Mumax with a go syntax but I don't understant my error. Here the script where the error appears :

n:=0

Dtr0:=5*1e-12
Dtd0 :=300*1e-12
Dtf0:=5*1e-12
Dtz0:=20000*1e-12
tr0:=Dtr0
td0:=Dtd0+tr0
tf0:=Dtf0+td0
tz0:=Dtz0+tf0
TT:=tz0
n=t/TT
tr:=tr0+(n*TT)
td:=td0+(n*TT)
tf:=tf0+(n*TT)
tz:=tz0+(n*TT)

if (n % 2 == 0) {
        if (n<1 && t<tr) {
                a:=(t/tr)
        } else if (n>=1 && t>=tz0+((n-1)*TT) && t<tr) {
                a:=1/(tr-(tz0+((n-1)*TT)))*(t-(tz0+((n-1)*TT)))
        } else if (t>=tr && t<=td) {
                a:=1
        } else if (t>td && t<=tf) {
                a:=(-1/(tf-td))*(t-td)+1
        } else if (t>tf && t<tz) {
                a:=0
        }
}
if (int(n)%2==1) {
        if (n<1 && t<tr) {
                a:=-(t/tr)
        } else if (n>=1.0 && t>=tz0+((n-1)*TT) && t<tr) {
                a:=-(1/(tr-(tz0+((n-1)*TT)))*(t-(tz0+((n-1)*TT))))
        } else if (t>=tr && t<=td) {
                a:=-1
        } else if (t>td && t<=tf) {
                a:=-((-1/(tf-td))*(t-td)+1)
        } else if (t>tf && t<tz) {
                a:=0
        }
}

And the error message is : line 37: if (n % 2 == 0) {: not allowed: %

Thank's a lot

There are two problems here:

  • n must be a float because TT must be a float, because that's ultimately a function of two floats. This conflicts with the n := 0 default int definition at the top.
  • the modulus operator on floats is undefined (see this playground for what happens when you try).

This means you have a very weird Go implementation or we're not seeing everything.

In any case, either you have to either coerce n to an int (as you do in your second if) or use math.Mod somehow.