Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Discussion / Problems with cos( ) function.

Author
Message
mr joe
20
Years of Service
User Offline
Joined: 12th Aug 2004
Location: Edinburger, Scotland. DBC
Posted: 21st Dec 2004 22:24
Well, I was just trying to make a pong game, something easy I thought, but I can't even do this right it seems. For some reason DB (I have classic) is not working out the right cos values.

Check out my source (no media needed), can anyone help?

Chris K
21
Years of Service
User Offline
Joined: 7th Oct 2003
Location: Lake Hylia
Posted: 22nd Dec 2004 05:10
That's just inaccuracies.

Instead of 0 it's 0.000000008

Not too bad.
It's because of how real numbers are stored. It won't affect a game.

A maths demo, maybe, a game, no.
Benjamin
22
Years of Service
User Offline
Joined: 24th Nov 2002
Location: France
Posted: 23rd Dec 2004 22:26
@Chris K: erm it shouldn't come out as 0.000000008, because he only used integer numbers.

CURRENT PROJECT: Chaos Hizzle Demo
Chris K
21
Years of Service
User Offline
Joined: 7th Oct 2003
Location: Lake Hylia
Posted: 24th Dec 2004 00:21
Do you know how sin is calculated?

It's an infinite series. A computer obviously can't calculated an infinite series so I guess it does the first 100 terms or so. That is why it doesn't get to 0 exactly.

Also, floats are stored by using multiples of 2, so there are always slight inaccuracies. Try storing 1.3 in a variable then printing it.
thinkdigital
21
Years of Service
User Offline
Joined: 18th Oct 2003
Location:
Posted: 25th Dec 2004 07:30
Yes, but if he is using integers and and the cos() is 0.00000008 some, then would DBC not turn it into 0?

"Never put off for tomorrow what you can put off for the day AFTER tomorrow..."

eGeneric Works DB site - egenericworks.com
Emperor Baal
21
Years of Service
User Offline
Joined: 1st Dec 2003
Location: The Netherlands - Oudenbosch
Posted: 25th Dec 2004 08:48
sin / tan / cos functions should be called using:

float# = sin / tan / cos ( float# )

this means, you pass a float and it will return a float. You pass a integer, it converts this into a float, and still returns a float.

Benjamin
22
Years of Service
User Offline
Joined: 24th Nov 2002
Location: France
Posted: 25th Dec 2004 20:18 Edited at: 25th Dec 2004 20:18
Quote: "Do you know how sin is calculated?"

You obviously don't know much about programming, saying that an integer value can store values like 0.000000008

thinkdigital got it right

Cerry Mhristmas!
--
CURRENT PROJECT: Chaos Hizzle Demo
empty
22
Years of Service
User Offline
Joined: 26th Aug 2002
Location: 3 boats down from the candy
Posted: 25th Dec 2004 21:01
Quote: " @Chris K: erm it shouldn't come out as 0.000000008, because he only used integer numbers."

Although he uses integer numbers to position the ball, he doesn't use integers to display the results of the sin and cos function.

t$ = "cos deg: " + str$(cos(deg))
text 0,20,t$

cos() returns a float value, str$() converts this float to a string (no integers used).


But the main problem of this example is the fact that he uses integer variables to store the results of the sin and cos functions.




Play Nice! Play Basic! Version 1.06
thinkdigital
21
Years of Service
User Offline
Joined: 18th Oct 2003
Location:
Posted: 26th Dec 2004 03:05
I thought so. There's the problem; he just getting 0. All you need to do is use floats.

"Never put off for tomorrow what you can put off for the day AFTER tomorrow..."

eGeneric Works DB site - egenericworks.com
mr joe
20
Years of Service
User Offline
Joined: 12th Aug 2004
Location: Edinburger, Scotland. DBC
Posted: 27th Dec 2004 05:21
Thanks everyone, floaters it is.

Login to post a reply

Server time is: 2025-05-24 09:55:22
Your offset time is: 2025-05-24 09:55:22