## Problems with double format

## Problems with double format

(OP)

Hello everybody, I hope somebody can help me.

If I write in the console the following then I get:

>> 3.3-2.2-1.1

ans =

-4.4409e-016

Instead of 0. Situations like that are making some important accuracy errors in my applications. How could I get a 0 using the double format please??

Thank yoy very much, and congratulations for the forum.

If I write in the console the following then I get:

>> 3.3-2.2-1.1

ans =

-4.4409e-016

Instead of 0. Situations like that are making some important accuracy errors in my applications. How could I get a 0 using the double format please??

Thank yoy very much, and congratulations for the forum.

## RE: Problems with double format

» 3.3-2.2-1.1

ans =

-4.4409e-016

» multiplier = 1e9

multiplier =

1.0000e+009

» (multiplier*3.3-multiplier*2.2-multiplier*1.1)/multiplier

ans =

0

=====================================

Eng-tips forums: The best place on the web for engineering discussions.

## RE: Problems with double format

I would like some solution as easy as:

double (3.3)-double (2.2)-double (1.1)=0

But this does not work of course.

Thank you very much.

## RE: Problems with double format

floor (3.3-2.2-1.1)

ans =

-1

I think it is a basic operation, and it makes it wrong.

## RE: Problems with double format

http://ma

Simply put, binary fractions (as used by IEEE 754 floating point representation) can't hold multiples of 1/10 (1/100, 1/1000, etc) in an exact way, so there is always a rounding error when working with these numbers. You just can't write code that relies on exact representation unless it's symbolic.

As an aside, most calculators use BCD (Binary coded decimal) which wastes memory by storing each decimal digit in a 4 byte nibble.

- Steve

## RE: Problems with double format

It seems there is no a basic solution to this problem, so I will try to write my code in a different way, where it could be possible to store the values in different format to double, or using the round () function.

Thank you very much.

## RE: Problems with double format

if (a==b)

should be replaced by something like

if(abs(a-b)<tol)

where tol is some small number, possibly based on eps if you want to be rigorous.

- Steve

## RE: Problems with double format

Cheers

Greg Locock

SIG:Please see FAQ731-376: Eng-Tips.com Forum Policies for tips on how to make the best use of Eng-Tips.

## RE: Problems with double format

- Steve

## RE: Problems with double format

» vpa(3.3)-vpa(2.2)-vpa(1.1)

ans =

0

=====================================

Eng-tips forums: The best place on the web for engineering discussions.

## RE: Problems with double format

=====================================

Eng-tips forums: The best place on the web for engineering discussions.

## RE: Problems with double format

x^2

and x^2.0000000000000000000000000000001

have very different properties. That is a quick example, perhaps there are others.

Cheers

Greg Locock

SIG:Please see FAQ731-376: Eng-Tips.com Forum Policies for tips on how to make the best use of Eng-Tips.

## RE: Problems with double format

Eng-tips forums: The best place on the web for engineering discussions.

## RE: Problems with double format

Cheers

Greg Locock

SIG:Please see FAQ731-376: Eng-Tips.com Forum Policies for tips on how to make the best use of Eng-Tips.

## RE: Problems with double format

In your case, if this is a really big deal, then you need to code the problems directly in fractional representations, or use more significant figures, but that just pushes the problem further out in the sig-fig realm. There are various mathmatics applications, like Mathcad, or Mathematica, that can take various exact representations, and produce exact answers.

TTFN

FAQ731-376: Eng-Tips.com Forum Policies