Jump to content

flaot to double cast problem

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
5 replies to this topic

#1
kavery

kavery

    Learning Programmer

  • Members
  • PipPipPip
  • 38 posts
hi i'm trying to cast a float value to a double normally i would bother but i'm use Atan2 from the namespace math which want a double varibles passed to it,
the code looks like :cursing:

AngleDeg = Math.Atan2((double)(mTargetPos.Y - Pos.Y), (double)(mTargetPos.X - Pos.X));

but it dosn't like the convert. is there a math function in another namespace that uses float or away to trick the function to like having a float to deal with.

#2
FlashM

FlashM

    Learning Programmer

  • Members
  • PipPipPip
  • 90 posts
Please post complete code. It's impossible to resolve this issue with that little information...


I tried:

float a = 5.3f;
float b = 2.7f;
double c = Math.Atan2(a, b);

and it works perfectly. Is it giving you wrong result or what? Can you be more specific?

#3
PGP_Protector

PGP_Protector

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 253 posts
Have you tried using convert?

AngleDeg = Math.Atan2(Convert.ToDouble(mTargetPos.Y - Pos.Y), Convert.ToDouble(mTargetPos.X - Pos.X));


#4
kavery

kavery

    Learning Programmer

  • Members
  • PipPipPip
  • 38 posts
i didn't know that exiested, but i tried it and i still get the same error that it can't convert float to double

#5
FlashM

FlashM

    Learning Programmer

  • Members
  • PipPipPip
  • 90 posts
It would be much easier if you posted your code or just a piece of code. But casting or converting float to double should work.
What is this mTarget variable? What type it is?

#6
kavery

kavery

    Learning Programmer

  • Members
  • PipPipPip
  • 38 posts
I've just figured it out. quite stupidly i focus on the float to double. but what i forgot was to canvert the result back to a float .

AngleDeg = (float)Math.Atan2(Convert.ToDouble(mTargetPos.Y - Pos.Y), Convert.ToDouble(mTargetPos.X - Pos.X));

sorry it was just my forgetful ness

sorry to trouble all of you