Sin Cos

Post your Gambas programming questions here.
Post Reply
mugwump
Posts: 2
Joined: Tuesday 10th August 2021 6:02pm

Sin Cos

Post by mugwump » Tuesday 10th August 2021 6:21pm

in gambas playground i typed:-


dim a as float
dim b as float
a=rad(90)

print sin(a)
print cos(a)

result:-

1
6.12323399573677E-17

What is wrong here?

Thanks.

User avatar
grayghost4
Posts: 119
Joined: Wednesday 05th December 2018 5:00am
Location: Marengo, illinois usa

Re: Sin Cos

Post by grayghost4 » Wednesday 11th August 2021 1:31am

looks correct to me

rad(90) is equal to 1/2 of pi = 3.14/2 = 1.5707

a = 1.5707 not 90 deg.

the rest is simple math.

what is it you are trying to do?

vuott
Posts: 97
Joined: Wednesday 05th April 2017 6:07pm
Location: European Union

Re: Sin Cos

Post by vuott » Wednesday 11th August 2021 2:26pm

mugwump wrote:
Tuesday 10th August 2021 6:21pm
print cos(a)

6.12323399573677E-17
Today Gianluigi (a member of italian forum) asked Minisini about the problem of this result.
Minisini replied:

" This is normal. Rad() uses the Pi constant, and floating point numbers are never 100% accurate. "

User avatar
stevedee
Posts: 456
Joined: Monday 20th March 2017 6:06pm

Re: Sin Cos

Post by stevedee » Wednesday 11th August 2021 5:18pm

mugwump wrote:
Tuesday 10th August 2021 6:21pm

print cos(a)

result:-
6.12323399573677E-17

What is wrong here?
As has already been stated, these kind of rounding errors are to be expected on a digital computer.

3 points:-
- you will get the same result if you perform the same calculations in LibreOffice Calc (...on the same computer)
- the error is very, very small
- using something like;
myCosine = Cos(Rad(90)) 
...doesn't make much sense.
It would be better to code something like this;
myCosine = Round(Cos(Rad(90)),-10)
I hope this helps.


Edit; sorry, forgot the Rad

mugwump
Posts: 2
Joined: Tuesday 10th August 2021 6:02pm

Re: Sin Cos

Post by mugwump » Thursday 12th August 2021 9:50am

OK Thanks for that .
I need to brush up on my maths!

Post Reply