Skip to content

Commit

Permalink
Add standard log function
Browse files Browse the repository at this point in the history
  • Loading branch information
Praveen2112 authored and martint committed Jan 25, 2019
1 parent d04152e commit f6044e6
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
4 changes: 4 additions & 0 deletions presto-docs/src/main/sphinx/functions/math.rst
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ Mathematical Functions

Returns the natural logarithm of ``x``.

.. function:: log(b, x) -> double

Returns the base ``b`` logarithm of ``x``.

.. function:: log2(x) -> double

Returns the base 2 logarithm of ``x``.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,14 @@ public static double ln(@SqlType(StandardTypes.DOUBLE) double num)
return Math.log(num);
}

@Description("logarithm to given base")
@ScalarFunction
@SqlType(StandardTypes.DOUBLE)
public static double log(@SqlType(StandardTypes.DOUBLE) double base, @SqlType(StandardTypes.DOUBLE) double number)
{
return Math.log(number) / Math.log(base);
}

@Description("logarithm to base 2")
@ScalarFunction
@SqlType(StandardTypes.DOUBLE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,20 @@ public void testLog10()
assertFunction("log10(NULL)", DOUBLE, null);
}

@Test
public void testLog()
{
for (double doubleValue : DOUBLE_VALUES) {
for (double base : DOUBLE_VALUES) {
assertFunction("log(" + base + ", " + doubleValue + ")", DOUBLE, Math.log(doubleValue) / Math.log(base));
assertFunction("log(REAL '" + (float) base + "', REAL'" + (float) doubleValue + "')", DOUBLE, Math.log((float) doubleValue) / Math.log((float) base));
}
}
assertFunction("log(NULL, NULL)", DOUBLE, null);
assertFunction("log(5.0E0, NULL)", DOUBLE, null);
assertFunction("log(NULL, 5.0E0)", DOUBLE, null);
}

@Test
public void testMod()
{
Expand Down

0 comments on commit f6044e6

Please sign in to comment.