简体   繁体   English

不允许出现错误 %

[英]go error % not allowed

I have written a program for Mumax with a go syntax but I don't understant my error.我用 go 语法为 Mumax 编写了一个程序,但我不明白我的错误。 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: %错误信息是:第 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. n 必须是浮点数,因为 TT 必须是浮点数,因为这最终是两个浮点数的函数。 This conflicts with the n := 0 default int definition at the top.这与顶部的 n := 0 默认 int 定义冲突。
  • 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.这意味着你有一个非常奇怪的 Go 实现,或者我们没有看到一切。

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.在任何情况下,要么您要么将 n 强制转换为 int (就像您在第二个 if 中所做的那样),要么以某种方式使用math.Mod

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM