×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Are you an
Engineering professional?
Join Eng-Tips Forums!
• Talk With Other Members
• Be Notified Of Responses
• Keyword Search
Favorite Forums
• Automated Signatures
• Best Of All, It's Free!

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

#### Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

# 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.

### RE: Problems with double format

I'm sure there are more elegant ways to do what you want, but here is one thought:

» 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

(OP)
Thank you very much electricpete. Your solution works, but I prefer a more elegant one, because it is going to be a application to distribute. Thus, the operation that I do where I have that problem is more complex than 3.3-2.2-1.1, although it is the same problem, so if I use your solution the code would be very very more complex, something I am avoiding.

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

(OP)
In the other hand my question is: Should I trust in an application that gets this output??:

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

There are discussions like this at least once a week in the Matlab USENET newsgroup (comp.soft-sys.matlab).  There is a FAQ devoted to it:

http://matlabwiki.mathworks.com/MATLAB_FAQ#Why_is_0.3-0.2-0.1_not_equal_to_zero_.28or_similar.29.3F

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

(OP)
Thank you SomptingGuy,

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

Sorry to sound negative, but round() won't help you either.  You just can't trim those pesky unwanted digits - they come back.   You need to change the way you compare values, assuming that's the problem.

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

Alternatively implement BCD arithmetic in Matlab. I haven't done it, but can't really see it as being difficult, just slow.

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

Ack!! a four bit nibble.  I do have an 'O' level in computer studies.

- 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

Also one has to think about the precision of the numbers that you work with.  A 1E-16 error in treating numbers on the order of magnitude of 1 wouldn't be important unless your numbers have 16 significant figures to begin with.

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

### RE: Problems with double format

I can't think of a case where it has come up, but if you were calculating the exponent of something then

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

Like I said... "unless your numbers have 16 significant figures to begin with".  You are assuming this 2 has 16+ significant figures and if so the unless clause is applicable.

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

### RE: Problems with double format

I was wrong on two counts, #sf, and in fact the example is unhelpful, the difference between the two is tiny.

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

Fractions cannot be represented exactly in a binary floating point system, unless they are a power of 1/2.  While there are problems that thrive on such roundoff errors, most real world problems can get along quite fine.

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.

#### Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

#### Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Close Box

# Join Eng-Tips® Today!

Join your peers on the Internet's largest technical engineering professional community.
It's easy to join and it's free.

Here's Why Members Love Eng-Tips Forums:

• Talk To Other Members
• Notification Of Responses To Questions
• Favorite Forums One Click Access
• Keyword Search Of All Posts, And More...

Register now while it's still free!