SQL Server float datatype
Posted
by Martin Smith
on Stack Overflow
See other posts from Stack Overflow
or by Martin Smith
Published on 2010-05-30T12:07:13Z
Indexed on
2010/05/30
12:12 UTC
Read the original article
Hit count: 195
sql-server
|floating-point
The documentation for SQL Server Float says
Approximate-number data types for use with floating point numeric data. Floating point data is approximate; therefore, not all values in the data type range can be represented exactly.
Which is what I expected it to say. If that is the case though why does the following return 'Yes' in SQL Server
DECLARE @D float
DECLARE @E float
set @D = 0.1
set @E = 0.5
IF ((@D + @D + @D + @D +@D) = @E)
BEGIN
PRINT 'YES'
END
ELSE
BEGIN
PRINT 'NO'
END
but the equivalent C++ program returns "No"?
#include <iostream>
using namespace std;
int main()
{
float d = 0.1F;
float e = 0.5F;
if((d+d+d+d+d) == e)
{
cout << "Yes";
}
else
{
cout << "No";
}
}
© Stack Overflow or respective owner