Skip to content

Commit a32f991

Browse files
Add math::exp and math::e (#8982)
Co-authored-by: Aljaž Mur Eržen <aljaz@erzen.si>
1 parent 0d84d1f commit a32f991

File tree

5 files changed

+174
-2
lines changed

5 files changed

+174
-2
lines changed

docs/reference/stdlib/math.rst

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,20 @@ Math
6464
----------
6565

6666

67+
.. eql:function:: math::exp(x: int64) -> float64
68+
math::exp(x: float64) -> float64
69+
math::exp(x: decimal) -> decimal
70+
71+
Returns the natural logarithm of a given value.
72+
73+
.. code-block:: edgeql-repl
74+
75+
db> select math::exp(1);
76+
{2.718281829}
77+
78+
----------
79+
80+
6781
.. eql:function:: math::ln(x: int64) -> float64
6882
math::ln(x: float64) -> float64
6983
math::ln(x: decimal) -> decimal
@@ -72,7 +86,7 @@ Math
7286

7387
.. code-block:: edgeql-repl
7488
75-
db> select 2.718281829 ^ math::ln(100);
89+
db> select math::exp(math::ln(100));
7690
{100.00000009164575}
7791
7892
@@ -198,6 +212,18 @@ Math
198212
db> select math::pi();
199213
{3.141592653589793}
200214
215+
-----------
216+
217+
218+
.. eql:function:: math::e() -> float64
219+
220+
Returns the value of e (euler's number).
221+
222+
.. code-block:: edgeql-repl
223+
224+
db> select math::e();
225+
{2.718281828459045}
226+
201227
202228
-----------
203229

docs/reference/stdlib/math_funcops_table.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
* - :eql:func:`math::floor`
1313
- :eql:func-desc:`math::floor`
1414

15+
* - :eql:func:`math::exp`
16+
- :eql:func-desc:`math::exp`
17+
1518
* - :eql:func:`math::ln`
1619
- :eql:func-desc:`math::ln`
1720

@@ -39,6 +42,9 @@
3942
* - :eql:func:`math::pi`
4043
- :eql:func-desc:`math::pi`
4144

45+
* - :eql:func:`math::e`
46+
- :eql:func-desc:`math::e`
47+
4248
* - :eql:func:`math::acos`
4349
- :eql:func-desc:`math::acos`
4450

edb/common/assert_data_shape.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,12 @@ def _assert_generic_shape(path, data, shape):
340340
elif isinstance(shape, type):
341341
return _assert_type_shape(path, data, shape)
342342
elif isinstance(shape, float):
343-
if not math.isclose(data, shape,
343+
if math.isnan(shape):
344+
if not math.isnan(shape):
345+
fail(
346+
f'NaN mismatch {_format_path(path)}'
347+
)
348+
elif not math.isclose(data, shape,
344349
rel_tol=rel_tol, abs_tol=abs_tol):
345350
fail(
346351
f'{message}: not isclose({data}, {shape}) '

edb/lib/math.edgeql

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,35 @@ std::math::floor(x: std::decimal) -> std::decimal
100100
USING SQL 'SELECT floor("x");';
101101
};
102102

103+
CREATE FUNCTION
104+
std::math::exp(x: std::int64) -> std::float64
105+
{
106+
CREATE ANNOTATION std::description :=
107+
'Return the exponential of the input value.';
108+
SET volatility := 'Immutable';
109+
USING SQL FUNCTION 'exp';
110+
};
111+
112+
113+
CREATE FUNCTION
114+
std::math::exp(x: std::float64) -> std::float64
115+
{
116+
CREATE ANNOTATION std::description :=
117+
'Return the exponential of the input value.';
118+
SET volatility := 'Immutable';
119+
USING SQL FUNCTION 'exp';
120+
};
121+
122+
123+
CREATE FUNCTION
124+
std::math::exp(x: std::decimal) -> std::decimal
125+
{
126+
CREATE ANNOTATION std::description :=
127+
'Return the exponential of the input value.';
128+
SET volatility := 'Immutable';
129+
USING SQL FUNCTION 'exp';
130+
};
131+
103132

104133
CREATE FUNCTION
105134
std::math::ln(x: std::int64) -> std::float64
@@ -413,6 +442,14 @@ std::math::pi() -> std::float64
413442
USING SQL FUNCTION 'pi';
414443
};
415444

445+
CREATE FUNCTION
446+
std::math::e() -> std::float64
447+
{
448+
CREATE ANNOTATION std::description :=
449+
'Return the constant value of e.';
450+
SET volatility := 'Immutable';
451+
USING SQL 'SELECT exp(1);'
452+
};
416453

417454
CREATE FUNCTION
418455
std::math::acos(x: std::float64) -> std::float64

tests/test_edgeql_functions.py

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5817,6 +5817,104 @@ async def test_edgeql_functions_math_floor_02(self):
58175817
{True},
58185818
)
58195819

5820+
async def test_edgeql_functions_math_exp_01(self):
5821+
# test math::exp basics
5822+
5823+
await self.assert_query_result(
5824+
r'''SELECT math::exp(0);''',
5825+
{1.0},
5826+
)
5827+
5828+
await self.assert_query_result(
5829+
r'''SELECT math::exp(1);''',
5830+
{math.e},
5831+
)
5832+
5833+
await self.assert_query_result(
5834+
r'''SELECT math::exp(2.0);''',
5835+
{math.exp(2)},
5836+
)
5837+
5838+
await self.assert_query_result(
5839+
r'''SELECT math::exp(<decimal>1.0);''',
5840+
{math.e},
5841+
)
5842+
5843+
await self.assert_query_result(
5844+
r'''SELECT math::exp({1, 2, 3});''',
5845+
{math.e, math.exp(2), math.exp(3)},
5846+
)
5847+
5848+
async def test_edgeql_functions_math_exp_02(self):
5849+
# test return type of math::exp
5850+
5851+
await self.assert_query_result(
5852+
r'''SELECT math::exp(<int64>1) IS float64;''',
5853+
{True},
5854+
)
5855+
5856+
await self.assert_query_result(
5857+
r'''SELECT math::exp(<float64>1.0) IS float64;''',
5858+
{True},
5859+
)
5860+
5861+
await self.assert_query_result(
5862+
r'''SELECT math::exp(<decimal>1.0) IS decimal;''',
5863+
{True},
5864+
)
5865+
5866+
async def test_edgeql_functions_math_exp_03(self):
5867+
# test math::exp edge cases
5868+
5869+
await self.assert_query_result(
5870+
r'''SELECT math::exp(-1);''',
5871+
{1 / math.e},
5872+
abs_tol=1e-5,
5873+
)
5874+
5875+
await self.assert_query_result(
5876+
r'''SELECT math::exp(-2.0);''',
5877+
{1 / (math.e**2)},
5878+
abs_tol=1e-5,
5879+
)
5880+
5881+
async with self.assertRaisesRegexTx(
5882+
edgedb.NumericOutOfRangeError, 'value out of range: overflow'
5883+
):
5884+
await self.con.query(r'''SELECT math::exp(1000);''')
5885+
5886+
await self.assert_query_result(
5887+
r'''SELECT math::exp(<decimal>1000);''',
5888+
{
5889+
int(
5890+
'1970071114017046993888879352243323125316937985323845789952'
5891+
'8029913850638507824411934749780765630268899309638179875202'
5892+
'2693598298173054461289923262783660152825232320535169584566'
5893+
'7561922715676027880714224668263140068551685086534979416603'
5894+
'1604536781793809290529972858013286994585647028653437590045'
5895+
'6564355589156220422320260518826112288638358372248724725214'
5896+
'5061504188819374941008712642322484363157605603774399306239'
5897+
'59705844189509050047074217568'
5898+
)
5899+
}
5900+
)
5901+
5902+
await self.assert_query_result(
5903+
r'''SELECT math::exp(<decimal>100);''',
5904+
{math.e ** 100}
5905+
)
5906+
5907+
await self.assert_query_result(
5908+
r'''SELECT math::exp(<float64>'inf');''',
5909+
{'Infinity'},
5910+
{math.inf},
5911+
)
5912+
await self.assert_query_result(
5913+
r'''SELECT math::exp(<float64>'nan');''',
5914+
{'NaN'},
5915+
{math.nan}
5916+
)
5917+
58205918
async def test_edgeql_functions_math_log_01(self):
58215919
await self.assert_query_result(
58225920
r'''SELECT math::ln({1, 10, 32});''',

0 commit comments

Comments
 (0)