Click here to Skip to main content
15,896,201 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi
I'm using MS SQL Server 2005.Check out the below query.
SQL
declare @str float
select  @str =convert(float,isnull(30452.8885,0)) * convert(float,isnull(219.8700,0))
select convert(decimal(38,10),@str) A,@str B


If we do the above multiplication using any calculator we will get B(6695676.594495) as output.
But then when we convert the multiplication of two values to decimal(38,10), sql gives 6695676.5944950003 as A, which is strange.
How can 6695676.594495 when converted to decimal(38,10) give 6695676.5944950003?

Any idea how this strange calculation happens?

Regards,
Sunit.
Posted

1 solution

floating point values contains approximations of decimal numbers (values are store in binary form, using a finite number of bits, see "IEEE 754 Wikipedia page"[^] for detailed info).
You may verify that the following C++ program produces the same ouput:
C++
#include<iostream> 
#include <iomanip>
#include<cstdlib> 
using namespace std; 
int main()
{
  double a = 30452.8885;
  double b = 219.8700;
  double k = a * b;
  cout << setprecision(30) << k << endl;
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900