Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Bug when doing simple subraction/multiplication

Subject: Bug when doing simple subraction/multiplication

From: lisett

Date: 24 Nov, 2010 00:59:05

Message: 1 of 7

I am using version R2010a of matlab,
in my command window simply for practicing

>> b = 6.245

b = 6.245000000000000

>> b = b- 6

b = 0.245000000000000

>> b = b *10

b = 2.450000000000001

>> b = b *10

b = 24.500000000000011

This is not an isolated problem to the numbers given in this example.

Where are these extra values coming from and more importantly how can I do this type of shifting without receiving these errors?

Subject: Bug when doing simple subraction/multiplication

From: Stan Bischof

Date: 24 Nov, 2010 01:32:34

Message: 2 of 7

lisett <lisvelar@gmail.com> wrote:
> I am using version R2010a of matlab,
> in my command window simply for practicing
>
>>> b = 6.245
>
> b = 6.245000000000000
>
>>> b = b- 6
>
> b = 0.245000000000000
>
>>> b = b *10
>
> b = 2.450000000000001
>
>>> b = b *10
>
> b = 24.500000000000011
>
> This is not an isolated problem to the numbers given in this example.
>
> Where are these extra values coming from and more importantly
> how can I do this type of shifting
> without receiving these errors?

This is ( or should be ) an FAQ.

All programs running on digital computers have finite-precision math.
That's just the way it is. Most math routines, including Matlab, follow
indistry standards- in this case IEEE 64-bit floating point standards.

One only gets exactly 64 bits ( 8 bytes) to represent the various numbers
and that's just the way it is. This leads to about 15 significant figures.
You are seeing this finite precision.

If you want more precision you can in some programs specify more
bits but you'll always have finite precision on a digital computer.

If you want infinite precision you need to use an analog computer.

regards
Stan

Subject: Bug when doing simple subraction/multiplication

From: Roger Stafford

Date: 24 Nov, 2010 04:06:03

Message: 3 of 7

Stan Bischof <stan@worldbadminton.com> wrote in message <4cec6b32$0$1592$742ec2ed@news.sonic.net>...
> lisett <lisvelar@gmail.com> wrote:
> >>> b = 6.245
> > b = 6.245000000000000
> >>> b = b- 6
> > b = 0.245000000000000
> >>> b = b *10
> > b = 2.450000000000001
> >>> b = b *10
> > b = 24.500000000000011
> > This is not an isolated problem to the numbers given in this example.
> > Where are these extra values coming from and more importantly
> > how can I do this type of shifting
> > without receiving these errors?
> ........
> If you want infinite precision you need to use an analog computer.
> .......
- - - - - - - - -
  The basic reason there are errors in the decimal arithmetic you show, Lisett, is that the digital computers we all use have a base of two for their storage and computations. That is, they use binary numbers. If they had used base ten numbers as with many hand calculators, the computations you show would have been done without error but there would be errors when fractions such as 1/3 or 1/7 occurred. Whatever base numbers are used in a computer, it will always make errors when dealing with fractions whose denominators are prime with respect to that base. If you designed a computer to use separate integer numerator and denominator in an effort to represent all rational numbers, these would quickly climb up to enormous values in even modestly complicated computations. Then when you encounter irrational numbers such as the square root of 2 or pi what would you do then? How would you
represent them in a computer without error?

  As for analog computers, Stan, all the ones I have ever had experience with are very much less accurate than even very cheap digital hand calculators. There is a good reason why the computer industry chose to go the digital route all those decades ago.

Roger Stafford

Subject: Bug when doing simple subraction/multiplication

From: James Tursa

Date: 24 Nov, 2010 04:33:04

Message: 4 of 7

"lisett " <lisvelar@gmail.com> wrote in message <icho0p$bjb$1@fred.mathworks.com>...
> I am using version R2010a of matlab,
> in my command window simply for practicing
>
> >> b = 6.245
>
> b = 6.245000000000000
>
> >> b = b- 6
>
> b = 0.245000000000000
>
> >> b = b *10
>
> b = 2.450000000000001
>
> >> b = b *10
>
> b = 24.500000000000011
>
> This is not an isolated problem to the numbers given in this example.
>
> Where are these extra values coming from and more importantly how can I do this type of shifting without receiving these errors?

As a learning aid you can use the num2strexact utility:

>> b = 6.245
b =
    6.2450
>> num2strexact(b)
ans =
6.24500000000000010658141036401502788066864013671875
>> b = b- 6
b =
    0.2450
>> num2strexact(b)
ans =
0.24500000000000010658141036401502788066864013671875
>> b = b *10
b =
    2.4500
>> num2strexact(b)
ans =
2.4500000000000010658141036401502788066864013671875
>> b = b *10
b =
   24.5000
>> num2strexact(b)
ans =
2.4500000000000010658141036401502788066864013671875e1

num2strexact can be found here:

http://www.mathworks.com/matlabcentral/fileexchange/22239-num2strexact-exact-version-of-num2str

James Tursa

Subject: Bug when doing simple subraction/multiplication

From: Walter Roberson

Date: 24 Nov, 2010 06:18:43

Message: 5 of 7

On 23/11/10 10:06 PM, Roger Stafford wrote:

> The basic reason there are errors in the decimal arithmetic you show,
> Lisett, is that the digital computers we all use have a base of two for
> their storage and computations. That is, they use binary numbers. If
> they had used base ten numbers as with many hand calculators, the
> computations you show would have been done without error but there would
> be errors when fractions such as 1/3 or 1/7 occurred.

Roger, it might amuse you to know that IBM has maintained decimal
arithmetic capabilities since the 1960's, primarily for financial
applications. Their current offerings that support decimal arithmetic
are under their Zeos operating system, but I do not recall at the moment
if the current model with the decimal CPU is the Z90 or Z900 (when I
last looked at the documentation, it implied the Z90 is still supported
but has been superceded by the Z900, but I could find very little
information about the Z900.)

IEEE is either in the process, or already has, approved a decimal
arithmetic extension to IEEE 754; apparently there is some interest from
some other manufacturers as well.

 >> If you want infinite precision you need to use an analog computer.

Stan, how do you propose to implement that infinite precision in an
analog computer? What kind of physical or electrical measurement is
robust down past the scale of brownian motion and yet is not quantized?
If you measure at the quantum level, how will your infinite precision
proceed once you get into Planck Scale measurements, down around 1E-74,
where it is suspected that space itself is quantized? I do not recall at
the moment whether it was last month's Scientific American, or last
month's American Scientist, but one of the two had an article that had
as an implication that measurements below 1E-70 are effectively
impossible: at those scales, the mass-equivalent of energy twists space
enough (gravitational "frame dragging") that you cannot measure more
accurately.

Subject: Bug when doing simple subraction/multiplication

From: Nasser M. Abbasi

Date: 24 Nov, 2010 14:11:29

Message: 6 of 7

On 11/23/2010 5:32 PM, Stan Bischof wrote:

>
> If you want infinite precision you need to use an analog computer.
>

do not need to go that advanced. any CAS system can do that (limited by
amount of RAM you have).

In[14]:= N[Pi,500000]
Out[14]= 3.1415926535897932384626433832795028841....etc...


--Nasser

Subject: Bug when doing simple subraction/multiplication

From: Walter Roberson

Date: 24 Nov, 2010 18:26:57

Message: 7 of 7

On 24/11/10 8:11 AM, Nasser M. Abbasi wrote:
> On 11/23/2010 5:32 PM, Stan Bischof wrote:

>> If you want infinite precision you need to use an analog computer.

> do not need to go that advanced. any CAS system can do that (limited by
> amount of RAM you have).

If it is limited by the amount of RAM you have, then unless you have an
infinite amount of RAM, you cannot get infinite precision, and Stan's
comments about finite precision being a limitation of digital computers
would stand.

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us