Ruby | Math log() function
The log() function in Ruby returns the logarithm value of X. The second parameter is the base given by the user to which the logarithm value is returned. In case its not given, then the default base is e.
Syntax: Math.log(X, base)
Parameter: The function takes one mandatory parameter X whose logarithm value is to be returned and a non-mandatory parameter base, to whose base is the logarithm.
Return Value: The function returns the logarithm value of X.
Example 1:
# Ruby program for log() function # Assigning values val1 = 213 val2 = 256 base2 = 2 val3 = 27 base3 = 3 val4 = 100 base4 = 10 # Prints the value returned by log() puts Math.log(val1) puts Math.log(val2, base2) puts Math.log(val3, base3) puts Math.log(val4, base4) |
Output:
5.3612921657094255 8.0 3.0 2.0
Example 2:
# Ruby program for log() function # Assigning values val1 = 10 val2 = 256 base2 = 4 val3 = 27 base3 = 10 val4 = 105 base4 = 7 # Prints the value returned by log() puts Math.log(val1) puts Math.log(val2, base2) puts Math.log(val3, base3) puts Math.log(val4, base4) |
Output:
2.302585092994046 4.0 1.4313637641589871 2.3916625094004957
Reference: https://devdocs.io/ruby~2.5/math#method-c-log
Recommended Posts:
- Ruby | Math cos() function
- Ruby | Math sin() function
- Ruby | Math tan() function
- Ruby | Math exp() function
- Ruby | Math erf() function
- Ruby | Math atan2() function
- Ruby | Math sqrt() function
- Ruby | Math cbrt() function
- Ruby | Math erfc() function
- Ruby | Math atan() function
- Ruby | Math atanh() function
- Ruby | Math log2() function
- Ruby | Math log10() function
- Ruby | Math lgamma() function
- Ruby | Math sinh() function
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.



