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:
operation precision in Matlab

Subject: operation precision in Matlab

From: Dimitar Dimitrov

Date: 30 Aug, 2007 08:38:45

Message: 1 of 5

Hello,

Is there a way to specify the precision with which an
operation in Matlab is performed.

I am asking because I have an .m file which is basically a
simple implementation of a primal active set method for
solving a QP.
And when I have a degenerate case (for the QP) I get
different results depending on weather I am working on my
Windows or Linux installation (using the perfectly the same
function).

So I presume that I have to "check some flag" or something ...

Thank you
Dimitar

Subject: operation precision in Matlab

From: us

Date: 31 Aug, 2007 23:26:15

Message: 2 of 5

Walter Roberson:
<SNIP poking his/her...

> 1) Perhaps the floating point "rounding mode" is different
> for the two machines. I poked around a little but could not
> see any way to control the rounding mode for floating point...

here is some stuff

% note: we cannot run this in a test loop - for obvious
% reasons...
     system_dependent('setround',-inf);
     a=1.009;
     disp(sprintf('%.30e',a));
     system_dependent('setround',0);
     a=1.009;
     disp(sprintf('%.30e',a));
     system_dependent('setround',inf);
     a=1.009;
     disp(sprintf('%.30e',a));
     system_dependent('setround',.5);
     a=1.009;
     disp(sprintf('%.30e',a));

% will yield this in a typical wintel env:
% ic2/2*2.4g/2g/winvista/r2007a
% 1.008999999999999900000000000000e+000
% 1.008999999999999900000000000000e+000
% 1.009000000000000100000000000000e+000 % <-!!!!!
% 1.008999999999999900000000000000e+000

% -and- the precision
     format hex;
     system_dependent('setprecision',24);
     eig(magic(3))
% 402e000000000000
% 4013988e40000000
% c013988e20000000
     system_dependent('setprecision',53);
     eig(magic(3))
% 402e000000000000
% 4013988e14092137 % <- !
% c013988e1409212e % <- !
     system_dependent('setprecision',64);
     eig(magic(3))
% 402e000000000000
% 4013988e14092132 % <- !
% c013988e1409212e

us

Subject: operation precision in Matlab

From: us

Date: 31 Aug, 2007 23:35:27

Message: 3 of 5

Walter Roberson:
<SNIP poking his/her...

> 1) Perhaps the floating point "rounding mode" is different
> for the two machines. I poked around a little but could
not
> see any way to control the rounding mode for floating
point...

here is some stuff

% note: we cannot run this in a test loop - for obvious
% reasons...
     system_dependent('setround',-inf);
     a=1.009;
     disp(sprintf('%.30e',a));
     system_dependent('setround',0);
     a=1.009;
     disp(sprintf('%.30e',a));
     system_dependent('setround',inf);
     a=1.009;
     disp(sprintf('%.30e',a));
     system_dependent('setround',.5);
     a=1.009;
     disp(sprintf('%.30e',a));

% will yield this in a typical wintel env:
% ic2/2*2.4g/2g/winvista/r2007a
% 1.008999999999999900000000000000e+000
% 1.008999999999999900000000000000e+000
% 1.009000000000000100000000000000e+000 % <-!!!!!
% 1.008999999999999900000000000000e+000

% -and- the precision
     format hex;
     system_dependent('setprecision',24);
     eig(magic(3))
% 402e000000000000
% 4013988e40000000
% c013988e20000000
     system_dependent('setprecision',53);
     eig(magic(3))
% 402e000000000000
% 4013988e14092137 % <- !
% c013988e1409212e % <- !
     system_dependent('setprecision',64);
     eig(magic(3))
% 402e000000000000
% 4013988e14092132 % <- !
% c013988e1409212e

us

Subject: operation precision in Matlab

From: Dimitar Dimitrov

Date: 3 Sep, 2007 08:24:45

Message: 4 of 5

"us" thank you for the reply!

I think that this is Matlab related problem and not platform
related one. Simply because a c++ implementation of my
program on a linix or windows machine (say with gcc and the
same compilation flags) will perform in the same way (this
is if fast-math flag is not used of course).

Frankly speaking I dont have time to get deep into the
problem, and since there is no simple workaround I will just
hope for the best.

Thanks for the interesting test program though,
Dimitar




"us " <us@neurol.unizh.ch> wrote in message
<fba8jv$4ra$1@fred.mathworks.com>...
> Walter Roberson:
> <SNIP poking his/her...
>
> > 1) Perhaps the floating point "rounding mode" is different
> > for the two machines. I poked around a little but could
> not
> > see any way to control the rounding mode for floating
> point...
>
> here is some stuff
>
> % note: we cannot run this in a test loop - for obvious
> % reasons...
> system_dependent('setround',-inf);
> a=1.009;
> disp(sprintf('%.30e',a));
> system_dependent('setround',0);
> a=1.009;
> disp(sprintf('%.30e',a));
> system_dependent('setround',inf);
> a=1.009;
> disp(sprintf('%.30e',a));
> system_dependent('setround',.5);
> a=1.009;
> disp(sprintf('%.30e',a));
>
> % will yield this in a typical wintel env:
> % ic2/2*2.4g/2g/winvista/r2007a
> % 1.008999999999999900000000000000e+000
> % 1.008999999999999900000000000000e+000
> % 1.009000000000000100000000000000e+000 % <-!!!!!
> % 1.008999999999999900000000000000e+000
>
> % -and- the precision
> format hex;
> system_dependent('setprecision',24);
> eig(magic(3))
> % 402e000000000000
> % 4013988e40000000
> % c013988e20000000
> system_dependent('setprecision',53);
> eig(magic(3))
> % 402e000000000000
> % 4013988e14092137 % <- !
> % c013988e1409212e % <- !
> system_dependent('setprecision',64);
> eig(magic(3))
> % 402e000000000000
> % 4013988e14092132 % <- !
> % c013988e1409212e
>
> us

Subject: operation precision in Matlab

From: carlos lopez

Date: 5 Sep, 2007 14:50:46

Message: 5 of 5

"Dimitar Dimitrov" <mail_mitko@mathworks.com> wrote in
message <fb5vml$aaq$1@fred.mathworks.com>...
> Hello,
>
> Is there a way to specify the precision with which an
> operation in Matlab is performed.
Check "multiple precision toolbox" (by Ben Barrowes) in the FEX.
Regards
Carlos

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