Reputation: 79
Good day,
as C# newbie i'm facing another problem with C# that in C is not problem at all.
I have this method that calls other methods and looks like this.
public double ipoval(double[] xa,double[] ya, int n, double x, int ipord)
{
ulong ind, jnd;
double y, dy;
if (ipord>=1)
{
hunt(xa,Convert.ToUInt64(n),x, ind);
jnd = Math.Min(Math.Max(ind - ipord / 2, 1), n - ipord);
polint(xa + jnd - 1, ya + jnd - 1, ipord + 1, x, y, dy);
}
return y;
}
Now it shows me this errors for jnd:
Looks like C# have a problem with subtracting two different data types. Should i convert that int ipord into ulong?
And method polint has an error of kinda similar type:
Any ideas? Thank you in advance for help.
I have same thing in C and it works:
NRREAL ipoval(NRREAL xa[], NRREAL ya[], const int n, const NRREAL x, const int ipord)
{
unsigned long ind,jnd;
NRREAL y,dy,*y2;
if (ipord>=1) { /* polynomial interpolation of ipord-th order */
hunt(xa,(unsigned long)n,x,&ind);
jnd=IMIN(IMAX(ind-ipord/2,1),n-ipord);
polint(xa+jnd-1,ya+jnd-1,ipord+1,x,&y,&dy);
}
return y;
}
Btw little side question, in that polint method in c# with parameters like this:
public void polint(double[] xa, double[] ya, int n, double x, ref double y, ref double dy)
because i want y,dy as output from that void method.
Sorry for long post and thanks once again.
Upvotes: 1
Views: 587
Reputation: 450
I think this should do it:
public double ipoval(double[] xa,double[] ya, int n, double x, int ipord)
{
ulong ind = 0, jnd = 0;
double y = 0, dy = 0;
if (ipord>=1)
{
hunt(xa,(ulong)n,x, ind);
jnd = (ulong)Math.Min(Math.Max(ind - (ulong)ipord / 2, 1), n - ipord);
polint(xa[jnd - 1], ya[jnd - 1], ipord + 1, x, ref y, ref dy);
}
return y;
}
Upvotes: 1
Reputation: 4288
You have a couple of errors; first, you must assign an initial value to a variable, whether it be new ulong()
or a direct value such as ulong a = 0;
Next, in the line where you use use Math.Max(...)
you use a minus operator between an int
and a ulong
, this throws another error; when using 2 different numerical types such as the ones above, you most of the time want to cast the one with the smaller range to the one with the larger range because the limit of the smaller one is, well smaller, the larger variable can most surely contain the smaller one. Before I give an example, there is another error in the same line; there are multiple Math.Min
s, one for a decimal
and one for a float
. The problem is that a ulong
can be implicitly casted to either of these, meaning that you can do float r = b //where b is a ulong
or decimal r = b //where b is still a ulong
so, you must explicitly cast to specify that you want the decimal, or the float version of Math.Min
Here is an example:
jnd = (ulong)Math.Min(Math.Max(ind - (ulong)ipord / 2, 1), (decimal)(n - ipord));
Have a nice day!!!
Upvotes: 0