0

In *Mathematica* 8 when I enter `1 - 0.99 - 0.01`

I get `8.67362*10^-18`

instead of zero. How do I fix this problem?

I am getting imaginary solutions because of this error in my program.

0

In *Mathematica* 8 when I enter `1 - 0.99 - 0.01`

I get `8.67362*10^-18`

instead of zero. How do I fix this problem?

I am getting imaginary solutions because of this error in my program.

1Have a look at

`Chop`

. – b.gates.you.know.what – 2013-01-22T09:35:59.0131

When you input a number like 0.1 Mathematica will carry out computation using a floating point approximation to the number, this is done in order to gain speed at the sacrifice of accuracy. If you desire exact numbers you can use rationals:

– jVincent – 2013-01-22T09:46:11.993`1-99/100-1/100 => 0`

. Or you can simply account for approximation errors as b.gate suggest3Suggested reading: "What Every Computer Scientist Should Know About Floating-Point Arithmetic" – Niki Estner – 2013-01-22T15:50:57.587

1This is a very commonly asked about thing not only in Mathematica, but in just about any computer language. In short, the reason is that these numbers are represented in binary (e.g. 1/2 is 0.1 in binary, 1/4 is 0.01), and not every number that's representable in decimal is representable in binary (similarly to how 1/3 = 0.3333... is not exactly representable in decimal with a finite number of digits). E.g. when you type 1/10 = 0.1 in decimal, this is

`0.1100110011001100...`

in binary. Because the machine works with a finite number of binary digits, precision suffers. – Szabolcs – 2013-01-22T17:36:49.9371Without having more context, it's difficult to determine which of many possible solutions would work. For instance, in many cases a simple change of the form

`N(1-99/100-1/100)`

would do the trick. Is this sort of solution acceptable or do you need some kind of automatic way to avoid floating point imprecision? – whuber – 2013-01-22T18:22:47.8731Voting NaRQ due to no response from the OP and no obvious way to answer the question as posed in any more detail than the suggestions already given above. – Oleksandr R. – 2013-01-27T01:57:19.417

@OleksandrR. Do you think that questions that are answered in the comments should be closed as NaRQ? The question is clear enough and IMO doesn't need additional detailing from the OP. I suggest we wrap this up by putting all the comments in a community wiki answer. What do you think? – Sjoerd C. de Vries – 2013-01-27T18:41:31.863

1

I think it's best closed as a dupe of http://mathematica.stackexchange.com/q/18393/8

– Verbeia – 2013-01-27T22:03:34.247@SjoerdC.deVries no, of course not. I voted NaRQ in this case because it is not at all clear what kind of a "fix" the OP wants and despite many comments giving suggestions there has (as pointed out by @whuber) been no clarification as to which of these, if any, would be the basis of a useful answer. If you want to explain the ins and outs of IEEE754 along with all the implications of machine precision numbers in

Mathematicathen you'll get my upvote for sure, but otherwise, I really don't anticipate anyone volunteering to give such an epic answer to this question! – Oleksandr R. – 2013-01-28T01:14:59.133@Verbeia ditto, more or less. I doubt if indiscriminate

`Chop`

ping is really all that helpful for this issue in general; users would be better off having a proper understanding of how numbers are represented and then they can decide for themselves whether`Chop`

is appropriate. But between the usual subtleties of floating point arithmetic (stemming from the fact that floats are actually rationals) andMathematica's own rather unique implementation of approximate reals, this is a massive undertaking. I'll drop a large bounty for a comprehensive answer but don't have time to write it myself. – Oleksandr R. – 2013-01-28T01:32:14.650`Rationalize[Unevaluated[1 - 0.99 - 0.01]]`

or`RootApproximant /@ Unevaluated[1 - 0.99 - 0.01]`

can be helpful. – Artes – 2013-04-27T10:31:19.870