N.A.N value ---SOLVED---

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

User avatar
short
ES Beta Backer
ES Beta Backer
Posts: 548
Joined: Thu Apr 30, 2009 2:22 am
Current Project: c++, c
Favorite Gaming Platforms: SNES, PS2, SNES, SNES, PC NES
Programming Language of Choice: c, c++
Location: Oregon, US

Re: N.A.N value

Post by short »

THe Floating Brain wrote:
short wrote:

Code: Select all

if( x == 0 && y == 0 && Vector.getZ() == 0 )
Technically, you should use float comparison :)
I did not get a compiler warning. Vector.getZ() also returns a float.
That's because an explicit conversion is happening.
THe Floating Brain wrote: Agreed that I should probably put my magnitude calculation into a function/method so it can return the magnitude. The reason for not sqrt'ing it is so I can use the if statement to make sure my number does not come out N.A.N.
How would squaring two floats EVER return in NAN?
My github repository contains the project I am currently working on,
link: https://github.com/bjadamson
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Re: N.A.N value

Post by Falco Girgis »

^ He has some very good points. You really do need to start using epsilon-based floating-point comparisons.
User avatar
THe Floating Brain
Chaos Rift Junior
Chaos Rift Junior
Posts: 284
Joined: Tue Dec 28, 2010 7:22 pm
Current Project: RTS possible Third Person shooter engine.
Favorite Gaming Platforms: PC, Wii, Xbox 360, GAME CUBE!!!!!!!!!!!!!!!!!!!!!!
Programming Language of Choice: C/C++, Python 3, C#
Location: U.S

Re: N.A.N value

Post by THe Floating Brain »

I tried it but I get a error with:

Code: Select all

inline bool operator==(const Vector &lhs, const int r) {
    return ((lhs.x - r) < FLT_EPSILON)  && ((lhs.y - r) < FLT_EPSILON) && ((lhs.z - r) < FLT_EPSILON);
}
I don't usually use operator command ( I should probably use it more it is a nessary skill ), and I am unfamiliar with how to fix this:

Code: Select all

1>------ Build started: Project: Vector Scan Area, Configuration: Debug Win32 ------
1>Compiling...
1>Main.cpp
1>c:\users\aperson\documents\visual studio 2008\projects\vector scan area\vector scan area\vector calculator.h(24) : error C2804: binary 'operator ==' has too many parameters
1>c:\users\aperson\documents\visual studio 2008\projects\vector scan area\vector scan area\vector calculator.h(24) : error C2333: 'Math::Vector::Vector_Calculator::operator ==' : error in function declaration; skipping function body
1>c:\users\aperson\documents\visual studio 2008\projects\vector scan area\vector scan area\vector calculator.h(36) : error C2678: binary '==' : no operator found which takes a left-hand operand of type 'Containers::Base::XYZ' (or there is no acceptable conversion)
1>        c:\program files\microsoft sdks\windows\v6.0a\include\guiddef.h(192): could be 'int operator ==(const GUID &,const GUID &)'
1>        while trying to match the argument list '(Containers::Base::XYZ, int)'
1>Build log was saved at "file://c:\Users\aperson\Documents\Visual Studio 2008\Projects\Vector Scan Area\Vector Scan Area\Debug\BuildLog.htm"
1>Vector Scan Area - 3 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
I feel like such a noob :oops: :oops: :oops:
"Why did we say we were going to say we were going to change the world tomorrow yesterday? Maybe you can." - Myself

ImageImage
User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

Re: N.A.N value

Post by Ginto8 »

the error is saying you're providing too many arguments. As it is, you'd have to say it Vec1 == Vec2, 3. And that's just plain wrong. r, the const int, is supposed to be provided in the source file, not as a parameter to the operator.
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
User avatar
THe Floating Brain
Chaos Rift Junior
Chaos Rift Junior
Posts: 284
Joined: Tue Dec 28, 2010 7:22 pm
Current Project: RTS possible Third Person shooter engine.
Favorite Gaming Platforms: PC, Wii, Xbox 360, GAME CUBE!!!!!!!!!!!!!!!!!!!!!!
Programming Language of Choice: C/C++, Python 3, C#
Location: U.S

Re: N.A.N value

Post by THe Floating Brain »

So you are saying this is incorrect:

Code: Select all

//Modified to fit my program. I did test this with a struct instead of a class I get the same result.//
inline bool operator==( const Containers::Base::XYZ &lhs, const int r ) {
				return ((lhs.getX() - r) < FLT_EPSILON)  && ((lhs.getY() - r) < FLT_EPSILON) && ((lhs.getZ() - r) < FLT_EPSILON);
}
Containers::Base::XYZ Vector_Calculation( Containers::Base::XYZ Goto, Containers::Base::XYZ Position, float Speed )
{
...
if( Vector == 0 )
   return Vector;
...
}
Or are you saying this should be declared outside the class "Vector_Calculator" in the source file?
???
"Why did we say we were going to say we were going to change the world tomorrow yesterday? Maybe you can." - Myself

ImageImage
User avatar
short
ES Beta Backer
ES Beta Backer
Posts: 548
Joined: Thu Apr 30, 2009 2:22 am
Current Project: c++, c
Favorite Gaming Platforms: SNES, PS2, SNES, SNES, PC NES
Programming Language of Choice: c, c++
Location: Oregon, US

Re: N.A.N value

Post by short »

Yes. Define the method outside the class, inside the same namespace. Preferably within the same source file. The reason for this is you can completely define the == operator in terms of vectors public interface.

My experience tells me, if you can declare a function in terms of its pre-existing public interface, and it makes sense to do so, then do it. There is no reason to lower encapsulation by making the == operator a member function of the vector class.

To fit your example from you first post

Code: Select all

namespace Math
{
   namespace Vector
   {
      class Vector_Calculator
      {
      public:
          .... blah blah blah
      }; // end of class

    // put operator overload here
    inline bool operator==(const Vector &lhs, const int r) {
        return ((lhs.x - r) < FLT_EPSILON)  && ((lhs.y - r) < FLT_EPSILON) && ((lhs.z - r) < FLT_EPSILON);
    }
    
   }
}
My github repository contains the project I am currently working on,
link: https://github.com/bjadamson
User avatar
THe Floating Brain
Chaos Rift Junior
Chaos Rift Junior
Posts: 284
Joined: Tue Dec 28, 2010 7:22 pm
Current Project: RTS possible Third Person shooter engine.
Favorite Gaming Platforms: PC, Wii, Xbox 360, GAME CUBE!!!!!!!!!!!!!!!!!!!!!!
Programming Language of Choice: C/C++, Python 3, C#
Location: U.S

Re: N.A.N value

Post by THe Floating Brain »

Thank you :-D
"Why did we say we were going to say we were going to change the world tomorrow yesterday? Maybe you can." - Myself

ImageImage
Post Reply