HP Forums
COS(X) dislpay wrong answer - Printable Version

+- HP Forums (https://www.hpmuseum.org/forum)
+-- Forum: HP Calculators (and very old HP Computers) (/forum-3.html)
+--- Forum: HP Prime (/forum-5.html)
+--- Thread: COS(X) dislpay wrong answer (/thread-11315.html)



COS(X) dislpay wrong answer - Magnus512 - 09-01-2018 11:46 PM

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....
[attachment=6282]
But when I work in degrees it works fine
[attachment=6283]
So is this a Bug ? an internal error ? Or a mistake of mine ?


RE: COS(X) dislpay wrong answer - Albert Chan - 09-02-2018 01:10 AM

I think it is just rounding error.

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


RE: COS(X) dislpay wrong answer - Magnus512 - 09-02-2018 02:28 AM

(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...
[attachment=6286]
another rounding error


RE: COS(X) dislpay wrong answer - Albert Chan - 09-02-2018 02:56 AM

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


RE: COS(X) dislpay wrong answer - Magnus512 - 09-02-2018 03:40 AM

(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,


RE: COS(X) dislpay wrong answer - Albert Chan - 09-02-2018 12:10 PM

(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


RE: COS(X) dislpay wrong answer - JMB - 09-02-2018 01:02 PM

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:
[attachment=6289]


RE: COS(X) dislpay wrong answer - Albert Chan - 09-02-2018 01:39 PM

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 ?


RE: COS(X) dislpay wrong answer - Magnus512 - 09-02-2018 04:16 PM

(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


RE: COS(X) dislpay wrong answer - Magnus512 - 09-02-2018 04:22 PM

(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...


RE: COS(X) dislpay wrong answer - Magnus512 - 09-02-2018 04:33 PM

(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


RE: COS(X) dislpay wrong answer - rprosperi - 09-03-2018 01:55 AM

(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.