當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。