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:
numbers output

Subject: numbers output

From: Anna Kaladze

Date: 7 Sep, 2010 14:09:04

Message: 1 of 9

Hi All,
I have a little code in MATLAB, whose success depends on MATLAB “correctly” reading certain numbers, that is the way I want them to be read. The issue can be described as follow. In my MATLAB window, I type
>> a=4123
a =
        4123
>> b=a*0.01
b =
  41.230000000000004
I want MATLAB to "get" b=41.23, not something else, and I can’t see why multiplying 4123 by 0.01 should mathematically give me 41.230000000000004 and not 41.23. Can you please help me to deal with this issue? I have 7.10.0 (R2010a) version. (I am not so much worried how the number is “displayed”, I need to make sure they are “stored/read” they way I need).
Thanks a lot.
Anna.

Subject: numbers output

From: Steven_Lord

Date: 7 Sep, 2010 14:12:54

Message: 2 of 9



"Anna Kaladze" <anna.kaladze@gmail.com> wrote in message
news:i65h20$fdo$1@fred.mathworks.com...
> Hi All,
> I have a little code in MATLAB, whose success depends on MATLAB
> “correctly” reading certain numbers, that is the way I want
> them to be read. The issue can be described as follow. In my MATLAB
> window, I type
>>> a=4123
> a =
> 4123
>>> b=a*0.01
> b =
> 41.230000000000004
> I want MATLAB to "get" b=41.23, not something else, and I can’t see
> why multiplying 4123 by 0.01 should mathematically give me
> 41.230000000000004 and not 41.23.

See question 6.1 in the newsgroup FAQ. 0.01 is NOT exactly one
one-hundredth; it is the closest representable floating-point number to one
one-hundredth.

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: numbers output

From: Anna Kaladze

Date: 7 Sep, 2010 15:20:21

Message: 3 of 9

"Steven_Lord" <slord@mathworks.com> wrote in message <i65h96$109$1@fred.mathworks.com>...
>
>
> "Anna Kaladze" <anna.kaladze@gmail.com> wrote in message
> news:i65h20$fdo$1@fred.mathworks.com...
> > Hi All,
> > I have a little code in MATLAB, whose success depends on MATLAB
> > “correctly” reading certain numbers, that is the way I want
> > them to be read. The issue can be described as follow. In my MATLAB
> > window, I type
> >>> a=4123
> > a =
> > 4123
> >>> b=a*0.01
> > b =
> > 41.230000000000004
> > I want MATLAB to "get" b=41.23, not something else, and I can’t see
> > why multiplying 4123 by 0.01 should mathematically give me
> > 41.230000000000004 and not 41.23.
>
> See question 6.1 in the newsgroup FAQ. 0.01 is NOT exactly one
> one-hundredth; it is the closest representable floating-point number to one
> one-hundredth.
>
> --
> Steve Lord
> slord@mathworks.com
> comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
> To contact Technical Support use the Contact Us link on
> http://www.mathworks.com
Thank you for your answer Steve.
Does this mean that I never will be able to "get" 41.23 in the above example? Is there a simple way to deal with my problem?

Subject: numbers output

From: Matt J

Date: 7 Sep, 2010 15:30:31

Message: 4 of 9

"Anna Kaladze" <anna.kaladze@gmail.com> wrote in message <i65l7l$17$1@fred.mathworks.com>...

> Does this mean that I never will be able to "get" 41.23 in the above example? =======

No. Due to floating point approximation, the number 41.23 isn't even representable as the following shows

>> format long, 41.23

ans =

  41.229999999999997


>Is there a simple way to deal with my problem?
==================

Possibly. First, describe why it's a problem, i.e., why you can't accept a floating point approximation of this number.

Subject: numbers output

From: Steven_Lord

Date: 7 Sep, 2010 16:53:47

Message: 5 of 9



"Matt J " <mattjacREMOVE@THISieee.spam> wrote in message
news:i65lqn$be7$1@fred.mathworks.com...
> "Anna Kaladze" <anna.kaladze@gmail.com> wrote in message
> <i65l7l$17$1@fred.mathworks.com>...
>
>> Does this mean that I never will be able to "get" 41.23 in the above
>> example? =======
>
> No. Due to floating point approximation, the number 41.23 isn't even
> representable as the following shows
>
>>> format long, 41.23
>
> ans =
>
> 41.229999999999997

Matt's correct; 41.23 is not exactly representable. The way 41.23 is
represented is smaller than forty-one and twenty-three one-hundredths; the
next largest representable number after that is larger than forty-one and
twenty-three one-hundredths.

format long
x = 41.23
next = x+eps(x)

and if you print out those numbers' IEEE representations in hex, you can see
that only the last bit of the mantissa changed, so there are no
representable numbers in-between.

format hex
x
next

Don't forget to change the display format back to your favorite after doing
this, or you may confuse yourself the next time you display something.

>>Is there a simple way to deal with my problem?
> ==================
>
> Possibly. First, describe why it's a problem, i.e., why you can't accept a
> floating point approximation of this number.

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: numbers output

From: James Tursa

Date: 7 Sep, 2010 17:25:23

Message: 6 of 9

"Anna Kaladze" <anna.kaladze@gmail.com> wrote in message <i65h20$fdo$1@fred.mathworks.com>...
> Hi All,
> I have a little code in MATLAB, whose success depends on MATLAB “correctly” reading certain numbers, that is the way I want them to be read. The issue can be described as follow. In my MATLAB window, I type
> >> a=4123
> a =
> 4123
> >> b=a*0.01
> b =
> 41.230000000000004
> I want MATLAB to "get" b=41.23, not something else, and I can’t see why multiplying 4123 by 0.01 should mathematically give me 41.230000000000004 and not 41.23. Can you please help me to deal with this issue? I have 7.10.0 (R2010a) version. (I am not so much worried how the number is “displayed”, I need to make sure they are “stored/read” they way I need).

You can find a useful tool num2strexact to help visualize this on the FEX:

>> num2strexact(4123)
ans =
4.123e3
>> num2strexact(0.01)
ans =
1.000000000000000020816681711721685132943093776702880859375e-2
>> num2strexact(4123*0.01)
ans =
4.12300000000000039790393202565610408782958984375e1
>> num2strexact(4123*0.01-eps(4123*0.01))
ans =
4.122999999999999687361196265555918216705322265625e1

As you can see, there are two floating point numbers close to 41.23, but the exact value of 41.23 cannot be represented in IEEE double precision floating point format.

num2strexact can be found here:

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


James Tursa

Subject: numbers output

From: Grzegorz Knor

Date: 21 Oct, 2010 09:58:04

Message: 7 of 9

Hi,
I can't find how display numbers without power e. For example:

0.003923^2

I would like to print:
0.000015389929
instead of:
1.5389929e-05

Thanks in advance

Grzegorz

Subject: numbers output

From: Grzegorz Knor

Date: 21 Oct, 2010 10:08:04

Message: 8 of 9

"Grzegorz Knor" <gknor@o2.pl> wrote in message <i9p2rb$ilk$1@fred.mathworks.com>...
> Hi,
> I can't find how display numbers without power e. For example:
>
> 0.003923^2
>
> I would like to print:
> 0.000015389929
> instead of:
> 1.5389929e-05
>
> Thanks in advance
>
> Grzegorz

Ok, I found that i can do this in this way:
fprintf('%.10f',n)

But is there any possibilty, to set it as a default Matlab format?

Subject: numbers output

From: Loren Shure

Date: 21 Oct, 2010 13:58:44

Message: 9 of 9


"Grzegorz Knor" <gknor@o2.pl> wrote in message
news:i9p3e3$pd8$1@fred.mathworks.com...
> "Grzegorz Knor" <gknor@o2.pl> wrote in message
> <i9p2rb$ilk$1@fred.mathworks.com>...
>> Hi, I can't find how display numbers without power e. For example:
>>
>> 0.003923^2
>>
>> I would like to print:
>> 0.000015389929
>> instead of:
>> 1.5389929e-05
>>
>> Thanks in advance
>>
>> Grzegorz
>
> Ok, I found that i can do this in this way:
> fprintf('%.10f',n)
>
> But is there any possibilty, to set it as a default Matlab format?
>

help format ?? e.g., format short or format long - set it in your
preferences if you want to always use it.

--
Loren
http://blogs.mathworks.com/loren/
http://matlabwiki.mathworks.com/MATLAB_FAQ

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