views:

48

answers:

1

I have seen this code floating around on the intertubes for determining if a Windows Mobile device has a VGA screen (code is inside a method of a Form class):

SizeF currentScreen = this.CurrentAutoScaleDimensions;
bool isVGA = currentScreen.Height == 192;

Is it possible that isVGA could be set to false even if the screen is VGA because of float imprecision (the Height property is a float)?

+1  A: 

Not in the example you present, because (IEEE 754-compliant, 32-bit) floating-point numbers can accurately represent all integers (whole numbers) whose absolute value is less than or equal to 2^24.

Michael Petrotta
Nice answer! You got straight to the point. If I was really paranoid I would worry about if I can be guaranteed that everyone will follow this standard. I would guess that it is reasonable to assume that everyone would at least follow the standard closely enough to accurately represent small, positive integers as floats. Is that right?
INTPnerd
@INTPnerd: It's worthwhile to verify that your programming language of choice complies with this (extremely common) standard. C# does, according to the 3.0 specification: *The two floating point types, float and double, are represented using the 32-bit single-precision and 64-bit double-precision IEEE 754 formats.*
Michael Petrotta
very helpful. thanks.
INTPnerd