Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Reply
Developer
Posts: 123
Registered: ‎09-18-2013
My Device: Dev Alpha B
My Carrier: BSNL INDIA
Accepted Solution

cos() returning incorrect values

Hi, have any of you tried getting values for cos() in native c++..? It seems that sin values are correct, but cos returns incorrect values. For eg, sin(pi/2) returns infinity. Any idea what might be wrong?
Developer
Posts: 6,152
Registered: ‎07-05-2012
My Device: Playbook, Dev Alpha C, Z10 LE, Z30
My Carrier: Orange

Re: cos() returning incorrect values

Where are you getting your value of Pi from, cos() uses radians not degrees, could that be it?


If you've been helped click on Like Button, if you've been saved buy the app. :smileyhappy:

Developer of stokLocker, Sympatico and Super Sentences.
Developer
Posts: 6,152
Registered: ‎07-05-2012
My Device: Playbook, Dev Alpha C, Z10 LE, Z30
My Carrier: Orange

Re: cos() returning incorrect values

You say Sin(Pi/2) is infinite but it should be 1 so that isn't correct either.


If you've been helped click on Like Button, if you've been saved buy the app. :smileyhappy:

Developer of stokLocker, Sympatico and Super Sentences.
Developer
Posts: 123
Registered: ‎09-18-2013
My Device: Dev Alpha B
My Carrier: BSNL INDIA

Re: cos() returning incorrect values

I'm sorry.. that was a problem with c++ in general. Not just blackberry. The value returned by cos(90*PI/180) returned -6.234e-17. That seems to be near zero. It was a limitation with float datatype. I'll try to use double and see if it corrects it.
Developer
Posts: 123
Registered: ‎09-18-2013
My Device: Dev Alpha B
My Carrier: BSNL INDIA

Re: cos() returning incorrect values

When I saw -6.234e-17, I thought it was infinity. Seems like I was wrong.
Developer
Posts: 1,523
Registered: ‎12-18-2012
My Device: Z30, Z10 LE, DevAlpha C, PlayBook

Re: cos() returning incorrect values

[ Edited ]

@SuhairZain,

 

#include <math.h>
#include <stdio.h>
int main(void)
{
        float pi = 3.14159265f;
        printf("Result: %f\n", sin(pi/2));
}

 

Result: 1.000000

 


Andrey Fidrya, @zmeyc on twitter
Developer
Posts: 123
Registered: ‎10-19-2012
My Device: Playbook, BB10DevAlphaB
My Carrier: Sonera, Saunalahti

Re: cos() returning incorrect values

Make sure you use the correct data type. cos() returns a double, cosf() a float
Developer
Posts: 153
Registered: ‎10-26-2010
My Device: Bold 9900
My Carrier: AT&T

Re: cos() returning incorrect values

[ Edited ]

Ignore me again  :No:  Not enough testing before posting.

 

--------------
Accepted PlayBook Applications: Marmi-doos, BASIC, Run and Hide, SuperClip
Accepted OS4-7 SmartPhone App: KeepLightOn