📜  红宝石 |复杂的 abs2函数

📅  最后修改于: 2022-05-13 01:54:40.064000             🧑  作者: Mango

红宝石 |复杂的 abs2函数

Complex#abs2()是一个 Complex 类方法,它返回复数极坐标形式的绝对值的平方。

示例 #1:

# Ruby code for Complex.abs2() method
  
# declaring Complex value
a = Complex(200)
  
# declaring Complex value
b = Complex(-1, 4)
  
# declaring Complex value
c = Complex('i')
  
  
# use of abs2()  
puts "Complex abs2 form : #{a.abs2}\n\n"
  
puts "Complex abs2 form : #{b.abs2}\n\n"
  
puts "Complex abs2 form : #{c.abs2}\n\n"

输出 :

Complex abs2 form : 40000

Complex abs2 form : 17

Complex abs2 form : 1

示例 #2:

# Ruby code for Complex.abs2() method
  
# declaring Complex value
a = Complex(20, 90)
  
# declaring Complex value
b = Complex('i')
  
# declaring Complex value
c = Complex(100, -500)
  
  
# use of abs2()  
puts "Complex abs2 form : #{a.abs2}\n\n"
  
puts "Complex abs2 form : #{b.abs2}\n\n"
  
puts "Complex abs2 form : #{c.abs2}\n\n"

输出 :

Complex abs2 form : 8500

Complex abs2 form : 1

Complex abs2 form : 260000