If you are running 32-bit 3.6 on Windows, please test this

Peter Otten __peter__ at web.de
Fri Sep 1 04:23:57 EDT 2017


Pavol Lisy wrote:

> On 8/31/17, 20/20 Lab <lab at 2020fresno.com> wrote:
>>
>>
>> On 08/31/2017 01:53 AM, Pavol Lisy wrote:
> [...]
>> Valid point, fired up a windows 10 machine and worked as well.
>>
>> Python 3.6.2 (v3.6.2:5fd33b5, Jul 8 2017, 04:14:34) [MSC v.1900 32 bit
>> (Intel)] on win32
>> Type "copyright", "credits" or "license()" for more information.
>>  >>> import math
>>  >>> math.sqrt(1.3)
>> 1.140175425099138
>>  >>>
>>
>> This machine does not have the creators update yet.  So there's that.
>> --
>> https://mail.python.org/mailman/listinfo/python-list
> 
> Thx! :)
> 
> Could somebody help me?
> 
> I was trying to call sqrt using ctypes from msvcrt but I am not succesful:
> 
> import ctypes
> msc = ctypes.windll.msvcrt
> 
> def msqrt(arg):
>     s = ctypes.create_string_buffer(100)
>     d = ctypes.c_longdouble(arg)
>     msc.sprintf(s, b'arg = %g', d)
>     print(s.value.decode())
>     r = msc.sqrt(d)
>     msc.sprintf(s, b'sqrt = %g', r)   # r is int so this format is
> wrong I just like to show my intention
>     print(s.value.decode())
>     print("r = ", r, r.__class__)
> 
>>>> msqrt(1.3)
> arg = 1.3
> sqrt = 0
> r =  0 <class 'int'>
> 
>>>> msqrt(-1)
> arg = -1
> sqrt = 4.00144e-320   # because wrong format in sprintf
> r =  8099 <class 'int'>
> 
> And ->
> 
>>>> msc.sqrt.restype
> ctypes.c_long
> 
>>>> msc.sqrt.argtypes is None
> True
> 
> How to do it properly? Isn't ctypes broken?

I think you have to specify the types yourself:

>>> import ctypes
>>> libm = ctypes.cdll.LoadLibrary("libm.so")
>>> libm.sqrt(42)
0
>>> libm.sqrt.argtypes = [ctypes.c_double]
>>> libm.sqrt.restype = ctypes.c_double
>>> libm.sqrt(42)
6.48074069840786





More information about the Python-list mailing list