Problem with calling lapack_make_complex_{float,double} with C/C++

本文介绍了一个在C++中使用LAPACK库进行复数运算时出现的异常问题及解决方案。作者通过编写简单的测试例发现,使用lapack_make_complex_double函数创建复数时会导致程序崩溃。为解决该问题,作者自行编写了make_complex函数来替代,并成功解决了异常问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

I wrote a simple test routine:

for(int i = 0; i < 100;++i)
 {
  std::cout<< "i="<< i <<": ";
  print_complex(lapack_make_complex_double(1,0));
  std::cout<< std::endl;
 }
where print_complex prints the complex to console.

The routine will end with an exception:



First-chance exception at 0x00420fd0 in LTELinkLevelSimulator.exe: 0xC0000005: Access violation reading location 0x00000000.
Unhandled exception at 0x770b15de in LTELinkLevelSimulator.exe:0xC0000005: Access violation reading location 0x00000000.

 and the printed complexes on the console are:


See, something goes wrong. The complex should be 1+0j, but the lapack_make_complex_double function, which is a C interface to create a complex with double precision, is not stable.

I have to write my own make_complex routine instead. It looks like:

lapack_complex_double make_complex(constdouble a, const double b)
{
 lapack_complex_double c;
 c.real = a;
 c.imag = b;

 return c;
}
If replace the lapack_make_complex_double with make_complex, the output is:


No error happens again!

One last word, when using the CBLAS and LAPACKE library in your C/C++ code, be very careful of such flaws.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值