Ruby | Math ldexp() function

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

189.0
19.0
18.0
123.0

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

23.0
27.0
1092.0
1087.0

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



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.