0

I am calling the pow() function of C's math library. My installation is Python3.5 on Windows 10 but I tried the same program in Python2.7 with same result. The return from the function is not as expected. Not sure what's happening. The code is given below. The result I get is 1.0.

from ctypes import *

libc = cdll.msvcrt
libc.pow.argtypes = [c_double, c_int]
libc.pow.restype = c_double
libc.pow(2.3, 2)
coder.in.me
  • 1,048
  • 9
  • 19

3 Answers3

1

There is a type mismatch. Use c_double for power as well. Like this. It should work.

libc.pow.argtypes = [c_double, c_double]

Will_Panda
  • 534
  • 10
  • 26
1

Double check your types, I'm pretty sure the prototype for pow is

double pow(double a, double b);

So changes this line

libc.pow.argtypes = [c_double, c_int]

To

libc.pow.argtypes = [c_double, c_double]

Whenever you call c functions from python always make sure the types you are inputting from python are correct, don't rely on casting like you would in c/c++

  • That's correct. Looks like an interface bug. It should catch this as wrong argument type instead of returning wrong result. – coder.in.me Oct 10 '16 at 07:05
0

pow in C libray takes double, double as input arguments. That's why.

from ctypes import *

libc = cdll.msvcrt
libc.pow.argtypes = [c_double, c_double]
libc.pow.restype = c_double
libc.pow(2.3, 2.0)
stefan_ah
  • 54
  • 4