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:
computation precision and display

Subject: computation precision and display

From: Jose

Date: 4 Dec, 2012 01:01:10

Message: 1 of 4

Hi,

It just happens in my linux 64 bits PC, running Matlab 2010a (on format long g) that when I calculate

>> A=(0.714844838298060 + 0.699283102297360i)*conj(0.714844838298060 + 0.699283102297360i)

A =

         1.000000000000000

Now when I calculate


>> B=(0.757342604591773 + 0.653017748051421i)*conj(0.757342604591773 + 0.653017748051421i)

B =

                         1

And the difference is:

>>A-B

ans =

     -1.11022302462516e-16

This is important because when I try to plot A and B as elements of a matrix with a colormap using imagesc they get very different colors.

Then my questions are:

How to interpret a number such as A=1.000000000000000?

Is it possible to display its nonzero decimals after the 15th zero?

Another important question is that Matlab seems to make this kind of rounding off randomly so many other complex numbers when operated as above produce results sometimes displayed as an exact one or a one follwed by many zeroes.

So, how can this be controlled or predicted?

Thanks.

Subject: computation precision and display

From: Nasser M. Abbasi

Date: 4 Dec, 2012 02:15:06

Message: 2 of 4

On 12/3/2012 7:01 PM, Jose wrote:
> Hi,
>
> It just happens in my linux 64 bits PC, running Matlab 2010a (on format long g) that when I calculate
>
>>> A=(0.714844838298060 + 0.699283102297360i)*conj(0.714844838298060 + 0.699283102297360i)
>
> A =
>
> 1.000000000000000
>
> Now when I calculate
>
>
>>> B=(0.757342604591773 + 0.653017748051421i)*conj(0.757342604591773 + 0.653017748051421i)
>
> B =
>
> 1
>
> And the difference is:
>
>>> A-B
>
> ans =
>
> -1.11022302462516e-16
>
> This is important because when I try to plot A and B as elements of a
>matrix with a colormap using imagesc they get very different colors.

that is strange, since (A-B) is less than eps

---------------------------------
EDU>> (abs(A-B))<eps('double')

ans =

      1
---------------------------------

So, I would have expected same 'color' to show for these.

>
> Then my questions are:
>
> How to interpret a number such as A=1.000000000000000?
>
> Is it possible to display its nonzero decimals after the 15th zero?
>

may be (file exchange command)

-------------------------------
EDU>> num2strexact(A)

ans =

0.99999999999999966693309261245303787291049957275390625
-----------------------------------

Subject: computation precision and display

From: Roger Stafford

Date: 4 Dec, 2012 04:17:09

Message: 3 of 4

"Jose" wrote in message <k9ji0m$rcv$1@newscl01ah.mathworks.com>...
> Another important question is that Matlab seems to make this kind of rounding off randomly so many other complex numbers when operated as above produce results sometimes displayed as an exact one or a one follwed by many zeroes.
- - - - - - - - - -
  I would like to correct your assertion that "Matlab seems to make this kind of rounding off randomly." It may appear random on casual observation, but in fact in computers that adhere to the IEEE 754 Standard for Floating Point Arithmetic, the rules for "rounding to nearest" are very precise when it involves the four operations of addition, subtraction, multiplication, and division. In each case the precise result (to infinite precision) must be rounded to the very nearest floating point value that is available in that precision format - that is, double precision or single precision. There is nothing random about it.

  In the examples you give, the precise answer would not be an exact 1, because the sums of the squares of your defined real and imaginary parts are not precisely 1. These parts cannot be expressed to exact precision, but only to that allowed by the computer's format. If you receive an answer of exactly 1, it is only because that happened to be the closest to the precise answer. An answer 1.000000000000000 is simply the display to 15 places of a number that is presumably different from an exact 1 in its least bit. However, strictly speaking, both answers are incorrect because the computer is incapable of expressing the exact answer.

  This is the nature of rounding errors in computers and it is necessary for all users to take such behavior into consideration and make appropriate allowances.

Roger Stafford

Subject: computation precision and display

From: Steven_Lord

Date: 4 Dec, 2012 17:32:58

Message: 4 of 4



"Nasser M. Abbasi" <nma@12000.org> wrote in message
news:k9jmbo$ila$1@speranza.aioe.org...
> On 12/3/2012 7:01 PM, Jose wrote:
>> Hi,
>>
>> It just happens in my linux 64 bits PC, running Matlab 2010a (on format
>> long g) that when I calculate
>>
>>>> A=(0.714844838298060 + 0.699283102297360i)*conj(0.714844838298060 +
>>>> 0.699283102297360i)
>>
>> A =
>>
>> 1.000000000000000
>>
>> Now when I calculate
>>
>>
>>>> B=(0.757342604591773 + 0.653017748051421i)*conj(0.757342604591773 +
>>>> 0.653017748051421i)
>>
>> B =
>>
>> 1
>>
>> And the difference is:
>>
>>>> A-B
>>
>> ans =
>>
>> -1.11022302462516e-16
>>
>> This is important because when I try to plot A and B as elements of a
>>matrix with a colormap using imagesc they get very different colors.
>
> that is strange, since (A-B) is less than eps

Why is that strange?

Remember that eps() [== eps(1)] is the smallest number such that 1+eps is
greater than 1. It is perfectly fine to have numbers smaller than eps (for
example, REALMIN is many orders of magnitude smaller) and in particular the
difference between 1 and the next SMALLER number is eps/2.

format hex
a = 1
b = 1-(eps/2)
d = eps(b)
(a == (b+d))

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

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