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:
degrees radians conversion

Subject: degrees radians conversion

From: Andrew

Date: 11 Feb, 2009 06:38:02

Message: 1 of 7

Hi all,

I'm having a problem where the numeric error in converting between degrees and radians is causing problems.

What I would like to do, is define a set of real numbers X where the conversion from radians to degrees can be inverted exactly, so

(x * (180/pi)) * (pi/180) = x for all x in X

then whenever I get a value in radians I can map it to its closest value in X and problem solved. But I don't know how to do that.

I was thinking I could loop the rads->degs->rads process until it finds an exactly invertible value. I have no idea if this is guaranteed to converge, or if it will converge to a value anywhere near the original.

My other thought was that there might be a property where any x with less than c significant figures will be a member of X.

Thanks for any help,
Andrew

Subject: degrees radians conversion

From: Johannes Huth

Date: 11 Feb, 2009 07:29:03

Message: 2 of 7

"Andrew" <awbsmith@itee.uq.edu.au> wrote in message <gmtro9$oqm$1@fred.mathworks.com>...
> Hi all,
>
> I'm having a problem where the numeric error in converting between degrees and radians is causing problems.
>
> What I would like to do, is define a set of real numbers X where the conversion from radians to degrees can be inverted exactly, so
>
> (x * (180/pi)) * (pi/180) = x for all x in X
>
> then whenever I get a value in radians I can map it to its closest value in X and problem solved. But I don't know how to do that.
>
> I was thinking I could loop the rads->degs->rads process until it finds an exactly invertible value. I have no idea if this is guaranteed to converge, or if it will converge to a value anywhere near the original.
>
> My other thought was that there might be a property where any x with less than c significant figures will be a member of X.
>
> Thanks for any help,
> Andrew

Hi Andrew,

Would a workaround do for you?
Simply use a Nx2 lookup table LUT where N = |X|
Each row stores the x-value and the computed radian (after your formula).
Then if you get a radian number r, find the index to the value in LUT that is closest to r:

diff = LUT(:,2)-(ones(N,1).*r); % difference between all stored radians and the requested value r
idx = find(diff==min(diff)); % find the index of the closest radian

And then use the LUT entry LUT(idx,1) as your matching number

Regards,
Joh

Subject: degrees radians conversion

From: Jos

Date: 11 Feb, 2009 10:47:02

Message: 3 of 7

"Andrew" <awbsmith@itee.uq.edu.au> wrote in message <gmtro9$oqm$1@fred.mathworks.com>...
> Hi all,
>
> I'm having a problem where the numeric error in converting between degrees and radians is causing problems.

Can you give a relevant example? The maximum numeric error appears to be small (and regular)

a = (180/pi)
x=0:.01:180 ;
y = x / a ; % deg -> rad
x2 = y * a ; % rad -> deg
max(abs(x2-x))
% 1.4211e-014
plot(x,x2-x,'-') ;

Jos

Subject: degrees radians conversion

From: us

Date: 11 Feb, 2009 14:36:02

Message: 4 of 7

"Johannes Huth"
> Simply use a Nx2 lookup table LUT where N = |X|
> Each row stores the x-value and the computed radian (after your formula).
> Then if you get a radian number r, find the index to the value in LUT that is closest to r:
> diff = LUT(:,2)-(ones(N,1).*r); % difference between all stored radians and the requested value r
> idx = find(diff==min(diff)); % find the index of the closest radian
> And then use the LUT entry LUT(idx,1) as your matching number

a (slightly faster) solution could look like this

% the data
     lut=[
          1:10
          pi*(1:10)
     ].';
     val=[4.5,1.2]; % <= two vals to look up...
% the engine
     val=sort(val);
     ix=histc(val,lut(:,1));
     r=lut(ix~=0,:);
% the result
     disp([val.',r]);
%{
          1.2 1 3.1416
          4.5 4 12.566
%}

us

Subject: degrees radians conversion

From: Roger Stafford

Date: 11 Feb, 2009 20:37:01

Message: 5 of 7

"Andrew" <awbsmith@itee.uq.edu.au> wrote in message <gmtro9$oqm$1@fred.mathworks.com>...
> .....
> What I would like to do, is define a set of real numbers X where the conversion from radians to degrees can be inverted exactly, so
>
> (x * (180/pi)) * (pi/180) = x for all x in X
>
> then whenever I get a value in radians I can map it to its closest value in X and problem solved. But I don't know how to do that.
> ......

  If you look for equality in x==(x*(180/pi))*(pi/180) for randomly selected values of x, you will probably find that it holds exactly for about 85% of all such selections using matlab's double precision numbers. Therefore, there is no possibility of storing these numbers in a table. However, it does mean that for any given value of y, you wouldn't ordinarily have to look very far from y before encountering an x that does satisfy this equality. You could find it with this procedure or something similar:

 a = 180/pi; b = pi/180;
 k1 = 1+1/2^52;
 k2 = k1;
 x = y;
 while x~=(x*a)*b
  x = k2*y;
  k1 = 1/k1;
  k2 = k1/k2;
 end

This should almost always give you an x very quickly with the required property in the immediate vicinity of y. (However, reducing the number of "significant figures" in x will not be of any help.)

  However, I find it curious that you should want to do a thing like this. Round-off errors of this kind are a fact of life for anyone doing numerical calculations, whether in binary or in decimal, and whether using a computer or a hand-held calculator. I would think that the very small errors produced by doing the radians --> degrees --> radians conversions would be quite acceptable in any reasonable computing environment.

Roger Stafford

Subject: degrees radians conversion

From: James Tursa

Date: 11 Feb, 2009 23:17:02

Message: 6 of 7

"Andrew" <awbsmith@itee.uq.edu.au> wrote in message <gmtro9$oqm$1@fred.mathworks.com>...
>
> What I would like to do, is define a set of real numbers X where the conversion from radians to degrees can be inverted exactly, so
>
> (x * (180/pi)) * (pi/180) = x for all x in X
>

I am still curious. Why do you need to do this?

James Tursa

Subject: degrees radians conversion

From: Andrew

Date: 12 Feb, 2009 02:07:02

Message: 7 of 7

Thanks for your help (and to all those above)!

The reason I want to do this is that I perfrom an optimization seeded with a state vector with variables measured in radians. The objective function is horrible, in that there is a large number of points in the state space where the objective function's 1st derivative is unavailable. As such rounding errors can effect which "side" of the 1st order discontinuity the estimate is on. So when I return the converged state in degrees (for interpretability), but then re-evaluate the objective function (say to try and break convergence using a different optimizer), the "cost" doesn't match the convergence value. So my plan is to map any potential state update to another point where the inversion works perfectly.

In hindsight I should have written the program so the conversion only happens right before I plot something.


"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <gmvctd$efv$1@fred.mathworks.com>...
> "Andrew" <awbsmith@itee.uq.edu.au> wrote in message <gmtro9$oqm$1@fred.mathworks.com>...
> > .....
> > What I would like to do, is define a set of real numbers X where the conversion from radians to degrees can be inverted exactly, so
> >
> > (x * (180/pi)) * (pi/180) = x for all x in X
> >
> > then whenever I get a value in radians I can map it to its closest value in X and problem solved. But I don't know how to do that.
> > ......
>
> If you look for equality in x==(x*(180/pi))*(pi/180) for randomly selected values of x, you will probably find that it holds exactly for about 85% of all such selections using matlab's double precision numbers. Therefore, there is no possibility of storing these numbers in a table. However, it does mean that for any given value of y, you wouldn't ordinarily have to look very far from y before encountering an x that does satisfy this equality. You could find it with this procedure or something similar:
>
> a = 180/pi; b = pi/180;
> k1 = 1+1/2^52;
> k2 = k1;
> x = y;
> while x~=(x*a)*b
> x = k2*y;
> k1 = 1/k1;
> k2 = k1/k2;
> end
>
> This should almost always give you an x very quickly with the required property in the immediate vicinity of y. (However, reducing the number of "significant figures" in x will not be of any help.)
>
> However, I find it curious that you should want to do a thing like this. Round-off errors of this kind are a fact of life for anyone doing numerical calculations, whether in binary or in decimal, and whether using a computer or a hand-held calculator. I would think that the very small errors produced by doing the radians --> degrees --> radians conversions would be quite acceptable in any reasonable computing environment.
>
> Roger Stafford

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