# CAS Best Practices

**URL:** <https://discourse.julialang.org/t/cas-best-practices/58092>\
**Category:** Machine Learning\
**Tags:** symbolic\
**Created:** [March 27, 2021, 8:51pm UTC](https://discourse.julialang.org/t/cas-best-practices/58092 "2021-03-27T20:51:11Z")\
**Posts on this page:** 1\
**Showing post:** 75

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [April 5, 2021, 10:39am UTC](https://discourse.julialang.org/t/cas-best-practices/58092/75 "2021-04-05T10:39:07Z")

</div>

If you want it exact, don’t ask for the Float64 result?

```julia
julia> ex = Term(sqrt,[big(2)])
sqrt(2)

julia> eval(toexpr(ex))
1.414213562373095048801688724209698078569671875376948073176679737990732478462102

```

It just follows Julia’s semantics.

> [@stevengj](#):
>
> `0 * x` is not the same as `zero(x)` if `x` is not finite ( `NaN` or `±Inf` ). That’s why `x - x` can be simplified to `0*x` (for an appropriate `0` ) but not to `zero(x)` in general, and simplifying x−xx-x is the [context](https://discourse.julialang.org/t/cas-best-practices/58092/61) here.

I see. Yeah there’s a more interesting question to be answered here, which is what kind of number is the default symbol? The default is `@variables x::Real`, specifically not `::AbstractFloat`. Since these rules are all type-based, I think simplifying `x-x -> 0` on `::Real` is the semantics you’d expect, while `x-x -> 0 * x` should be what happens on `::AbstractFloat`. This keeps two algebras distinct: real numbers obey commutative and associative laws, they are a field, etc. while floating point numbers have a different set of rules.

The reason for keeping this difference is that the set of transformations that keep exact floating point semantics is already in LLVM, so I don’t think there’s too much to gain there. But it would be a good opt-in: “make simplifications only do what’s allowed on floating point numbers” instead of “make simplifications only do what’s allowed on real numbers”. The latter seems to be what people normally want on models, or at least out of a CAS, though there are times when exact floating point semantics need to be followed (in which case we have `@register` for someone to register a function and keep some calculation intact). What I want to do is add a call to [Herbie](https://herbie.uwplse.org/), so that in many cases we aren’t just applying rules that change floating point behavior but actually improve it, so even further down the path of “keep floating point separate”.

---

_[View the full topic](https://discourse.julialang.org/t/cas-best-practices/58092)._
