isNaN() doesn't return truw with -nan(ind)

Problems building or running the engine, queries about how to use features etc.
Post Reply
User avatar
suny
Greenskin
Posts: 137
Joined: Thu Mar 12, 2020 5:53 pm
x 60

isNaN() doesn't return truw with -nan(ind)

Post by suny »

Ogre Version: :?: Ogre 1.13

Hi,

I'm trying to check if a vector3 is NaN, but it seems like the check doesn't work for me:

Code: Select all

if (EnemyPos.isNaN())
	EnemyPos = Ogre::Vector3(0, 0, 0);

EnemyPos = Ogre::Vector3(0, 0, 0) is never evaluated, even with a (Ogre::VectorBase<3,float> = {x=-nan(ind) y=-nan(ind) z=-nan(ind)) value.

Do I miss something?
S.

paroj
OGRE Team Member
OGRE Team Member
Posts: 1994
Joined: Sun Mar 30, 2014 2:51 pm
x 1074
Contact:

Re: isNaN() doesn't return truw with -nan(ind)

Post by paroj »

are you using /fp:fast or similar?

User avatar
suny
Greenskin
Posts: 137
Joined: Thu Mar 12, 2020 5:53 pm
x 60

Re: isNaN() doesn't return truw with -nan(ind)

Post by suny »

Yes, I'm using Fast (/fp:fast) on my app.
S.

paroj
OGRE Team Member
OGRE Team Member
Posts: 1994
Joined: Sun Mar 30, 2014 2:51 pm
x 1074
Contact:

Re: isNaN() doesn't return truw with -nan(ind)

Post by paroj »

/fp:fast disables default isNaN testing. This might help: https://github.com/OGRECave/ogre/pull/2615

User avatar
dark_sylinc
OGRE Team Member
OGRE Team Member
Posts: 5296
Joined: Sat Jul 21, 2007 4:55 pm
Location: Buenos Aires, Argentina
x 1278
Contact:

Re: isNaN() doesn't return truw with -nan(ind)

Post by dark_sylinc »

/fp:fast assumes NaNs cannot happen and optimizes them out.

That includes std::is_nan.
The patch may work if the optimizer stops seeing the optimization opportunity, but it will be a brittle approach because it may still break in other places.

The only two solutions are to either disable the compiler flag, or prevent the nan from happening (e.g. avoid 0/0, sqrt of negative values etc)

paroj
OGRE Team Member
OGRE Team Member
Posts: 1994
Joined: Sun Mar 30, 2014 2:51 pm
x 1074
Contact:

Re: isNaN() doesn't return truw with -nan(ind)

Post by paroj »

dark_sylinc wrote: Tue Oct 04, 2022 5:39 am

That includes std::is_nan.

yeah, should have finished reading this thread first: https://groups.google.com/g/llvm-dev/c/Ys0hpgTFMH8

I had the impression that the consensus was to keep std::isnan with fast math, but it is not.

Post Reply