Open In App

Ruby | Math frexp() function

The frexp() function in Ruby returns a two-element array containing the normalized fraction which is a float value and an exponent that is an integer value of the given number. On equating, fraction ** (2 ^ exponent) gives back the number.

Syntax: Math.frexp(number)



Parameter: The function takes a mandatory parameter number whose normalised fraction and exponent are to be returned.

Return Value: The function returns a two-element array.



Example 1:




# Ruby program for frexp() function 
  
# Assigning values
val1 = 123
val2 = 256
val3 = 23
val4 = 18
  
# Prints the frexp() value 
puts Math.frexp(val1)
puts
puts Math.frexp(val2)
puts
puts Math.frexp(val3)
puts
puts Math.frexp(val4) 

Output:

0.9609375
7

0.5
9

0.71875
5

0.5625
5

Example 2:




# Ruby program for frexp() function 
  
# Assigning values
val1 = 3213
val2 = 12
val3 = 16
val4 = 23
  
# Prints the frexp() value 
puts Math.frexp(val1)
puts
puts Math.frexp(val2)
puts
puts Math.frexp(val3)
puts
puts Math.frexp(val4) 

Output:

0.784423828125
12

0.75
4

0.5
5

0.71875
5

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


Article Tags :