Re: MATLAB can't sum!
- From: Christopher Hulbert <cchgroupmail@xxxxxxxxx>
- Date: Tue, 26 Jul 2005 13:50:35 -0400
Pablo C. wrote:
> Hi everyone,
>
> The numerical precision of MATLAB can be a serious Problem. Take
> for example the following lines
>
>
> >x = 0.5 + single(220*( cos( 278.32*degreetorad ) )); %
> >x2 = single((220)*(cos( 278.32*degreetorad ) )); %
>
> degreetorad is = pi/180. For the people who don't know it, single
> reduces the amount of memory used by MATLAB for representing the
> corresponding float number, that is, it should have less precision.
>
> Do you think that x2 + 0.5 == x? NO! Matlab returns a zero for this
> comparison. Why?
>
> Well, the problems begins when you introduce sin(pi). It should be
> zero, but the result is something^-16 or so, because MATLAB's precision.
>
> Single does not help any much, as it reduces only the precision of
> the mantise, so the problems remains... How to achieve that MATLAB can
> really sum when using some trascendental functions??
>
> I have tested all the combinations with/without single, nothing. In
> the help you can only see that double and single are the functions which
> operate over float numbers, no more.
>
> Can anyone help me? It is really nasty when you need a fixed limit to
> compare with and you see that the numbers "fly" around this value when
> all they should posses the same value.
>
> Thank you in beforehand and greetings from Germany,
>
> Pablo
Mine works just fine. Take this as proof that you should RARELY if EVER compare floating point
numbers with ==, ~=. Use a tolerance most likely related to the precision of your class variable.
Matlab's representation of sin(pi) isn't it's precision. Even with any finite precision sin(pi)
will not be exactly 0. pi is irrational, so only in it's symbolic form can you say the sin(pi) is
0. pi in matlab is an approximation of the irrational pi.
>> clear all
>> degreetorad = pi/180.0;
>> x = 0.5 + single(220*( cos( 278.32*degreetorad ) ));
>> x2 = single((220)*(cos( 278.32*degreetorad ) ));
>> fprintf('%0.20f\n%0.20f\n%0.20f\n',x,x2,x2+0.5)
32.33435058593750000000
31.83435249328613300000
32.33435058593750000000
>> x2+0.5 == x
ans =
1
.
- References:
- MATLAB can't sum!
- From: Pablo C.
- MATLAB can't sum!
- Prev by Date: Re: MATLAB can't sum!
- Next by Date: covariance matrix testing
- Previous by thread: Re: MATLAB can't sum!
- Next by thread: Re: MATLAB can't sum!
- Index(es):
Relevant Pages
|