Post Reply 
COS(X) dislpay wrong answer
09-01-2018, 11:46 PM (This post was last modified: 09-02-2018 02:28 AM by Magnus512.)
Post: #1
COS(X) dislpay wrong answer
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);
//Variables para guardar resultado temporal
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);
//Variables para guardar resultado temporal
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);
//Variables para guardar resultado temporal
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);
//Variables para guardar resultado temporal
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
//Separacion de coordenadas en (ρ,θ,φ)
LOCAL ρ:=CORD(1),θ:=CORD(2),φ:=CORD(3);
//Variables para guardar resultado temporal
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);
//Variables para guardar resultado temporal
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 ?
Find all posts by this user
Quote this message in a reply
09-02-2018, 01:10 AM
Post: #2
RE: COS(X) dislpay wrong answer
I think it is just rounding error.

However, you need to use atan2 instead of atan, to get angle of the correct quadrant.
Find all posts by this user
Quote this message in a reply
09-02-2018, 02:28 AM
Post: #3
RE: COS(X) dislpay wrong answer
(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
Find all posts by this user
Quote this message in a reply
09-02-2018, 02:56 AM
Post: #4
RE: COS(X) dislpay wrong answer
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
Find all posts by this user
Quote this message in a reply
09-02-2018, 03:40 AM (This post was last modified: 09-02-2018 03:43 AM by Magnus512.)
Post: #5
RE: COS(X) dislpay wrong answer
(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,
Find all posts by this user
Quote this message in a reply
09-02-2018, 12:10 PM
Post: #6
RE: COS(X) dislpay wrong answer
(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
Find all posts by this user
Quote this message in a reply
09-02-2018, 01:02 PM (This post was last modified: 09-02-2018 01:04 PM by JMB.)
Post: #7
RE: COS(X) dislpay wrong answer
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).
Find all posts by this user
Quote this message in a reply
09-02-2018, 01:39 PM (This post was last modified: 09-02-2018 01:41 PM by Albert Chan.)
Post: #8
RE: COS(X) dislpay wrong answer
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 ?
Find all posts by this user
Quote this message in a reply
09-02-2018, 04:16 PM
Post: #9
RE: COS(X) dislpay wrong answer
(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
Find all posts by this user
Quote this message in a reply
09-02-2018, 04:22 PM
Post: #10
RE: COS(X) dislpay wrong answer
(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...
Find all posts by this user
Quote this message in a reply
09-02-2018, 04:33 PM
Post: #11
RE: COS(X) dislpay wrong answer
(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
Find all posts by this user
Quote this message in a reply
09-03-2018, 01:55 AM
Post: #12
RE: COS(X) dislpay wrong answer
(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

For the record, that comment was about the 50g, and may not apply to the Prime, or CAS mode; I frankly don't get CAS mode and it's 413,000 special function names (which must be in lower-case, unless it's Tuesday and cloudy) and other arcane rules. Not saying CAS isn't good, it very clearly is amazingly capable, it's just too persnickety for me.

--Bob Prosperi
Find all posts by this user
Quote this message in a reply
Post Reply 




User(s) browsing this thread: 1 Guest(s)