How to convert an argument of a function from int to real in sml? - sml

This is the code I wrote in sml for calculating a harmonic sum. I basically want to calculate real numbers
as for integers all would evaluate to 0(not of any use). But this code is giving error.
Trial 1:
if y<x then 0
else f(x,y-1)+ 1/y;
Error:
Elaboration failed: Type clash. Functions of type "real * real → real" cannot take an argument of type "int * int": Cannot merge "int" and "real".
Trial 2:
if y<x then 0
else f(real(x),y-1)+ 1/y;
error:
Elaboration failed: "real" is not a constructor.
Even replacing 0 with 0.0 didn't work. Please help.

I got the answer to my question.
We basically need to use
if y<x then 0.0
else f(x,y-1)+ 1.0/real(y);
since the type of our function is (int*int)->real

Related

Incompatible ranks 0 and 1 in assignment at (1) in Fortran [duplicate]

I'm working in a finite difference method on an irregular grid, this is the important part of the code:
IMPLICIT DOUBLE PRECISION (A-Z)
REAL*16 IPSI,ICORR,POT(20000),VA(20000),delta1(20000),
$delta2(20000),R(20000),a,b,d
COMPLEX Y(20000),TY2(50000),Z(20000),PSI0(20000),RES,DPSI,C,
$CORR,OPK
DO I=3,NR-1
delta1=R(I)-R(I-1)
delta2=R(I+1)-R(I)
a=(2/(delta1*(delta1+delta2)))
b=(-2/(delta1*delta2))
d=(2/(delta2*(delta1+delta2)))
TY2(I)=((d*Z(I+1))+(b*Z(I))+(a*Z(I-1)))
ENDDO
When I try to compile I got Error: Incompatible ranks 0 and 1 in assignment at (1) for a,b,d and TY2. Any solution will be appreciated. Thanks!
a=(2/(delta1*(delta1+delta2)))
b=(-2/(delta1*delta2))
and the following lines are illegal. On the right you have arrays, on the left a scalar.
Maybe you forgot some index like delta1(I) or delta1 should be a scalar. We can't say without knowing more about your code.

Error: Incompatible ranks 0 and 1 in assignment at (1)

I'm working in a finite difference method on an irregular grid, this is the important part of the code:
IMPLICIT DOUBLE PRECISION (A-Z)
REAL*16 IPSI,ICORR,POT(20000),VA(20000),delta1(20000),
$delta2(20000),R(20000),a,b,d
COMPLEX Y(20000),TY2(50000),Z(20000),PSI0(20000),RES,DPSI,C,
$CORR,OPK
DO I=3,NR-1
delta1=R(I)-R(I-1)
delta2=R(I+1)-R(I)
a=(2/(delta1*(delta1+delta2)))
b=(-2/(delta1*delta2))
d=(2/(delta2*(delta1+delta2)))
TY2(I)=((d*Z(I+1))+(b*Z(I))+(a*Z(I-1)))
ENDDO
When I try to compile I got Error: Incompatible ranks 0 and 1 in assignment at (1) for a,b,d and TY2. Any solution will be appreciated. Thanks!
a=(2/(delta1*(delta1+delta2)))
b=(-2/(delta1*delta2))
and the following lines are illegal. On the right you have arrays, on the left a scalar.
Maybe you forgot some index like delta1(I) or delta1 should be a scalar. We can't say without knowing more about your code.

boost_check_equal_collections for double arrays

I have a question with BOOST_CHECK_EQUAL_COLLECTIONS. It is straightforward to compare the calculated array with the expected one if the data type of the array happens to be int or char. However, when the data type is float or double, using BOOST_CHECK_EQUAL_COLLECTIONS becomes very tricky. For example,
std::vector<float> dis_array;
Distance<float,float>(pt_array,base_point,dis_array);
std::vector<float> dis_ground_truth;
dis_ground_truth.push_back(3.6055f);
dis_ground_truth.push_back(1);
dis_ground_truth.push_back(9.2194f);
BOOST_CHECK_EQUAL_COLLECTIONS(dis_ground_truth.begin(), dis_ground_truth.end(),
dis_array.begin(),dis_array.end());
However, when running BOOST unit test framework, the following error message appear:
{ dis_array.begin(), dis_array.end() } failed.
Mismatch in a position 0: 3.6055 != 3.60555
Mismatch in a position 2: 9.2194 != 9.21954
The problem comes from comparing two variables of double type, and it is very hard to give one variable the exact expected value if its type is double. For the now being, the only solution I can think of is to compare the difference of the two arrays, and if the norm of the difference is below than a small threshold, then it will pass the unit test. Any other ideas? Thanks!

Fortran get complex number from a real number.

I am working on writing a Fortran which has to solve square root and that results in getting a complex number, but Fortran doesn't print it or passes that to another variable. It gives (NaN, 0.000).
This is a dummy code to represent the problem that i am having with the actual code i am working on. If you guys can give me any information that would be helpful. Thank You.
program test
IMPLICIT NONE
COMPLEX X
REAL a, b, c
a = 1
b = 1
c = 1
X = sqrt(b - 4*a*c)
print *, REAL(X), ' - j',-AIMAG(X)
end program test
Since a, b and c are all reals, the expression on the right-hand side of the assignment will be calculated in real arithmetic. Assigning it to a complex variable on the left-hand side doesn't change that. If you want the calculation done as a complex value, the easiest way is to declare a, b, and c as complex.

handling exceptions in ML

everyone, I'm trying to understand how exceptions work in ML, but I have strange error, and I can't figure out what is wrong:
exception Factorial
fun checked_factorial n =
if n < 0 then
raise Factorial
else n;
fun factorial_driver () =
checked_factorial(~4)
handle
Factorial => print "Out of range.";
what may be wrong? thanks in advance for any help.
You need to make sure that factorial_driver has a consistent type. The non-exceptional case returns int, so ML infers the function to be of type unit -> int, but the exceptional case (that is, the print expression) returns unit, not int.
Generally, you basically need to return a value of the same type in all cases.