May 19, 2011 at 2:19 pm
I am performing a series of calculations where accuracy is very important, so have a quick question about single vs double precision variables in SQL 2008.
I'm assuming that there is an easy way to cast a variable that is currently stored as a FLOAT as a DOUBLE prior to these calculations for reduced rounding errors, but I can't seem to find it.
I've tried CAST and CONVERT, but get errors when I try to convert to DOUBLE.
For example...
SELECT CAST(1.0/7.0 AS FLOAT)
SELECT CONVERT(FLOAT, 1.0/7.0)
both give the same 6 decimal place approximation, and the 6 decimals make me think this is single precision.
But I get errors if I try to change the word FLOAT to DOUBLE in either one of those commands...
SELECT CAST(1.0/7.0 AS DOUBLE)
gives "Incorrect syntax near )"
SELECT CONVERT(DOUBLE, 1.0/7.0)
gives "Incorrect syntax near ,"
Any and all ideas are greatly appreciated!
May 19, 2011 at 11:21 pm
bkmooney (5/19/2011)
I am performing a series of calculations where accuracy is very important, so have a quick question about single vs double precision variables in SQL 2008.I'm assuming that there is an easy way to cast a variable that is currently stored as a FLOAT as a DOUBLE prior to these calculations for reduced rounding errors, but I can't seem to find it.
I've tried CAST and CONVERT, but get errors when I try to convert to DOUBLE.
For example...
SELECT CAST(1.0/7.0 AS FLOAT)
SELECT CONVERT(FLOAT, 1.0/7.0)
both give the same 6 decimal place approximation, and the 6 decimals make me think this is single precision.
But I get errors if I try to change the word FLOAT to DOUBLE in either one of those commands...
SELECT CAST(1.0/7.0 AS DOUBLE)
gives "Incorrect syntax near )"
SELECT CONVERT(DOUBLE, 1.0/7.0)
gives "Incorrect syntax near ,"
Any and all ideas are greatly appreciated!
Don't use float for this. Use the DECIMAL datatype, instead.
--Jeff Moden
Change is inevitable... Change for the better is not.
April 26, 2015 at 6:10 am
For those finding this post through a search engine, like I did...
A single-precision floating point number is represented as the REAL data type which can also be declared as FLOAT(24).
A double-precision floating point number is represented as the FLOAT data type which can also be declared as FLOAT(53) or DOUBLE PRECISION.
--this:
SELECT CAST(1.0/7.0 AS DOUBLE PRECISION);
-- is equivalent to this:
SELECT CAST(1.0/7.0 AS FLOAT);
-- which is equivalent to this:
SELECT CAST(1.0/7.0 AS FLOAT(53));
There are no special teachers of virtue, because virtue is taught by the whole community.
--Plato
Viewing 3 posts - 1 through 2 (of 2 total)
You must be logged in to reply to this topic. Login to reply