# Workaround to Symbolics.build\_functions returning NaN

**URL:** <https://discourse.julialang.org/t/workaround-to-symbolics-build-functions-returning-nan/81925>\
**Category:** General Usage\
**Tags:** question, symbolics\
**Created:** [May 30, 2022, 5:51pm UTC](https://discourse.julialang.org/t/workaround-to-symbolics-build-functions-returning-nan/81925 "2022-05-30T17:51:55Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Aleksandr\_Mikheev](https://avatars.discourse-cdn.com/v4/letter/a/b4bc9f/32.png) [@Aleksandr\_Mikheev](https://discourse.julialang.org/u/Aleksandr_Mikheev)\
**Post date:** [May 30, 2022, 5:51pm UTC](https://discourse.julialang.org/t/workaround-to-symbolics-build-functions-returning-nan/81925/1 "2022-05-30T17:51:55Z")

</div>

I am currently using `build_function` from `Symbolics.jl` to generate Julia-type functions that I later work with (numerically integrate, etc.). Recently, I was not able to numerically integrate one of such-obtained functions and I soon realized, why. Fixing all but one variables the function in question has the following form (I write it the same way Julia returns it to me):

f(\varepsilon) = b\_1 + \dfrac{b\_2}{\left[1 + \dfrac{k\_1 \left( \varepsilon^{2} \right)^{0.375}}{ \exp\left(c\_1/\varepsilon^{2}\right) - 1} \right]^2 \left[1 + \dfrac{k\_2 \left( \varepsilon^{2} \right)^{0.375}}{\exp\left(c\_2/\varepsilon^{2}\right) - 1} \right]^2} \\ + b\_3 \varepsilon \dfrac{\left( 2 + \dfrac{k\_3 \left( \varepsilon^{2} \right)^{0.375}}{\exp\left(c\_3/\varepsilon^{2}\right) - 1}\right) \left( \dfrac{g\_1 \varepsilon}{\left[\exp\left(c\_3/\varepsilon^{2}\right) - 1\right] \left( \varepsilon^{2} \right)^{0.625}} + \dfrac{g\_2 \varepsilon \exp\left(c\_4/\varepsilon^{2}\right)}{\left[\exp\left(c\_4/\varepsilon^{2}\right) - 1\right]^{2} \left( \varepsilon^{2} \right)^{1.625}} \right)}{\left\lbrace \left[1 + \dfrac{k\_5 \left( \varepsilon^{2} \right)^{0.375}}{\exp\left(c\_5/\varepsilon^{2}\right) - 1} \right]^2\right\rbrace^{2} \left[1 + \dfrac{k\_6 \left( \varepsilon^{2} \right)^{0.375}}{\exp\left(c\_6/\varepsilon^{2}\right) - 1} \right]^2}

with a\_i, b\_i, and c\_i (\simeq 1) being (positive) constants, some of which might be equal to each other. In any case, it’s rather evident that \lim\_{\varepsilon\to0} f(\varepsilon) = b\_1 + b\_2, which in this case \simeq 0.03. This is nicely illustrated by the plot below:

![](https://global.discourse-cdn.com/julialang/original/3X/0/c/0c4f29b15dc9c4d18d775bd524f67f659bcc3148.png)

It also, however, shows that Julia struggles to evaluate f(\varepsilon) when \varepsilon gets too small: in the dashed region, f returns `NaN`. The problem seems to lie in the expression

\dfrac{g\_2 \varepsilon \exp\left(c\_4/\varepsilon^{2}\right)}{\left[\exp\left(c\_4/\varepsilon^{2}\right) - 1\right]^{2} \left( \varepsilon^{2} \right)^{1.625}}

since in the \varepsilon\to0 limit the latter has the `Inf/Inf` form (even though it’s clear that the denominator’s `Inf` is the “stronger” one).

Is there any workaround? The problem would probably be solved if I could factor out the leading-order exponential in each term. Perhaps, `Symbolics.jl` has some built-in function that deals with such limits, which I was missed in the documentation…

P.S. Perhaps, minimal (non-)working example could help:

```julia
using Symbolics

@variables x;
f(x) = x*exp(1/x^2)/(x^3.25*(exp(1/x^2) - 1)^2);
g = build_function(f(x),x,expression=Val{false});
g(0.03)

```

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [May 30, 2022, 6:20pm UTC](https://discourse.julialang.org/t/workaround-to-symbolics-build-functions-returning-nan/81925/2 "2022-05-30T18:20:30Z")

</div>

[https://herbie.uwplse.org/](https://herbie.uwplse.org/) is really good here (although it isn’t an automatic solution). That said, from a quick glance, `expm1` might be really helpful.
