Jump to content

  • Log In with Google      Sign In   
  • Create Account


== comparison not working


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
9 replies to this topic

#1 KingofNoobs   Members   -  Reputation: 301

Like
0Likes
Like

Posted 13 December 2012 - 03:39 AM

Hello all,

I have the following code that works in case (2) but not in case (1). Can anyone explain this to me? This could be the cause of many of my more subtle bugs.
int main()
{
	sf::Window gameWindow(sf::VideoMode::GetMode(0), "Block Buster",sf::Style::Fullscreen);
	uint32_t VideoModesCount = sf::VideoMode::GetModesCount();
	for(uint32_t i = 0; i < VideoModesCount; ++i)
	{
		sf::VideoMode Mode = sf::VideoMode::GetMode(i);
		float sixteenByNine = (float)16/9;
		float aspectRatio = (float)Mode.Width / Mode.Height;
	
		(1) if((float)((float)Mode.Width / Mode.Height) == ((float)16/9))
		(2) if(aspectRatio == sixteenByNine)
		std::cout << "Video Mode " << i << ":\tWidth: " << Mode.Width
			<<"\tHeight: " << Mode.Height << "\tBpp: " << Mode.BitsPerPixel
			<< std::endl;
	}
	// ...
	return EXIT_SUCCESS;
}

Is there some weird truncation that is happening when I am directly comparing the variables inside the if() statement?

Thanks for your help.

- Dave Ottley

I wonder as I wander...

http://www.davesgameoflife.com


Sponsor:

#2 Ectara   Crossbones+   -  Reputation: 2912

Like
7Likes
Like

Posted 13 December 2012 - 03:50 AM

Floating point inaccuracy; one should not directly test for equality between floating point numbers.

#3 C0lumbo   Crossbones+   -  Reputation: 2197

Like
3Likes
Like

Posted 13 December 2012 - 03:54 AM

TBH, the two options look equivalent to me, so I don't know why your results are inconsistent, but I'd recommend that you never try to compare the result of floating point calculations using ==. In general, instead of:

if (fA == fB)

You should use something like.

if (fabsf(fA - fB) < 0.0001f)

Because you almost always have precision errors in any floating point calculation and even if it works in debug, it might not work in release because the floating point operations can be rearranged to produce slightly different results.

#4 KingofNoobs   Members   -  Reputation: 301

Like
0Likes
Like

Posted 13 December 2012 - 03:54 AM

However, it works in case 2. Why should this work any better than in case 1?

I wonder as I wander...

http://www.davesgameoflife.com


#5 KingofNoobs   Members   -  Reputation: 301

Like
0Likes
Like

Posted 13 December 2012 - 03:55 AM

I see, I just read your post Columbo. I will try your code to see if it works.

I wonder as I wander...

http://www.davesgameoflife.com


#6 KingofNoobs   Members   -  Reputation: 301

Like
0Likes
Like

Posted 13 December 2012 - 03:59 AM

FYI, it works! Thank you very much kind sirs.

I wonder as I wander...

http://www.davesgameoflife.com


#7 caldiar   Members   -  Reputation: 702

Like
0Likes
Like

Posted 13 December 2012 - 04:02 AM

Ectara's right. Removing the poorly thought out suggestion.

Edited by caldiar, 13 December 2012 - 04:14 AM.


#8 Ectara   Crossbones+   -  Reputation: 2912

Like
1Likes
Like

Posted 13 December 2012 - 04:05 AM

You could also check the width and height to see if they're multiples of 16 and 9 respectively such as:

if( ((Mode.Width % 16) == 0) && ((Mode.Height % 9) == 0))
{
	 std::cout << "Video Mode " << i << ":	Width: " << Mode.Width
			<<"	Height: " << Mode.Height << "	Bpp: " << Mode.BitsPerPixel
			<< std::endl;
}

Almost, but the important part is the ratio: 32 by 9 is not a 16:9 ratio.

#9 caldiar   Members   -  Reputation: 702

Like
0Likes
Like

Posted 13 December 2012 - 04:13 AM

Almost, but the important part is the ratio: 32 by 9 is not a 16:9 ratio.


Well spotted! Perhaps a sign for me to turn off the computer for the night and go to bed.

Edited by caldiar, 13 December 2012 - 04:13 AM.


#10 Ectara   Crossbones+   -  Reputation: 2912

Like
0Likes
Like

Posted 13 December 2012 - 04:25 AM

Well spotted! Perhaps a sign for me to turn off the computer for the night and go to bed.

Happens to everyone every now and again. :)

Personally, I used an enum for preset aspect ratios, with an extra value to signify that it should calculate it from the width and height.

I think that if you know the ranges for the width and height, and you need determinism, you could try fixed point math.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS