如何使用numpy longdouble dtype? [英] How do I use the numpy longdouble dtype?

查看:220
本文介绍了如何使用numpy longdouble dtype?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的Python代码中使用np.longdouble dtype,并试图使用NumPy来处理从使用Cython编译的C模块中获得的长双精度.

I am trying to work with the np.longdouble dtype in my Python code, and am trying to use NumPy to manipulate long doubles that I get from a C module compiled with Cython.

假设我这样做:

import numpy as np

print np.finfo(np.longdouble)
Machine parameters for float128
---------------------------------------------------------------------
precision= 18   resolution= 1e-18
machep=   -63   eps=        1.08420217249e-19
negep =   -64   epsneg=     5.42101086243e-20
minexp=-16382   tiny=       3.36210314311e-4932
maxexp= 16384   max=        1.18973149536e+4932
nexp  =    15   min=        -max
---------------------------------------------------------------------


a = np.longdouble(1e+346)

a
Out[4]: inf

b = np.longdouble(1e+347)

b
Out[6]: inf

c = a/b
/usr/lib/python2.7/site-packages/spyderlib/widgets/externalshell/start_ipython_kernel.py:1:
RuntimeWarning: invalid value encountered in longdouble_scalars
  # -*- coding: utf-8 -*-

c
Out[8]: nan

a.dtype, b.dtype, c.dtype
Out[9]: (dtype('float128'), dtype('float128'), dtype('float128'))

本质上,它与

In essence, it is linked to the same issue as in this question and I understand that Python first converts the 1e+346 into a float, whose represntation would be inf. However, can someone suggest a workaround? Is there a way to create NumPy longdoubles that are not converted to floats first?

我有一个C模块,可以输出长双精度数,我想在dtype np.longdouble的numpy数组中使用它.

I have a C module that can output long doubles, which I want to use in a numpy array of dtype np.longdouble.

即使该解决方案涉及重新编译Python/NumPy,我也愿意尝试.

Even if the solution involves re-compiling Python/NumPy, I am willing to try it.

推荐答案

您可能需要考虑几件事.

There are a few things you might want to take into account.

首先,这是一团糟. NumPy知道longdoublefloat128.不幸的是,名称令人误解,其基础实现是C long double,通常(但不一定总是)是80位浮点数. (实际上,您可以通过查看精度"在此处看到它; 18位数字约为60位,而80位浮点数的尾数则为64位.如果使用实际的128位浮点数,则精度约为34位. )

First, this is a mess. NumPy knows longdouble and float128. Unfortunately, the names are misleading, the underlying implementation is C long double, which is usually (but not necessarily always) an 80-bit float. (Actually you can see it here by looking at "precision"; 18 digits is approximately 60 bits, and the 80-bit float has 64 bits in its mantissa. The precision would be around 34 digits if real 128-bit floats were used.)

可能没有任何直接方法将长双精度作为参数传递给C函数,但是如果您改为传递指针,则可以避免此问题.例如,您可以将数组数据作为uint8传递(通过使用myarray.view(dtype='uint8')),并将指向缓冲区的指针强制转换为C程序中的long double *.至少那么Python与类型转换无关. (很可能您不需要使用view,因为毕竟您只需要导出一个指向数组缓冲区的指针.)

There may not be any direct way to pass long doubles as arguments to a C function, but if you pass pointers instead, then you can avoid the problem. For example, you may pass your array data as uint8 (by using myarray.view(dtype='uint8')) and the cast the pointer to the buffer into long double * in your C program. At least then Python has nothing to do with type conversions. (Most probably you do not need to take the view because you are, after all, only exporting a pointer to the array buffer.)

请注意,此技巧依赖于在编译Python和C程序时具有相同类型设置的编译器.除了精度差异外,还可能存在字节顺序差异(如果程序在同一台计算机上运行,​​则很少发生)和对齐差异.我的Python似乎将longdouble项以16个字节的边界对齐(即每个元素始终有6个字节的零),但是C编译器可能会使用10/12/16个字节的对齐方式.

Just beware that this trick relies on the compiler having the same kind of type settings when compiling Python and your C program. In addition to precision differences, there may be byte order differences (rarely if the programs run in the same machine) and alignment differences. My Python seems to align the longdouble items at 16 byte borders (i.e. there is always 6 bytes of zeros per each element), but the C compiler may use 10/12/16 byte alignment.

据我所知,细节是特定于实现的.因此,这是可行的,但需要格外小心,并且可能存在可移植性问题.

As far as I know, the details are implementation specific. So, this is doable but requires some extra care and there may be portability issues.

这篇关于如何使用numpy longdouble dtype?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆