Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Function returning double parameter

This thread is locked; no one can reply to it. rss feed Print
 1   2   3 
Function returning double parameter
thats me
Member #5,501
February 2005

Hello, I got th following problem: a function is returning a value from type 'double', but the caller receives the wrong value. In the function's body all is correct, but the return value does not match. The prototype is as follows:
double grad2rad( int grad_angle );
and the body is:

double grad2rad( int grad_angle ){
  double new_angle;
  new_angle = (grad_angle/180.0)*PI;
  return new_angle;
}

PI is a constant: (22.0/7.0). Will be thankful for any help.

ReyBrujo
Moderator
January 2001
avatar

I don't see anything wrong. What you expect and what you get? Remember there may be rounding errors.

--
RB
光子「あたしただ…奪う側に回ろうと思っただけよ」
Mitsuko's last words, Battle Royale

miran
Member #2,407
June 2002

Quote:

PI is a constant: (22.0/7.0)

What's wrong with M_PI? Your code should look like this:

double grad2rad( int grad_angle ){
   return (double)grad_angle*M_PI/180.0;
}

M_PI lives in <math.h> by the way...

--
sig used to be here

Victor Williams Stafusa da Silva
Member #4,212
January 2004
avatar

Try this:

double grad2rad( int grad_angle ){
  double new_angle;
  new_angle = (PI * grad_angle)/180.0;
  return new_angle;
}

And if still doesn't works, then try this:

double grad2rad( int grad_angle ){
  double new_angle;
  new_angle = (PI * ((double) grad_angle))/180.0;
  return new_angle;
}

Edit: Or simpliest, but equivalent to that:

double grad2rad( int grad_angle ){
  return (PI * ((double) grad_angle))/180.0;
}

[The attack of the space bugs - Speedhack 2005] [Rambananas - Speedhack 2006] [Make clean - Speedhack 2009] [The source god - TINS 2010]

thats me
Member #5,501
February 2005

Thank you people, but the problem is not in the calculation, but in the data transfer (that's the reason I assign the value to a variable instead of just returning it). Before I return the variable, it has the right value, but afterwards not any more... :(

By the way, the result should be 1.570796 and the caller actually receives 1227133513.000000... ???

ReyBrujo
Moderator
January 2001
avatar

Are you using malloc or arrays somewhere? That sounds like memory corruption.

--
RB
光子「あたしただ…奪う側に回ろうと思っただけよ」
Mitsuko's last words, Battle Royale

Epsi
Member #5,731
April 2005
avatar

Show the "caller"'s code then.

___________________________________

piccolo: "soon all new 2d alegro games will be better. after i finsh my MMRPG. my game will serve as a code reference. so you can understand and grab code from."
piccolo: "just wait until my invetion comes out its going to take the wii to the next leave of game play. it will run sony and microsoft out of busness if i dont let them use it aswell."

thats me
Member #5,501
February 2005

Quote:

Are you using malloc or arrays somewhere? That sounds like memory corruption.

Actually no. The problem was elsewhere...
The declaration of the function is in external header file and I forgot to include the corresponding header. The compiler didn't complain at all and I suspect it thought the return value is by default int or something like that, so it interpreted the data wrong. Strange stuff... Hopefully my failure helps someone else, who is experiencing the same problem.

stone830209
Member #7,509
July 2006

you are right,in your situation the compiler will recognize the return value of
this function as int by default.

gnolam
Member #2,030
March 2002
avatar

miran said:

What's wrong with M_PI? Your code should look like this:

double grad2rad( int grad_angle ){
return (double)grad_angle*M_PI/180.0;
}

M_PI lives in <math.h> by the way...

Normally lives in math.h, one should say. IIRC, M_PI is neither C89 nor C99. It might be POSIX, but I don't think MSVC supports it.

But either way, using 22/7 instead of a sensible value for pi is just silly. :P

--
Move to the Democratic People's Republic of Vivendi Universal (formerly known as Sweden) - officially democracy- and privacy-free since 2008-06-18!

tobing
Member #5,213
November 2004
avatar

Quote:

but I don't think MSVC supports it

It does (math.h, MSVC 7.1).

Evert
Member #794
November 2000
avatar

Quote:

PI is a constant: (22.0/7.0)

That is a horrible approximation.

Quote:

The compiler didn't complain at all

It does if you enable warnings (which is always a good idea).

miran
Member #2,407
June 2002

Quote:

That is a horrible approximation.

Well, at least it's better than 3. :)

--
sig used to be here

FMC
Member #4,431
March 2004
avatar

I usually just use 3.142, no need to get more precise.

[FMC Studios] - [Caries Field] - [Ctris] - [Pman] - [Chess for allegroites]
Written laws are like spiders' webs, and will, like them, only entangle and hold the poor and weak, while the rich and powerful will easily break through them. -Anacharsis
Twenty years from now you will be more disappointed by the things that you didn't do than by the ones you did do. So throw off the bowlines. Sail away from the safe harbor. Catch the trade winds in your sails. Explore. Dream. Discover. -Mark Twain

Corelian
Member #3,376
March 2003
avatar

You people make mathematicians and your math teachers cry. Like this: :'(

FMC
Member #4,431
March 2004
avatar

It depends on what you are doing, in computer games it's a waste to use many significant figures (unless, maybe, in some kind of advanced physics simulation), usually one or two are more than enough...

[FMC Studios] - [Caries Field] - [Ctris] - [Pman] - [Chess for allegroites]
Written laws are like spiders' webs, and will, like them, only entangle and hold the poor and weak, while the rich and powerful will easily break through them. -Anacharsis
Twenty years from now you will be more disappointed by the things that you didn't do than by the ones you did do. So throw off the bowlines. Sail away from the safe harbor. Catch the trade winds in your sails. Explore. Dream. Discover. -Mark Twain

tobing
Member #5,213
November 2004
avatar

It's exactly that what Corelian was referring to...

And before you talk about performance, do some profiling to find out, where your program does spend its time. You will be surprised!

Evert
Member #794
November 2000
avatar

Quote:

in computer games it's a waste to use many significant figures

How? It is just as fast to use more correct digits as it is to use the same number of incorrect ones. What are you wasting?

FMC
Member #4,431
March 2004
avatar

Show me the advantage of using 10 instead of 3 significant figures in the average game... anyway using, for example, floats (less precision) instead of doubles is surely faster, likewise if i knew i was using 2 digits of precision i could simply treat everything as an int and just divide by 100 at the end, this would be much harder if i had to use M_PI (god knows how many digits of precision it uses).
I agree that this isn't a huge impact on performance but it just seems an overkill to use 20 digits of precision when at the end you're working with pixels (integers...)

[FMC Studios] - [Caries Field] - [Ctris] - [Pman] - [Chess for allegroites]
Written laws are like spiders' webs, and will, like them, only entangle and hold the poor and weak, while the rich and powerful will easily break through them. -Anacharsis
Twenty years from now you will be more disappointed by the things that you didn't do than by the ones you did do. So throw off the bowlines. Sail away from the safe harbor. Catch the trade winds in your sails. Explore. Dream. Discover. -Mark Twain

Neil Walker
Member #210
April 2000
avatar

I use AL_PI. Haven't a clue the difference as I never looked at M_PI and I know it'll work for every allegro program :)

Neil.
MAME Cabinet Blog / AXL LIBRARY (a games framework) / AXL Documentation and Tutorial

wii:0356-1384-6687-2022, kart:3308-4806-6002. XBOX:chucklepie

Evert
Member #794
November 2000
avatar

Quote:

Show me the advantage of using 10 instead of 3 significant figures in the average game...

Try shooting a missile or particle under a specific angle across the screen and see if it moves to where it's supposed to go. You'll be surprised at how far off course it will go. If you can do it right for the same cost as doing it wrong, why not do it right?

That wasn't what I asked you though. I asked you how it's wasteful to use the normal approximation of pi instead of, say, 22/7.

miran
Member #2,407
June 2002

Quote:

I agree that this isn't a huge impact on performance but it just seems an overkill to use 20 digits of precision when at the end you're working with pixels (integers...)

Even in simple games with simple physics basically what you're doing is you're numerically solving differential equations. For this task there are several methods but because for a simple game fourth order Runge-Kutta would be overkill, most people just use the Newton method (or whatever it's actually called). That's when you do x_pos = x_pos + velocity_x and things like that. The advantage of this method is that it is extremely easy to implement. The disadvantage though is that all random errors in data or whatever add up over time (with Runge-Kutta and other methods they effectively get canceled out in successive steps), which results in what Evert said. You fire a missile in a certain direction, but after many steps of physics simulation with the Newton method, the missile is several pixels off because of round off error that kept piling on and on at each step...

--
sig used to be here

FMC
Member #4,431
March 2004
avatar

Quote:

That wasn't what I asked you though. I asked you how it's wasteful to use the normal approximation of pi instead of, say, 22/7.

Did you read the rest of the post?

Quote:

Even in simple games with simple physics basically what you're doing is you're numerically solving differential equations. For this task there are several methods but because for a simple game fourth order Runge-Kutta would be overkill, most people just use the Newton method (or whatever it's actually called). That's when you do x_pos = x_pos + velocity_x and things like that. The advantage of this method is that it is extremely easy to implement. The disadvantage though is that all random errors in data or whatever add up over time (with Runge-Kutta and other methods they effectively get canceled out in successive steps), which results in what Evert said. You fire a missile in a certain direction, but after many steps of physics simulation with the Newton method, the missile is several pixels off because of round off error that kept piling on and on at each step...

Agreed, but does it really matter much while playing?

[FMC Studios] - [Caries Field] - [Ctris] - [Pman] - [Chess for allegroites]
Written laws are like spiders' webs, and will, like them, only entangle and hold the poor and weak, while the rich and powerful will easily break through them. -Anacharsis
Twenty years from now you will be more disappointed by the things that you didn't do than by the ones you did do. So throw off the bowlines. Sail away from the safe harbor. Catch the trade winds in your sails. Explore. Dream. Discover. -Mark Twain

tobing
Member #5,213
November 2004
avatar

<sarcasm on>
Seems there's no reason to do it right if you can also do it wrong.
<sarcasm off>

miran
Member #2,407
June 2002

Quote:

Agreed, but does it really matter much while playing?

With some types of games it does. Suppose you have a racing simulation where you simulate the suspension and all other things. Now if after a few seconds the suspension starts to wobble more and more and finally goes completely out of control because you're physics simulation is unstable, then I'd say it matters.

--
sig used to be here

 1   2   3 


Go to: