Sign in to follow this  
KingofNoobs

== comparison not working

Recommended Posts

KingofNoobs    305
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.
[code]
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;
}
[/code]

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

Share this post


Link to post
Share on other sites
C0lumbo    4411
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.

Share this post


Link to post
Share on other sites
Ectara    3097
[quote name='caldiar' timestamp='1355392945' post='5010131']
You could also check the width and height to see if they're multiples of 16 and 9 respectively such as:

[CODE]
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;
}
[/CODE]
[/quote]
Almost, but the important part is the ratio: 32 by 9 is not a 16:9 ratio.

Share this post


Link to post
Share on other sites
caldiar    715
[quote name='Ectara' timestamp='1355393154' post='5010133']
Almost, but the important part is the ratio: 32 by 9 is not a 16:9 ratio.
[/quote]

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

Share this post


Link to post
Share on other sites
Ectara    3097
[quote name='caldiar' timestamp='1355393584' post='5010137']
Well spotted! Perhaps a sign for me to turn off the computer for the night and go to bed.
[/quote]
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this