我有一个 JavaScript 方法,我需要将它转换为标准 Java,但是我对这些行有一点问题
if (jsum) sum += bj;
jsum=!jsum;
和
return x < 0.0 && (n & 1) ? -ans : ans;
这显示了错误:运算符是 undefined。我知道我不能在 Java 中使用这种语法,但我不明白什么是正确的语法来保持原始目的而没有问题
完整的 JS 方法是
function bessel_fkb(n,x)
{
if (n == null || n.length == 0) return "n not specified";
var iac = 40.0;
var bigno = 1.0e10;
var bigni = 1.0e-10;
var j,jsum,m,ax,bj,bjm,bjp,sum,tox,ans;
ax=Math.abs(x);
if (ax == 0.0) return 0.0;
else if (ax > n) {
tox = 2.0/ax;
bjm=BesselJ0(ax);
bj=BesselJ1(ax);
for (j=1;j<n;j++) {
bjp=j*tox*bj-bjm;
bjm=bj;
bj=bjp;
}
ans=bj;
} else {
tox=2.0/ax;
if (Math.sqrt(iac*n) >= 0)
m=2*((n + Math.floor(Math.sqrt(iac*n))) / 2);
else
m=2*((n + Math.ceil(Math.sqrt(iac*n))) / 2);
jsum=0;
bjp=ans=sum=0.0;
bj=1.0;
for (j=m;j>0;j--) {
bjm=j*tox*bj-bjp;
bjp=bj;
bj=bjm;
if (Math.abs(bj) > bigno) {
bj *= bigni;
bjp *= bigni;
ans *= bigni;
sum *= bigni;
}
if (jsum) sum += bj;
jsum=!jsum;
if (j == n) ans=bjp;
}
sum=2.0*sum-bj;
ans /= sum;
}
return x < 0.0 && (n & 1) ? -ans : ans;
}
我的完整 Java 转换尝试是:
public double bessel_fkb(double n, double x)
{
double iac = 40.0;
double bigno = 1.0e10;
double bigni = 1.0e-10;
double jsum,m,ax,bj,bjm,bjp,sum,tox,ans;
ax=Math.abs(x);
if (ax == 0.0) return 0.0;
else if (ax > n) {
tox = 2.0/ax;
bjm=BesselJ0(ax);
bj=BesselJ1(ax);
for (int j=1;j<n;j++) {
bjp=j*tox*bj-bjm;
bjm=bj;
bj=bjp;
}
ans=bj;
} else {
tox=2.0/ax;
if (Math.sqrt(iac*n) >= 0)
m=2*((n + Math.floor(Math.sqrt(iac*n))) / 2);
else
m=2*((n + Math.ceil(Math.sqrt(iac*n))) / 2);
jsum=0;
bjp=ans=sum=0.0;
bj=1.0;
for (int j=(int)m;j>0;j--) {
bjm=j*tox*bj-bjp;
bjp=bj;
bj=bjm;
if (Math.abs(bj) > bigno) {
bj *= bigni;
bjp *= bigni;
ans *= bigni;
sum *= bigni;
}
if (jsum) sum += bj;
jsum=!jsum;
if (j == n) ans=bjp;
}
sum=2.0*sum-bj;
ans /= sum;
}
return x < 0.0 && (n & 1) ? -ans : ans;
}