红宝石 |数学 ldexp()函数
当分数和指数作为参数给出时,Ruby 中的ldexp()函数返回分数 * (2^exponent)的值。分数是浮点值,指数是整数。
Syntax: Math.ldexp(fraction, exponent)
Parameter: The function takes two mandatory parameter fraction and exponent which specifies the fraction and exponent whose result is returned.
Return Value: The function returns the value of fraction and exponent.
示例 1 :
# Ruby program for ldexp() function
# Assigning values
fraction1, exponent1 = Math.frexp(189)
fraction2, exponent2 = Math.frexp(19)
fraction3, exponent3 = Math.frexp(18)
fraction4, exponent4 = Math.frexp(123)
# Prints the value returned by ldexp()
puts Math.ldexp(fraction1, exponent1)
puts Math.ldexp(fraction2, exponent2)
puts Math.ldexp(fraction3, exponent3)
puts Math.ldexp(fraction4, exponent4)
输出:
189.0
19.0
18.0
123.0
示例 2 :
# Ruby program for ldexp() function
# Assigning values
fraction1, exponent1 = Math.frexp(23)
fraction2, exponent2 = Math.frexp(27)
fraction3, exponent3 = Math.frexp(1092)
fraction4, exponent4 = Math.frexp(1087)
# Prints the value returned by ldexp()
puts Math.ldexp(fraction1, exponent1)
puts Math.ldexp(fraction2, exponent2)
puts Math.ldexp(fraction3, exponent3)
puts Math.ldexp(fraction4, exponent4)
输出:
23.0
27.0
1092.0
1087.0
参考:https://devdocs.io/ruby~2.5/math#method-c-ldexp