@@ -93,12 +93,19 @@ def _(expr: TypedExpr) -> sge.Expression:
9393def _ (expr : TypedExpr ) -> sge .Expression :
9494 return sge .Case (
9595 ifs = [
96+ # |x| < 1: The standard formula
97+ sge .If (
98+ this = sge .func ("ABS" , expr .expr ) < sge .convert (1 ),
99+ true = sge .func ("ATANH" , expr .expr ),
100+ ),
101+ # |x| > 1: Returns NaN
96102 sge .If (
97103 this = sge .func ("ABS" , expr .expr ) > sge .convert (1 ),
98104 true = constants ._NAN ,
99- )
105+ ),
100106 ],
101- default = sge .func ("ATANH" , expr .expr ),
107+ # |x| = 1: Returns Infinity or -Infinity
108+ default = sge .Mul (this = constants ._INF , expression = expr .expr ),
102109 )
103110
104111
@@ -145,15 +152,11 @@ def _(expr: TypedExpr) -> sge.Expression:
145152
146153@register_unary_op (ops .expm1_op )
147154def _ (expr : TypedExpr ) -> sge .Expression :
148- return sge .Case (
149- ifs = [
150- sge .If (
151- this = expr .expr > constants ._FLOAT64_EXP_BOUND ,
152- true = constants ._INF ,
153- )
154- ],
155- default = sge .func ("EXP" , expr .expr ),
156- ) - sge .convert (1 )
155+ return sge .If (
156+ this = expr .expr > constants ._FLOAT64_EXP_BOUND ,
157+ true = constants ._INF ,
158+ false = sge .func ("EXP" , expr .expr ) - sge .convert (1 ),
159+ )
157160
158161
159162@register_unary_op (ops .floor_op )
@@ -166,11 +169,22 @@ def _(expr: TypedExpr) -> sge.Expression:
166169 return sge .Case (
167170 ifs = [
168171 sge .If (
169- this = expr .expr <= sge .convert (0 ),
172+ this = sge .Is (this = expr .expr , expression = sge .Null ()),
173+ true = sge .null (),
174+ ),
175+ # |x| > 0: The standard formula
176+ sge .If (
177+ this = expr .expr > sge .convert (0 ),
178+ true = sge .Ln (this = expr .expr ),
179+ ),
180+ # |x| < 0: Returns NaN
181+ sge .If (
182+ this = expr .expr < sge .convert (0 ),
170183 true = constants ._NAN ,
171- )
184+ ),
172185 ],
173- default = sge .Ln (this = expr .expr ),
186+ # |x| == 0: Returns -Infinity
187+ default = constants ._NEG_INF ,
174188 )
175189
176190
@@ -179,11 +193,22 @@ def _(expr: TypedExpr) -> sge.Expression:
179193 return sge .Case (
180194 ifs = [
181195 sge .If (
182- this = expr .expr <= sge .convert (0 ),
196+ this = sge .Is (this = expr .expr , expression = sge .Null ()),
197+ true = sge .null (),
198+ ),
199+ # |x| > 0: The standard formula
200+ sge .If (
201+ this = expr .expr > sge .convert (0 ),
202+ true = sge .Log (this = sge .convert (10 ), expression = expr .expr ),
203+ ),
204+ # |x| < 0: Returns NaN
205+ sge .If (
206+ this = expr .expr < sge .convert (0 ),
183207 true = constants ._NAN ,
184- )
208+ ),
185209 ],
186- default = sge .Log (this = expr .expr , expression = sge .convert (10 )),
210+ # |x| == 0: Returns -Infinity
211+ default = constants ._NEG_INF ,
187212 )
188213
189214
@@ -192,11 +217,22 @@ def _(expr: TypedExpr) -> sge.Expression:
192217 return sge .Case (
193218 ifs = [
194219 sge .If (
195- this = expr .expr <= sge .convert (- 1 ),
220+ this = sge .Is (this = expr .expr , expression = sge .Null ()),
221+ true = sge .null (),
222+ ),
223+ # Domain: |x| > -1 (The standard formula)
224+ sge .If (
225+ this = expr .expr > sge .convert (- 1 ),
226+ true = sge .Ln (this = sge .convert (1 ) + expr .expr ),
227+ ),
228+ # Out of Domain: |x| < -1 (Returns NaN)
229+ sge .If (
230+ this = expr .expr < sge .convert (- 1 ),
196231 true = constants ._NAN ,
197- )
232+ ),
198233 ],
199- default = sge .Ln (this = sge .convert (1 ) + expr .expr ),
234+ # Boundary: |x| == -1 (Returns -Infinity)
235+ default = constants ._NEG_INF ,
200236 )
201237
202238
@@ -608,7 +644,7 @@ def isfinite(arg: TypedExpr) -> sge.Expression:
608644 return sge .Not (
609645 this = sge .Or (
610646 this = sge .IsInf (this = arg .expr ),
611- right = sge .IsNan (this = arg .expr ),
647+ expression = sge .IsNan (this = arg .expr ),
612648 ),
613649 )
614650
0 commit comments