IDL出现% Program caused arithmetic error: Floating illegal operand 错误!!

用IDL处理NaN数据时,用到where函数时,经常会出现Program caused arithmetic error: Floating illegal operand 错误,查了下发现官网有说明,记录下防下次出错!!

Note: For the minimum (<) and maximum (>) operators with NaN operands, the result is undefined and may not necessarily be the special value NaN.

For example:

; Multiply NaN by 3
PRINT, 3 * !VALUES.F_NAN

IDL prints:

NaN

It is important to remember that the value NaN is literally not a number, and as such cannot be compared with a number. For example, suppose you have an array that contains the value NaN:

A = [1.0, 2.0, !VALUES.F_NAN, 3.0]
PRINT, A

IDL prints:

1.00000     2.00000     NaN     3.0000

If you try to select elements of this array by comparing them with a number (using the WHERE function, for example), IDL might generate an error (depending on the hardware and operating system):

; Print the indices of A that are not equal to 1
PRINT, WHERE( A NE 1.0 )

IDL prints:

1     2     3
% Program caused arithmetic error: Floating illegal operand

(Depending on your hardware and operating system, you may not see the floating-point error.)

To avoid this problem, use the FINITE function to make sure arguments to be compared are in fact valid floating-point numbers:

PRINT, WHERE( FINITE(A) ) 

IDL prints the indices of the finite elements of A:

0     1     3

To then print the indices of the elements of A that are both finite and not equal to 1.0, you could use the command:

good = WHERE( FINITE(A) )
PRINT, good[WHERE(A[good] NE 1.0)]

IDL prints:

1     3

Similarly, if you wanted to find out which elements of an array were not valid floating-point numbers, you could use a command like:

; Print the indices of the elements of A that are not valid 
; floating-point numbers.
PRINT, WHERE( ~FINITE(A) )

IDL prints:

2

Note that the special value Infinity can be compared to a floating point number. Thus, if:

B = [1.0, 2.0, !VALUES.F_INFINITY]
PRINT, B

IDL prints:

1.00000      2.00000          Inf

and

PRINT, WHERE(B GT 1.0)

IDL prints:

1           2

You can also compare numbers directly with the special value Infinity:

PRINT, WHERE(B EQ !VALUES.F_INFINITY)

IDL prints:

2

Note: On Windows, using relational operators such as EQ and NE with the values infinity or NaN (Not a Number) causes an “illegal operand” error. The FINITE function’s INFINITY and NAN keywords can be used to perform comparisons involving infinity and NaN values. For more information, see FINITE.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值