当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


perl Math::BigInt->is_even()用法及代码示例


Math::BigIntPerl中的module提供了代表具有任意精度的整数和重载算术运算符的对象。

is_even()的方法Math::BigInt模块用于检查数字是否存储为BigInt对象是否相等。

用法: Math::BigInt->is_even()

参数:没有

返回:如果存储在BigInt对象中的数字是偶数,则为true,否则返回false。

范例1:用于Math::BigInt->is_even()方法

#!/usr/bin/perl  
  
# Import Math::BigInt module 
use Math::BigInt; 
  
# Value of n 
$n = '89132506319263974586'; 
  
# Create BigInt object 
$x = Math::BigInt->new($n); 
  
# Check if the number stored 
# in BigInt object is even or not 
$isEven = $x->is_even(); 
  
if ($isEven) 
{ 
    print "$n is an even number\n"; 
} 
  
else
{ 
    print "$n is not an even number\n"; 
} 
  
# Value of n 
$n = '98793270075788553683446589224555431'; 
  
# Create BigInt object 
$x = Math::BigInt->new($n); 
  
# Check if the number stored 
# in BigInt object is even or not 
$isEven = $x->is_even(); 
  
if ($isEven) 
{ 
    print "$x is an even number"; 
} 
  
else
{ 
    print "$x is not an even number"; 
}
输出:
89132506319263974586 is an even number
98793270075788553683446589224555431 is not an even number

范例2:用于Math::BigInt->is_even()检查十六进制数字是否为十进制偶数的方法。

#!/usr/bin/perl  
  
# Import Math::BigInt module 
use Math::BigInt; 
  
# Hexadecimal string 
$hexValue = '0x24CB016EA'; 
  
  
# value of '0x24CB016EA' in 
# decimal number system is 9876543210 
  
  
# Create BigInt object 
$x = Math::BigInt->new($hexValue); 
  
# Check whether the Hexadecimal  
# number stored as BigInt object 
# is an even number or not  
# using Math::BigInt->is_even() method 
$isEven = $x->is_even(); 
  
if($isEven) 
{ 
    print "$hexValue is an even number in decimal"; 
} 
  
else
{ 
    print "$hexValue is not an even number in decimal"; 
}
输出:
0x24CB016EA is an even number in decimal

范例3:用于Math::BigInt->is_even()检查二进制数是否为十进制偶数的方法。

#!/usr/bin/perl  
  
# Import Math::BigInt module 
use Math::BigInt; 
  
# Binary string 
$binary = '0b1001001100101100000001011011101010'; 
  
  
# 0b1001001100101100000001011011101010 
# is 9876543210 in decimal  
  
# Create BigInt object 
$x = Math::BigInt->new($binary); 
  
# Check whether the Binary 
# number stored as BigInt object 
# is an even number or not  
# using Math::BigInt->is_even() method 
$isEven = $x->is_even(); 
  
if($isEven) 
{ 
    print "$binary is an even Number in decimal"; 
} 
  
else
{ 
    print "$binary is not an even number in decimal"; 
}
输出:
0b1001001100101100000001011011101010 is an even Number in decimal


相关用法


注:本文由纯净天空筛选整理自ihritik大神的英文原创作品 Perl | Math::BigInt->is_even() method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。