如何强制长双打用Cython的使用情况如何? [英] How do I force usage of long doubles with Cython?
问题描述
我提前道歉为我的C知识贫乏:我使用Python code和写与用Cython使用标准的C函数几个模块以实现速度有很大提高。但是,我需要比 1e308
(是的,你没有看错)高的范围内,这是我目前使用的类型双复数获得
和功能 cexp
和驾驶室
。
I apologize in advance for my poor knowledge of C: I use Python to code and have written a few modules with Cython using the standard C functions to effect a great increase in speed. However, I need a range higher than 1e308
(yes, you read it right), which is what I currently get by using the type double complex
and the functions cexp
and cabs
.
我想使用的功能 cexpl
和 cabsl
,并宣布我的变量是类型长双复
,但 1e308后,我还是会遇到溢出
。这可能意味着我的编译器长双打转换为双打,是这样吗?但根据维基百科,
I tried to use the functions cexpl
and cabsl
, and declared my variables to be of type long double complex
, but I still encounter overflows after 1e308
. This probably means that my compiler converts long doubles to doubles, is that right? But according to Wikipedia,
使用GNU C编译器,在x86处理器上长双80位扩展precision不管用什么类型(它可以是96位或128位)的物理存储的。[4]
With the GNU C Compiler, long double is 80-bit extended precision on x86 processors regardless of the physical storage used for the type (which can be either 96 or 128 bits)..[4]
我使用的是64位系统的Arch Linux(和Python 2.7.8,如果它事项)。
I am using a 64-bit system with arch Linux (and Python 2.7.8 if it matters).
我如何写强制使用长双打的用Cython模块?我需要这样大的数字,对一些科学计算我做的。
How do I write a Cython module that forces the use of long doubles? I need such big numbers for some scientific computation I am doing.
编辑:
旗-m128bit长双编译给出相同的结果。如图所示这里,
在X86-64编译器,-m128bit长双是默认的选择,因为它的ABI指定长双精度是16字节边界对齐。
In the x86-64 compiler, -m128bit-long-double is the default choice as its ABI specifies that long double is to be aligned on 16 byte boundary.
因此,这似乎没有什么区别。
So this seems to make no difference.
我也跑这下面的程序来检查我的系统上的范围:
I also ran this following program to check the range on my system:
#include <stdio.h>
#include <float.h>
int main()
{
printf("Storage size for float : %d \n", sizeof(float));
printf("Minimum float positive value: %E\n", FLT_MIN );
printf("Maximum float positive value: %E\n", FLT_MAX );
printf("Precision value: %d\n", FLT_DIG );
printf("Storage size for double : %d \n", sizeof(double));
printf("Minimum double positive value: %E\n", DBL_MIN );
printf("Maximum double positive value: %E\n", DBL_MAX );
printf("Precision value: %d\n", DBL_DIG );
printf("Storage size for long double : %d \n", sizeof(long double));
printf("Minimum long double positive value: %Le\n", LDBL_MIN );
printf("Maximum long double positive value: %Le\n", LDBL_MAX );
printf("Precision value: %d\n", LDBL_DIG );
return 0;
}
和得到了以下的输出:
Storage size for float : 4
Minimum float positive value: 1.175494E-38
Maximum float positive value: 3.402823E+38
Precision value: 6
Storage size for double : 8
Minimum double positive value: 2.225074E-308
Maximum double positive value: 1.797693E+308
Precision value: 15
Storage size for long double : 16
Minimum long double positive value: 3.362103e-4932
Maximum long double positive value: 1.189731e+4932
Precision value: 18
所以我想问题出在我的code,是这样吗?是否有任何明确的标识符我需要比声明变量,以确保我的程序实际上使用长双打添加其他的?
So I guess the problem lies in my code, is that right? Are there any explicit identifiers I need to add other than declaring variables to ensure that my program actually uses long doubles?
推荐答案
正如看到在编辑的问题,我的系统和行为通过打印正确的范围长双预期编译器
,这里的相关变量是 LDBL_MAX = 1.189731e + 4932
As was seen in the edit to the question, my system and compiler was behaving as expected by printing the correct range for long double
, the relevant variable here being LDBL_MAX = 1.189731e+4932
此外,与用Cython编写的模块提供正确类型的输出长双
。
然而,由于这种类型本身并不在Python支持(请参见这问题),被返回的值大于最大尺寸双击
, 1.797693E + 308
我的系统上更大因此被等同于 + INF + 0J
。因此,这是不相关的GCC所有,但到Python间preting双打长以不正确的方式。
Also, the module written with Cython was correctly giving an output of type long double
.
However, since this type is not natively supported in Python (see this question), the value that was returned was greater than the maximum size of double
,1.797693E+308
on my system and hence being equated to +inf + 0j
. So this was not related to gcc at all, but to Python interpreting long doubles in an incorrect way.
我也希望能解决这个问题通过与另一I2C模块,能够接受长双输入类型和过程进一步,从这个子部分预期的结果预计不会是一个双
(或就此而言,即使是浮动
)。
I can hopefully get around this by working with another C module that can accept long double input types and process it further, the expected results from this sub-part is not expected to be outside the range of a double
(or for that matter, even a float
).
另一个选择可能是使用与Python库,可支持高precision和高范围内的数字,可能 GMPY
Another option might be to use libraries with Python that can support high precision and high range numbers, possibly GMPY.
这篇关于如何强制长双打用Cython的使用情况如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!