Ruby | Math Module

In Ruby, Modules are defined as a collection of methods, classes, and constants together. Math module consists of the module methods for basic trigonometric and transcendental functions.

Module Constants

Name Description
E Define the value of base of natural logrithum e.
PI Define the value of π.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

# Ruby code to illustrate the 
# Math Module constants
puts Math::E
  
puts Math::PI

chevron_right


Output:



2.718281828459045
3.141592653589793

Module Methods

  1. acos : This method calculate the arc cosine of given value a. It return in the range[0..PI]. The return type of this method is float.
    Math.acos(a)

    Example:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    # Ruby code to illustrate the 
    # acos method
    puts Math.acos(0)
      
    # checking its range
    puts Math.acos(0) == Math::PI/2

    chevron_right

    
    

    Output:

    1.5707963267948966
    true
    
  2. acosh : This method calculate the inverse hyperbolic cosine of given value a. The return type of this method is float.
    Math.acosh(a)
  3. asin : This method calculate the arc sine of given value a. It return in the range[-PI/2..PI/2]. The return type of this method is float.
    Math.asin(a)
  4. asinh : This method calculate the inverse hyperbolic sine of given value a. The return type of this method is float.
    Math.asinh(a)

    Example:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    # Ruby code to illustrate the 
    # asinh method
    puts Math.asinh(2)

    chevron_right

    
    

    Output:

    1.4436354751788103
    
  5. atan : This method calculate the arc of tangent of given of given value a. It returns in the range[-PI..PI]. The return type of this method is the float.
    Math.atan(a)
  6. atanh : This method calculate the inverse hyperbolic tangent of given value a. The return type of this method is float.
    Math.atanh(a)

    Example:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    # Ruby code to illustrate the 
    # atanh method
    puts Math.atanh(0.5)

    chevron_right

    
    

    Output:

    0.5493061443340548
    
  7. atan2 : This method calculate the arc of tangent of given value a and b. It returns in the range[-PI..PI]. The return type of this method is float.
    Math.atan2(a, b)
  8. cos : This method calculate the cosine of given value a, expressed in radians and return in the range[-1.0..1.0]. The return type of this method is float.
    Math.cos(a)

    Example:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    # Ruby code to illustrate the 
    # cos method
    puts Math.cos(1)

    chevron_right

    
    

    Output:

    0.5403023058681398
    
  9. cosh : This method calculate the hyperbolic cosine of given value a and expressed in radians. The return type of this method is float.
    Math.cosh(a)
  10. erf : This method returns the error function of given value a. The return type of this method is float.
    Math.erf(a)
  11. erfc : This method returns the complementary error function of given value a. The return type of this method is float.
    Math.erfc(a)
  12. exp : This method returns the value of ea. The return type of this method is float.
    Math.exp(a)

    Example:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    # Ruby code to illustrate the 
    # exp method
    puts Math.exp(2)

    chevron_right

    
    

    Output:


    7.38905609893065
    
  13. frexp : This method returns a two-element array that consisting the normalized fraction and exponent of numeric.
    Math.frexp(numeric)
  14. hypot :This method returns a2+b2. Or in other words it returns the hypotenuse of right-angle triangle with sides a and b. The return type of this method is float.
    Math.hypot(a, b)

    Example:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    # Ruby code to illustrate the 
    # hypot method
    puts Math.hypot(4,5)

    chevron_right

    
    

    Output:

    6.4031242374328485
    
  15. Idexp : This method returns the value of float * 2 integer. The return type of this method is float.
    Math.Idexp(float, integer)
  16. log : This method returns the natural logarithm of numeric. The return type of this method is float.
    Math.log(numeric)
  17. log10 : This method return the base 10 of the logarithm of numeric. The return type of this method is float.
    Math.log10(numeric)
  18. sin : This method calculate the sine of numeric and expressed in radians. It return in the range[-1.0..1.0 ]. The return type of this method is float.
    Math.sin(numeric)

    Example:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    # Ruby code to illustrate the 
    # sin method
    puts Math.sin(0)

    chevron_right

    
    

    Output:

    0.0
    
  19. sinh : This method calculate the hyperbolic sine of numeric and expressed in radians. The return type of this method is float.
    Math.sinh(numeric)
  20. sqrt : This method return the non-negative square root of numeric and raise ArgError if numeric is less than zero. The return type of this method is float.
    Math.sqrt(numeric)
  21. tan : This method return the tangent of numeric and expressed in radians. The return type of this method is float.
    Math.tan(numeric)
  22. tanh : This method calculate the hyperbolic tangent of numeric and expressed in radians. The return type of this method is float.
    Math.tanh(numeric)

    Example:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    # Ruby code to illustrate the 
    # tanh method
    puts Math.tanh(1)

    chevron_right

    
    

    Output:

    0.7615941559557649
    

Reference: https://ruby-doc.org/core-2.2.0/Math.html



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.