本文介绍了如何在Perl中创建或测试NaN或无穷大?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在Perl中创建或测试NaN或无限值?
How do I create or test for NaN or infinite values in Perl?
推荐答案
这是一种相当可靠的方法:
Here's a fairly reliable way:
my $inf = 9**9**9;
my $neginf = -9**9**9;
my $nan = -sin(9**9**9);
sub isinf { $_[0]==9**9**9 || $_[0]==-9**9**9 }
sub isnan { ! defined( $_[0] <=> 9**9**9 ) }
# useful for detecting negative zero
sub signbit { substr( sprintf( '%g', $_[0] ), 0, 1 ) eq '-' }
for my $num ( $inf, $neginf, $nan ) {
printf("%s:\tisinf: %d,\tisnan: %d,\tsignbit: %d\n", $num, isinf($num), isnan($num), signbit($num));
}
输出为:
inf: isinf: 1, isnan: 0, signbit: 0
-inf: isinf: 1, isnan: 0, signbit: 1
nan: isinf: 0, isnan: 1, signbit: 0
这篇关于如何在Perl中创建或测试NaN或无穷大?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!