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


perl Math::BigInt->is_odd()用法及代碼示例


Math::BigIntPerl中的module提供了代表具有任意精度的整數和重載算術運算符的對象。

is_odd()的方法Math::BigInt模塊用於檢查數字是否存儲為BigIntobject是否為奇數。

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

參數:沒有

返回:如果存儲在BigInt對象中的數字為奇數,則為true,否則返回false。

範例1:用於Math::BigInt->is_odd()方法

#!/usr/bin/perl  
  
# Import Math::BigInt module 
use Math::BigInt; 
  
# Value of n 
$n = '89123751682746'; 
  
# Create BigInt object 
$x = Math::BigInt->new($n); 
  
# Check if the number stored 
# in BigInt object is  
# an odd number or not 
$isOdd = $x->is_odd(); 
  
if ($isOdd) 
{ 
    print "$n is an odd number\n"; 
} 
  
else
{ 
    print "$n is not an odd number\n"; 
} 
  
# Value of n 
$n = '6348762649837957979685908708650797587783'; 
  
# Create BigInt object 
$x = Math::BigInt->new($n); 
  
# Check if the number stored 
# in BigInt object is 
# an odd number or not 
$isOdd = $x->is_odd(); 
  
if ($isOdd) 
{ 
    print "$n is an odd number\n"; 
} 
  
else
{ 
    print "$n is not an odd number\n"; 
}
輸出:
89123751682746 is not an odd number
6348762649837957979685908708650797587783 is an odd number

範例2:用於Math::BigInt->is_odd()檢查八進製數是否為十進製的奇數的方法。

#!/usr/bin/perl  
  
# Import Math::BigInt module 
use Math::BigInt; 
  
# Octal number represented as string 
$octal = 726746425; 
  
# value of octal '726746425'  is 
# 123456789 in decimal number system  
  
  
# Create BigInt object 
$x = Math::BigInt->from_oct($octal); 
  
# Check whether the Octal  
# number stored in BigInt object 
# is an odd number or not  
# using Math::BigInt->is_odd() method 
$isOdd = $x->is_odd(); 
  
if($isOdd) 
{ 
    print "$octal (in octal) is an odd number in decimal"; 
} 
  
else
{ 
    print "$octal (in octal) is not an odd number in decimal"; 
}
輸出:
726746425 (in octal) is an odd number in decimal

範例3:用於Math::BigInt->is_odd()一種檢查十六進製是否為十進製奇數的方法。

#!/usr/bin/perl  
  
# Import Math::BigInt module 
use Math::BigInt; 
  
# Hexadecimal number represented as string 
$hex = 'Ox112210F4B16C1CB1'; 
  
# Hexadecimal value '0x112210F4B16C1CB1'  is 
# 1234567890987654321 in decimal number system  
  
  
# Create BigInt object 
$x = Math::BigInt->new($hex); 
  
# Check whether the hexadecimal 
# number stored in BigInt object 
# is an odd number or not  
# using Math::BigInt->is_odd() method 
$isOdd = $x->is_odd(); 
  
if($isOdd) 
{ 
    print "$hex (in hexadecimal) is an odd number in decimal"; 
} 
  
else
{ 
    print "$hex (in hexadecimal) is not an odd number in decimal"; 
}
輸出:
Ox112210F4B16C1CB1 (in hexadecimal) is an odd number in decimal


相關用法


注:本文由純淨天空篩選整理自ihritik大神的英文原創作品 Perl | Math::BigInt->is_odd() method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。