Re: MATLAB can't sum!



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
.



Relevant Pages

  • Re: MATLAB cant sum!
    ... Pablo C. wrote: ... > The numerical precision of MATLAB can be a serious Problem. ... > corresponding float number, that is, it should have less precision. ... You should NEVER compare ...
    (comp.soft-sys.matlab)
  • Re: MATLAB cant sum!
    ... > Pablo C. wrote: ... >> The numerical precision of MATLAB can be a serious Problem. ... > abs> tol ...
    (comp.soft-sys.matlab)
  • Re: Precision and Accuracy of Elementary Functions in MATLAB
    ... exploring MATLAB as the programming language seems more accessible. ... Not so with with elementary functions. ... These operate in double or single precision, ... If you pass a double array into the Symbolic Math Toolbox, ...
    (comp.soft-sys.matlab)
  • Re: Int 32 processing speed: Is it a bug
    ... It is confirmed that matlab is not doing any integer arithmetic in ... I am not planning to use any matrix calculation in my program. ... Elapsed time is 0.017342 seconds. ... %Double precision ...
    (comp.soft-sys.matlab)
  • Re: A concern about the Matlab implementation of the Mersenne Twister RNG.
    ... On Mar 8, 10:48 am, Peter Perkins ... MATLAB's "basic" data class is double precision, ... That isn't exactly what MATLAB ... which comes with source code. ...
    (comp.soft-sys.matlab)