# Taylor series from symbolics uses wrong rational approximations

**URL:** <https://discourse.julialang.org/t/taylor-series-from-symbolics-uses-wrong-rational-approximations/133971>\
**Category:** General Usage\
**Tags:** symbolics\
**Created:** [November 18, 2025, 4:33pm UTC](https://discourse.julialang.org/t/taylor-series-from-symbolics-uses-wrong-rational-approximations/133971 "2025-11-18T16:33:15Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![bernardofpc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bernardofpc/32/7783_2.png) [@bernardofpc](https://discourse.julialang.org/u/bernardofpc)\
**Post date:** [November 18, 2025, 4:33pm UTC](https://discourse.julialang.org/t/taylor-series-from-symbolics-uses-wrong-rational-approximations/133971/1 "2025-11-18T16:33:15Z")

</div>

I am calculating a Taylor series and I get strange rational numbers where they should not appear. A minimal example is the following:

```julia
pkg> st
Status [REDACTED]
  [163ba53b] DiffResults v1.1.0
  [f6369f11] ForwardDiff v1.3.0
  [09f84164] HypothesisTests v0.11.6
  [23fbe1c1] Latexify v0.16.10
  [2edaba10] Nemo v0.52.3
  [0c5d862f] Symbolics v7.0.2

julia> using Symbolics

julia> @variables x
1-element Vector{Num}:
 x

julia> taylor(sqrt(2+x), x, 0, 0:2)
77227930//54608393 + 46611179//131836323*x - (14651750//331531257)*(x^2)

```

I don’t understand the use of a rational approximation for sqrt(2) - which makes the series wrong.

---

<div class="post-metadata">

**Author:** ![ForceBru](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/forcebru/32/21389_2.png) [@ForceBru](https://discourse.julialang.org/u/ForceBru)\
**Post date:** [November 18, 2025, 4:40pm UTC](https://discourse.julialang.org/t/taylor-series-from-symbolics-uses-wrong-rational-approximations/133971/2 "2025-11-18T16:40:28Z")

</div>

As is easily verified:

- `77227930//54608393` is approximately `sqrt(2)`
- `46611179//131836323` is approximately `0.5 / sqrt(2)`
- And so on.

These are the correct terms (derivatives evaluated at `x=0`), so this isn’t exactly wrong. However, I wouldn’t say it’s correct either. One would expect a symbolic representation like Mathematica’s `Sqrt[2]` or Julia’s `pi`.

---

<div class="post-metadata">

**Author:** ![mikmoore](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mikmoore/32/31109_2.png) [@mikmoore](https://discourse.julialang.org/u/mikmoore)\
**Post date:** [November 18, 2025, 4:50pm UTC](https://discourse.julialang.org/t/taylor-series-from-symbolics-uses-wrong-rational-approximations/133971/3 "2025-11-18T16:50:40Z")

</div>

And when the above poster says “approximately,” they mean “almost exactly.” The rational numbers above resolve to the float values with an error of \<1 ULP (i.e., nearly to the limits of machine precision, which would be \<0.5 ULP).

It looks like `taylor`’s default behavior is to return rational numbers. This isn’t a ridiculous choice because many functions have rational series (and at the end of the day, every float is rational also! so to be pedantic: there’s no escaping it without fancier symbolics like \sqrt{2}).

But I also see that [`taylor` has a `rationalize` keyword](https://docs.sciml.ai/Symbolics/stable/manual/taylor/#Symbolics.taylor). It looks like passing `rationalize=false` should give you floats like you want.

---

<div class="post-metadata">

**Author:** ![bernardofpc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bernardofpc/32/7783_2.png) [@bernardofpc](https://discourse.julialang.org/u/bernardofpc)\
**Post date:** [November 18, 2025, 6:08pm UTC](https://discourse.julialang.org/t/taylor-series-from-symbolics-uses-wrong-rational-approximations/133971/4 "2025-11-18T18:08:29Z")

</div>

> [@mikmoore](#):
>
> And when the above poster says “approximately,” they mean “almost exactly.” The rational numbers above resolve to the float values with an error of \<1 ULP (i.e., nearly to the limits of machine precision, which would be \<0.5 ULP).

I think that’s a bad idea. It wrongly conveys the impression that such numbers are exact as rationals, whereas exposing floating-point numbers makes more clear to the user that approximations might have been used. For example:

```julia
julia> taylor(sqrt(2+x)/sqrt(2), x, 0, 0:2, rationalize=false)
1 + 0.24999999999999994x - 0.03124999999999999(x^2)

julia> taylor(sqrt(2+x)/sqrt(2), x, 0, 0:2)
1 + 750599937895083//3002399751580333*x - (70368744177664//2251799813685249)*(x^2)

```

Notice that the coefficients are approximately 1/4 and 1/32 (which would have been the correct answers). I don’t think transforming into rationals make any sense here…

> [@mikmoore](#):
>
> But I also see that [`taylor` has a `rationalize` keyword](https://docs.sciml.ai/Symbolics/stable/manual/taylor/#Symbolics.taylor). It looks like passing `rationalize=false` should give you floats like you want.

Ah, that’s useful, thank you.

Maybe it would make sense to change the default behavior? Or - at least - give some rationale into having rationals as default…

---

<div class="post-metadata">

**Author:** ![PeterSimon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petersimon/32/25193_2.png) [@PeterSimon](https://discourse.julialang.org/u/PeterSimon)\
**Post date:** [November 18, 2025, 7:37pm UTC](https://discourse.julialang.org/t/taylor-series-from-symbolics-uses-wrong-rational-approximations/133971/5 "2025-11-18T19:37:51Z")

</div>

> [@bernardofpc](#):
>
> rationale into having rationals as default

In other words, you think the default behavior should be irrational?

( 😉 sorry, couldn’t resist)

---

<div class="post-metadata">

**Author:** ![bernardofpc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bernardofpc/32/7783_2.png) [@bernardofpc](https://discourse.julialang.org/u/bernardofpc)\
**Post date:** [November 18, 2025, 9:46pm UTC](https://discourse.julialang.org/t/taylor-series-from-symbolics-uses-wrong-rational-approximations/133971/6 "2025-11-18T21:46:14Z")

</div>

> [@PeterSimon](#):
>
> In other words, you think the default behavior should be irrational?

🙂

More to the point, I guess that, **if floating-point calculations were used** at some point during the Taylor series evaluation, then it would seem reasonable to keep those floating-point numbers since they convey the usual expectation that “numerical errors might be present”. If no FP happens, then rationals indeed look more sensible. Ideally, it would be nice for such design choices to be documented and (even more ideally) explained to users.

For comparison, `sympy` yields

```python
python> sympy.series(sympy.sqrt(2+x), x, 0, 3)
sqrt(2) + sqrt(2)*x/4 - sqrt(2)*x**2/32 + O(x**3)

```

actually exposing the `sqrt(2)` factors.

---

<div class="post-metadata">

**Author:** ![ForceBru](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/forcebru/32/21389_2.png) [@ForceBru](https://discourse.julialang.org/u/ForceBru)\
**Post date:** [November 19, 2025, 8:11am UTC](https://discourse.julialang.org/t/taylor-series-from-symbolics-uses-wrong-rational-approximations/133971/7 "2025-11-19T08:11:14Z")

</div>

I feel like this is a bug: Symbolics did the computations in floating-point (even though you evaluate your function at an exact integer value!) and then found the lowest-denominator fractions that were closest to the floating-point values. In Python this can be done as follows:

```python
>>> .1+.2
0.30000000000000004
>>> (.1+.2).as_integer_ratio()
(1351079888211149, 4503599627370496)
>>> 1351079888211149/4503599627370496
0.30000000000000004

```

In my experience, such massive fractions usually arise from this process, meaning that they’re _not_ exact.
