Open In App

Ruby | BigDecimal ** value

Last Updated : 08 Jan, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

BigDecimal#**() : **() is a BigDecimal class method which return the value of power raised.

Syntax:  BigDecimal.a**n()

Parameter:  BigDecimal values - two operands a, n

Return:  a to the power of n, an

Code #1 : Example for **() method




# Ruby code for BigDecimal.**() method
    
# loading BigDecimal
require 'bigdecimal'
    
# declaring BigDecimal
a = 42.1**13
    
# declaring BigDecimal
b = -BigDecimal("10")
    
# declaring BigDecimal
c = -(22 ** 7.1) ** 10
  
# a raised to b
puts "BigDecimal a ** b : #{a**b}\n\n"
  
# b raised to c
puts "BigDecimal b ** c : #{b**c}\n\n"
  
# c raised to a
puts "BigDecimal c ** a : #{c**a}\n\n"


Output :

BigDecimal a ** b : 0.69715210232947426173607301244803767284033052453728372066634423193813865098214021205394462027375795972963787035593558361900700497580618199181954177853985986229377825727463990264709423242001901437761702739369196154946225636089767529806260002899406861673710067184383173593327137087471327779730396921711103521973868394159296484550564E-211

BigDecimal b ** c : -0.0

BigDecimal a ** c : Infinity

Code #2 : Example for **() method




# Ruby code for BigDecimal.**() method
    
# loading BigDecimal
require 'bigdecimal'
    
# declaring BigDecimal
a = 12**12 - 27
    
# declaring BigDecimal
b = BigDecimal('10')-(22 ** 7.1) ** 10
    
# declaring BigDecimal
c = BigDecimal('-3')
  
# a raised to b
puts "BigDecimal a ** b : #{a**b}\n\n"
  
# b raised to c
puts "BigDecimal b ** c : #{b**c}\n\n"


Output :

BigDecimal a ** b : 0.0

BigDecimal b ** c : -0.115869460505314914957746925940192092903612946396078652748453710504203825643658481195206490785177384593226280520333448499717537935002335320917967720550949163576424480301076551069875568227226802915254668197534820819902843188549314590230983228607331232030126266456748767445151380299132177565231826179376056534445169265616908198098171511469118783824014109443388704362360537966444967894413631049344072193742700836143778689018999609971479716E-285


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads