# How to cancel floating point factors?

I am currently having problems with some floating points.

I have a function, which gives as an intermediate result (for example)

````-(10000. - 10000. a) E^(-16.4157 kp^2 x^0.277)/((-1. + a) a x^0.252525 xj^4.47047)
````

It gets passed through into another function, which will fill in the value `a = 1`. Then Mathematica reacts with a `1/Sqrt[0]` error. However, as you can see, the factor `(10000. - 10000. a)` should cancel with the factor `(-1.+ a)` in the denominator to give 10000, the result is thus well-defined.

Do you have any idea how to let Mathematica cancel these factors out? I have tried `Simplify` and `FullSimplify` with `Assumptions -> a!=1` but it doesn't work. I cannot change much to the function itself, as it is an intermediate result (and it is just an example for this post; other intermediate results also occur, sometimes with the same problem, sometimes they work fine).

-

3
Floating point numbers are different than rational numbers. Since floating point numbers don't really follow the rules of algebra, you'll find that Simplify and the like will not do much at all wit them. - Run the calculations with increased precision using SetPrecision. (still essentially the same problem) I think there are two options: - Use the function Rationalize to turn the floating point numbers into rational numbers and then use FullSimplify. (I'm not a big fan of this because it ignores the fact that the numbers of a limited precision) – Searke Mar 1 '12 at 19:47
@Searke "Floats don't really follow the rules of algebra"?! – David Mar 1 '12 at 19:56

4
@David That is right. Addition/multiplication is not even associative. – Szabolcs Mar 1 '12 at 19:57

1
@David for example, `(1 + $MaxMachineNumber) == $MaxMachineNumber` evaluates to `True` – acl Mar 1 '12 at 20:02
@David try `1.*10^16 + 1. - 1.*10^16` which returns different answers depending on where `1.` is, however `-10.^16 + (10.^16 + 1.)` and `-10.^16 + (1. + 10.^16)` return the same incorrect answer. This indicates that addition is not associative, but commutative. Also, given `g[a_, b_, c_] := a^2 - 2 a b + b^2 - c^2`, `g[10.^10, 10.^10, 10.] == 0` implying that mma is doing speculative processing as the first three terms simplify to $(a - b)^2$ and should cancel. – rcollyer Mar 1 '12 at 20:43
show 3 more comments

## 3 Answers

The problem is that symbolic computation doesn't mix well with floating point. If you use `Rationalize` first to convert to an exact representation, `Simplify` (or the faster `Cancel`) will be able to do it's job.

-

Use `Limit`, Mathematica's implementation of $\lim$:

$\displaystyle\lim_{x\to a}f(x) =$ `Limit[f[x], x -> a]`

Applied to your equation, this results in

````Limit[
    -(((10000. - 10000. a) E^(-16.4157 kp^2 x^0.277)/((-1. + a) a x^0.252525 xj^4.47047))),
    a -> 1
]
````
````(10000. E^(-16.4157 kp^2 x^0.277))/(x^0.252525 xj^4.47047)
````

The error you are talking about is not caused by floating point arithmetic, it's how Mathematica (or any other language for that matter) handles computations in general. If you substitute $a=1$ then you've got a division by zero, and once this occurs you're out of the domain of, well, mathematics; therefore, you can't cancel the two factors anymore. (Actually, your function is not defined at $a=1$, it has a removable singularity there.)

-
The function is well defined at `a = 1`. The singularity is artificial, and has more to do with the order of evaluation than a being a property of the function, unlike in $\sin(x)/x$. Since mma evaluates the terms in isolation from one another, i.e. $(a - 1)^{-1}$ is evaluated separately from $1/a$, etc., this is likely to occur. – rcollyer Mar 1 '12 at 20:08

1
$\frac xx$ stands for the product of x and its multiplicative inverse, which does not exist for $x=0$. It's not well-defined or even defined at that point, although the limit exists of course. – David Mar 1 '12 at 20:10
– rcollyer Mar 1 '12 at 20:34
– David Mar 1 '12 at 20:41

2
This, of course, reminds me why I hated my analysis course. – rcollyer Mar 1 '12 at 20:44

Well...

[Warning: This act is performed by trained professionals. Do not try it at home.]

````In[24]:= expr = -(10000. - 10000. a) E^(-16.4157 kp^2 x^0.277)/
  ((-1. + a) a x^0.252525 xj^4.47047);

In[25]:= Cancel[expr]
Out[25]//InputForm= 10000./(a*E^(16.4157*kp^2*x^0.277)*x^0.252525*xj^4.47047)
````

Okay, I cheated. I'm using the development version of Mathematica.

I will add some emphasis to remarks others have made. Doing computer algebra with approximate numbers is almost never a safe bet. Even to the extent that it might be supported, things can go wrong. And do. On a remarkably frequent basis.

-

+1, for cheek, alone. :) – rcollyer Mar 1 '12 at 23:03
lang-mma
{"extraction_info": {"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 10, "mathjax_display_tex": 0, "mathjax_asciimath": 0, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0}, "config": {"markdown_headings": true, "markdown_code": true, "boilerplate_config": {"ratio_threshold": 0.18, "absolute_threshold": 10, "end_threshold": 15, "enable": true}, "remove_buttons": true, "remove_image_figures": true, "remove_link_clusters": true, "table_config": {"min_rows": 2, "min_cols": 3, "format": "github"}, "remove_chinese": true, "remove_edit_buttons": true, "extract_latex": true}, "english_confidence": 0.9167249798774719, "perplexity_flag": "middle"}