1

I have a function which looks like

cdef double __test_func(double x, double y, double z):
 return (x-y)/((2*x-y)*y)**(0.5*z)
def test_func(x, y, z):
 return __test_func(<double>x, <double>y, <double>z)

What I want to do is to use this test_func in python without regarding whether I typed a dot or not like a real python function. The <double> part is what I wrote after reading the Cython guide, but honestly I am not sure if I did right, especially when they called it "type casting". Because, if I recall correctly, type casting isn't exactly the same thing as type conversion.

And as I suspected, this is not a great idea. This function works somehow as I wrote. But, if I were to type the arguments of the python wrapper,

ctypedef fused Numeric:
 char
 short
 int
 long
 long long
 float
 double
 long double
cdef double __test_func(double x, double y, double z):
 return (x-y)/((2*x-y)*y)**(0.5*z)
def test_func(Numeric x, Numeric y, Numeric z):
 return __test_func(<double>x, <double>y, <double>z)

and after compiling and loading it in a python shell, if I give the argument x as something integer, for instance, x=100, then it gets horribly wrong and gives completely different result to when I give x=100., as if I did the type casting in C.

So, how do I ensure the type conversion is done correctly in cython files? Specifically, how do I convert it to double-precision floating point (64bit floating point)? I know in python it is synonymous to float, but given it is cython, I am not entirely sure what precision float(x) would end up.

asked Nov 24, 2024 at 7:44
3
  • 1
    I think you're overthinking this and your first go is right. I'm not sure you even need <double>. A Python float object will be correctly converted to a C double Commented Nov 24, 2024 at 11:18
  • @DavidW Well, you tend to overthink when you don't really know that much. The first one is working fine, but I am still puzzled why the second one is not working "as intended". It is technically possible that I pass non-python object by accident (for instance, I get a number from another function which is technically np.float64 or np.int64 instead of python object), and I'd really love to safeguard against predictable situations beforehand. Commented Nov 25, 2024 at 6:55
  • For your second version every argument is the same numeric type. So if you give argument 1 as an integer then it casts all the other arguments to an integer too (before casting them to float) Commented Nov 25, 2024 at 7:32

0

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.