Open In App

Ruby | Math ldexp() function

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

The ldexp() function in Ruby returns the value of fraction * (2^exponent) when fraction and exponent are given as parameters. The fraction is a float value and the exponent is an integer.

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.

Example 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)


Output:

189.0
19.0
18.0
123.0

Example 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)


Output:

23.0
27.0
1092.0
1087.0

Reference: https://devdocs.io/ruby~2.5/math#method-c-ldexp



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads