Click here to Skip to main content
15,914,481 members
Home / Discussions / Database
   

Database

 
QuestionDTS Query Pin
A.Muthunagai30-Jul-07 1:20
A.Muthunagai30-Jul-07 1:20 
AnswerRe: DTS Query Pin
Rob Graham30-Jul-07 4:08
Rob Graham30-Jul-07 4:08 
AnswerAny sample for DTS? Pin
A.Muthunagai30-Jul-07 23:10
A.Muthunagai30-Jul-07 23:10 
QuestionIndexing SQL 2005 Database!!! Pin
kibromg30-Jul-07 0:37
kibromg30-Jul-07 0:37 
AnswerRe: Indexing SQL 2005 Database!!! Pin
Rob Graham30-Jul-07 4:15
Rob Graham30-Jul-07 4:15 
QuestionSQL Float Rounding... or something :-) Pin
MatthysDT30-Jul-07 0:07
MatthysDT30-Jul-07 0:07 
AnswerRe: SQL Float Rounding... or something :-) Pin
Krish - KP30-Jul-07 0:12
Krish - KP30-Jul-07 0:12 
AnswerRe: SQL Float Rounding... or something :-) Pin
Mike Dimmick30-Jul-07 12:05
Mike Dimmick30-Jul-07 12:05 
Ah, time to post What Every Computer Scientist Should Know About Floating Point[^] again.

The value 2.78 cannot be stored exactly in a 32-bit floating point field. The closest approximation has the bit pattern 40 31 EB 85 (in hex), which is 1.011 0001 1110 1011 1000 0101 left-shifted by 1, i.e. 10.11 0001 1110 1011 1000 0101. (See IEEE 754[^] on Wikipedia.)

Converting to decimal you get:
1 * 2 ^  1 = 2 +                2
0 * 2 ^  0 
1 * 2 ^ -1 = 0.5 +              2.5
1 * 2 ^ -2 = 0.25 +             2.75
0 * 2 ^ -3
0 * 2 ^ -4
0 * 2 ^ -5
1 * 2 ^ -6 = 0.015625           2.765625
1 * 2 ^ -7 = 0.0078125          2.7734375
1 * 2 ^ -8 = 0.00390625         2.77734375
1 * 2 ^ -9 = 0.001953125        2.779296875
You can see that the approximation is getting closer to 2.78, but the exact value is not possible as 0.03 (the residual after the first four bits) is not representable as a sum of negative powers of two. In the same way 1/3 is not representable exactly as a sum of negative powers of 10, or a decimal as we normally call it - the closest we can get is 0.33333 (and the 3s continue infinitely).

You can also see by the fact that the approximation is getting longer in decimal digits that a complete representation of the binary value will take a lot of space. I believe that a binary floating point value is always fully representable as a decimal (two dividing evenly into ten) but the space requirement may be unacceptable. Therefore, the value is almost always shown rounded to a certain number of places.

SQL Server does not have particularly good display-control features. That's not what it's for - it's for retrieving data efficiently. Instead you should use the typed-dataset mechanisms of whatever your client programming language or environment is - for example, the SqlDataReader in .NET - to retrieve the data without interpretation, then format the result using the programming language/environment's features (e.g. String.Format).

If you want to preserve the decimals accurately, you should use one of the scaled integer data types (e.g. decimal which is a synonym for numeric). These simply record the value without the decimal point, and a decimal exponent (e.g. here it would record 278 with an exponent of -2: 2.78 = 278 * 10 ^ -2).

Stability. What an interesting concept. -- Chris Maunder

GeneralRe: SQL Float Rounding... or something :-) Pin
MatthysDT30-Jul-07 21:36
MatthysDT30-Jul-07 21:36 
GeneralRe: SQL Float Rounding... or something :-) Pin
Colin Angus Mackay30-Jul-07 22:45
Colin Angus Mackay30-Jul-07 22:45 
GeneralRe: SQL Float Rounding... or something :-) Pin
Pete O'Hanlon31-Jul-07 10:16
mvePete O'Hanlon31-Jul-07 10:16 
QuestionQuery Of This Request? Pin
mehrdadc4829-Jul-07 23:12
mehrdadc4829-Jul-07 23:12 
AnswerRe: Query Of This Request? Pin
gauthee29-Jul-07 23:17
gauthee29-Jul-07 23:17 
AnswerRe: Query Of This Request? Pin
Sam Heller30-Jul-07 12:03
Sam Heller30-Jul-07 12:03 
QuestionRanks in FUll Text Search Pin
Brendan Vogt29-Jul-07 21:58
Brendan Vogt29-Jul-07 21:58 
Question(Urgent Need Plz hlp me)Group by & order by in Stored procedure..., [modified] Pin
Member 387988129-Jul-07 18:48
Member 387988129-Jul-07 18:48 
AnswerRe: (Urgent Need Plz hlp me)Group by & order by in Stored procedure..., Pin
gauthee29-Jul-07 21:12
gauthee29-Jul-07 21:12 
GeneralRe: (Urgent Need Plz hlp me)Group by & order by in Stored procedure..., Pin
Member 387988129-Jul-07 22:01
Member 387988129-Jul-07 22:01 
GeneralRe: (Urgent Need Plz hlp me)Group by & order by in Stored procedure..., Pin
gauthee29-Jul-07 23:01
gauthee29-Jul-07 23:01 
GeneralRe: (Urgent Need Plz hlp me)Group by & order by in Stored procedure..., Pin
Member 387988129-Jul-07 23:18
Member 387988129-Jul-07 23:18 
GeneralRe: (Urgent Need Plz hlp me)Group by & order by in Stored procedure..., Pin
gauthee29-Jul-07 23:23
gauthee29-Jul-07 23:23 
GeneralRe: (Urgent Need Plz hlp me)Group by & order by in Stored procedure..., Pin
Member 387988130-Jul-07 3:16
Member 387988130-Jul-07 3:16 
AnswerStop with the 'urgent' Pin
leckey30-Jul-07 3:22
leckey30-Jul-07 3:22 
QuestionSQL Server Licesning Pin
thedom229-Jul-07 15:29
thedom229-Jul-07 15:29 
AnswerRe: SQL Server Licesning Pin
Paul Conrad29-Jul-07 19:47
professionalPaul Conrad29-Jul-07 19:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.