09-01-2018, 11:46 PM (This post was last modified: 09-02-2018 02:28 AM by Magnus512.)
Post: #1
 Magnus512 Junior Member Posts: 23 Joined: Sep 2016
So I'm writing a code to change between coordinate systems, this is the code
-------------------------------------
EXPORT SPHERE,RECTAN,CYLIN;
//Rectangular to Spherical
//(x,y,z)-->(ρ,θ,φ)
EXPORT RECT2SPHE(CORD)
BEGIN
//Separacion de coordenadas en x,y y z.
LOCAL x:=CORD(1),y:=CORD(2),z:=CORD(3);
LOCAL ρ,θ,φ,result;
//Operaciones para convertir los valores
ρ:=SQRT(x^2+y^2+z^2);
θ:=atan2(y/x);
φ:=ACOS(z/ρ);
//Guardar el resultado en la variable global SPHERE
SPHERE:=[ρ,θ,φ];
RETURN("ρ="+ρ+" θ="+θ+" φ="+φ);
END;

//Spherical to Rectangular
//(ρ,θ,φ)-->(x,y,z)
EXPORT SPHE2RECT(CORD)
BEGIN
//Separacion de cordenadas en r,θ y φ.
LOCAL ρ:=CORD(1),θ:=CORD(2),φ:=CORD(3);
LOCAL x,y,z,result;
x:=ρ*SIN(φ)*COS(θ);
y:=ρ*SIN(φ)*SIN(θ);
z:=ρ*COS(φ);
//Guardar el resultado en la variable global RECTAN
RECTAN:=[x,y,z];
RETURN("x="+x+" y="+y+" z="+z);
END;

//Rectangular to Cylindrical
//(x,y,z)-->(r,θ,z)
EXPORT RECT2CYL(CORD)
BEGIN
//Separacion de coordenadas en x,y y z.
LOCAL x:=CORD(1),y:=CORD(2),z:=CORD(3);
LOCAL r,θ,result;
r:=SQRT(x^2+y^2);
θ:=atan2(y,x);
z:=z;
//Guardar el resultado en la variable global CYLIN
CYLIN:=[r,θ,z];
RETURN("r="+r+" θ="+θ+" z="+z);
END;

//Cylindrical to Rectangular
//(r,θ,z)-->(x,y,z)
EXPORT CYL2RECT(CORD)
BEGIN
//Separacion de coordenadas en r,θ y z.
LOCAL r:=CORD(1),θ:=CORD(2),z:=CORD(3);
LOCAL x,y,result;
x:=r*COS(θ);
y:=r*SIN(θ);
z:=z;
//Guardar el resultado en la variable global RECTAN
RECTAN:=[x,y,z];
RETURN("x="+x+" y="+y+" z="+z);
END;

//Spherical to Cylindrical
//(ρ,θ,φ)-->(r,θ,z)
EXPORT SPHE2CYL(CORD)
BEGIN
LOCAL ρ:=CORD(1),θ:=CORD(2),φ:=CORD(3);
LOCAL r,z,result;
r:=ρ*SIN(φ);
θ:=θ;
z:=ρ*COS(φ);
//Guardar el resultado en la variable global CYLIN
CYLIN:=[r,θ,z];
RETURN("r="+r+" θ="+θ+" z="+z);
END;

//Cylindrical to Spherical
//(r,θ,z)-->(ρ,θ,φ)
EXPORT CYL2SPHE(CORD)
BEGIN
//Separacion de coordenadas en r,θ y z.
LOCAL r:=CORD(1),θ:=CORD(2),z:=CORD(3);
LOCAL ρ,φ,result;
ρ:=SQRT(r^2+z^2);
θ:=θ;
φ:=atan2(r,z);
//Guardar el resultado en la variable global SPHERE
SPHERE:=[ρ,θ,φ];
RETURN("ρ="+ρ+" θ="+θ+" φ="+φ);
END;
------------------------------------
....
So when I convert from Rectangular to Cylindrical in radian mode the "error" comes out, I convert to Cylindrical and take the result of the conversion and I pass it to the function that converts from Cylindrical to Rectangular and there it is I don't get the first input values I mean almost, I don't get 1 I get 1.0000000.... But when I work in degrees it works fine So is this a Bug ? an internal error ? Or a mistake of mine ?
09-02-2018, 01:10 AM
Post: #2
 Albert Chan Senior Member Posts: 2,142 Joined: Jul 2018
I think it is just rounding error.

However, you need to use atan2 instead of atan, to get angle of the correct quadrant.
09-02-2018, 02:28 AM
Post: #3
 Magnus512 Junior Member Posts: 23 Joined: Sep 2016
(09-02-2018 01:10 AM)Albert Chan Wrote:  I think it is just rounding error.

However, you need to use atan2 instead of atan, to get angle of the correct quadrant.

I get this, but this rounding error is present even in cas mode... another rounding error
09-02-2018, 02:56 AM
Post: #4
 Albert Chan Senior Member Posts: 2,142 Joined: Jul 2018
Approximate number (number with decimal point) always handled in approx mode, even in CAS

See http://www.hpmuseum.org/forum/thread-11296.html, post 10 by rprosperi
09-02-2018, 03:40 AM (This post was last modified: 09-02-2018 03:43 AM by Magnus512.)
Post: #5
 Magnus512 Junior Member Posts: 23 Joined: Sep 2016
(09-02-2018 02:56 AM)Albert Chan Wrote:  Approximate number (number with decimal point) always handled in approx mode, even in CAS

See http://www.hpmuseum.org/forum/thread-11296.html, post 10 by rprosperi

When I run the operation that give me that result outside the function ---> x:=r*COS(θ); I get 2 as answer. In CAS in home I get 1.9999999... So even If I write the decimal point the answer must be 2. or 1,
09-02-2018, 12:10 PM
Post: #6
 Albert Chan Senior Member Posts: 2,142 Joined: Jul 2018
(09-02-2018 03:40 AM)Magnus512 Wrote:  When I run the operation that give me that result outside the function ---> x:=r*COS(θ); I get 2 as answer.
In CAS in home I get 1.9999999... So even If I write the decimal point the answer must be 2. or 1,

When you say result outside the function, I assume you meant do it on a regular calculator.

Example, on my Casio FX-115MS, COS(1 + .0471975512) = 0.5, so X = 4 * 0.5 = 2.0

Prime had a bit more precision: 12 BCD digits in home, 48-bits binary float in CAS
This is the result of COS(1.0471975512):

Home => COS(10471975512/1E10) = 0.499999999997
C A S => COS(0x1.0c152382daf4) = 0x1.fffffffff30aP-2 ~ 0.49999999999705

Thus, X = 1.9999999 ...
But, if you round X to 10 digits, you get 2.0, same as my Casio

BTW, I do not have a Prime, above numbers based on specs I read from thread HP Prime Miscalculating
09-02-2018, 01:02 PM (This post was last modified: 09-02-2018 01:04 PM by JMB.)
Post: #7 JMB Member Posts: 98 Joined: Jan 2016
An easy way to convert among rectangular, cylindrical and spherical coordinates, is to use the built-in functions polar_coordinates and rectangular_coordinates. You can see my conversion functions in the post: http://www.hpmuseum.org/forum/thread-7005.html.

Of course my functions also suffer from round-off errors. Nonetheless, the functions polar_coordinates and rectangular_coordinates deliver exact results in CAS, when using the radian mode: Josep Mollera. HP PRIME, HW: C, SW: 2.1.14730 (2023 04 13).
09-02-2018, 01:39 PM (This post was last modified: 09-02-2018 01:41 PM by Albert Chan.)
Post: #8
 Albert Chan Senior Member Posts: 2,142 Joined: Jul 2018
I was curious of how Prime round the 53 bits float to its internal 48-bits mantissa.
Can someone with a Prime try this for me ? (CAS mode)

sin(3.0) * 2^50 - 158887e9

With IEEE 53-bits mantissa float, I get 3928234.84375

Will Prime store the chopped mantissa, result of 3928234 ?
Or, will it stored better rounded 48 bits, result of 3928235 ?
09-02-2018, 04:16 PM
Post: #9
 Magnus512 Junior Member Posts: 23 Joined: Sep 2016
(09-02-2018 01:39 PM)Albert Chan Wrote:  I was curious of how Prime round the 53 bits float to its internal 48-bits mantissa.
Can someone with a Prime try this for me ? (CAS mode)

sin(3.0) * 2^50 - 158887e9

With IEEE 53-bits mantissa float, I get 3928234.84375

Will Prime store the chopped mantissa, result of 3928234 ?
Or, will it stored better rounded 48 bits, result of 3928235 ?

3928234
09-02-2018, 04:22 PM
Post: #10
 Magnus512 Junior Member Posts: 23 Joined: Sep 2016
(09-02-2018 01:02 PM)JMB Wrote:  An easy way to convert among rectangular, cylindrical and spherical coordinates, is to use the built-in functions polar_coordinates and rectangular_coordinates. You can see my conversion functions in the post: http://www.hpmuseum.org/forum/thread-7005.html.

Of course my functions also suffer from round-off errors. Nonetheless, the functions polar_coordinates and rectangular_coordinates deliver exact results in CAS, when using the radian mode:

That's good but, I'm doing this by myself, so now I get the same error y multiple occasions, even in cas mode, and a new error comes out, I can't store the results in a matrix but I can store it in a list so...
09-02-2018, 04:33 PM
Post: #11
 Magnus512 Junior Member Posts: 23 Joined: Sep 2016
(09-02-2018 12:10 PM)Albert Chan Wrote:
(09-02-2018 03:40 AM)Magnus512 Wrote:  When I run the operation that give me that result outside the function ---> x:=r*COS(θ); I get 2 as answer.
In CAS in home I get 1.9999999... So even If I write the decimal point the answer must be 2. or 1,

When you say result outside the function, I assume you meant do it on a regular calculator.

Example, on my Casio FX-115MS, COS(1 + .0471975512) = 0.5, so X = 4 * 0.5 = 2.0

Prime had a bit more precision: 12 BCD digits in home, 48-bits binary float in CAS
This is the result of COS(1.0471975512):

Home => COS(10471975512/1E10) = 0.499999999997
C A S => COS(0x1.0c152382daf4) = 0x1.fffffffff30aP-2 ~ 0.49999999999705

Thus, X = 1.9999999 ...
But, if you round X to 10 digits, you get 2.0, same as my Casio

BTW, I do not have a Prime, above numbers based on specs I read from thread HP Prime Miscalculating
By outside the function I mean in CAS and home view directly, in home view I get the rounding error in cas I don't get that error. I'm not talking about other calculators here.
So maybe I have to activate the fixed or rounded mode in the program... Sucks
09-03-2018, 01:55 AM
Post: #12
 rprosperi Super Moderator Posts: 5,646 Joined: Dec 2013