Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Comparing float to int

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)?

like image 511
still_dreaming_1 Avatar asked Oct 14 '22 23:10

still_dreaming_1


1 Answers

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.

like image 86
Michael Petrotta Avatar answered Oct 17 '22 09:10

Michael Petrotta