TSQL - make a literal float value

空扰寡人 提交于 2020-07-08 08:07:50

问题


I understand the host of issues in comparing floats, and lament their use in this case - but I'm not the table author and have only a small hurdle to climb...

Someone has decided to use floats as you'd expect GUIDs to be used. I need to retrieve all the records with a specific float value.

sp_help MyTable

-- Column_name  Type    Computed    Length  Prec
-- RandomGrouping   float   no  8   53   

Here's my naive attempt:

--yields no results
SELECT RandomGrouping
FROM MyTable
WHERE RandomGrouping = 0.867153569942739

And here's an approximately working attempt:

--yields 2 records
SELECT RandomGrouping
FROM MyTable
WHERE RandomGrouping BETWEEN 0.867153569942739 - 0.00000001
      AND 0.867153569942739 + 0.00000001

--  0.867153569942739
--  0.867153569942739

In my naive attempt, is that literal a floating point literal? Or is it really a decimal literal that gets converted later?

If my literal is not a floating point literal, what is the syntax for making a floating point literal?

EDIT: Another possibility has occurred to me... it may be that a more precise number than is displayed is stored in this column. It may be impossible to create a literal that represents this number. I will accept answers that demonstrate that this is the case.


EDIT: response to DVK.

TSQL is MSSQLServer's dialect of SQL.

This script works, and so equality can be performed deterministically between float types:

DECLARE @X float
SELECT top 1 @X = RandomGrouping
FROM MyTable
WHERE RandomGrouping BETWEEN 0.839110948199148 - 0.000000000001
  AND 0.839110948199148 + 0.000000000001
  --yields two records
SELECT *
FROM MyTable
WHERE RandomGrouping = @X

I said "approximately" because that method tests for a range. With that method I could get values that are not equal to my intended value.

The linked article doesn't apply because I'm not (intentionally) trying to straddle the world boundaries between decimal and float. I'm trying to work with only floats. This isn't about the non-convertibility of decimals to floats.


Response to Zinglon:

A literal value that can find my records, thanks.

DECLARE @Y binary(8)
SET @Y = 0x3FEAD9FF34076378

SELECT *
FROM MyTable
WHERE convert(binary(8), RandomGrouping) = @Y

回答1:


It is possible that the values are being truncated on display. I'm assuming the column doesn't have a unique constraint on it, otherwise the question would be moot. On my setup, SSMS truncates the more precise value in this script.

create table flt ( f float not null primary key )
insert into flt
select 0.111111111111111
union all
select 0.1111111111111111
select f, cast(f as binary(8)) from flt

Similarly, if these values are distinct you can cast them to binary(8) and identify them based on that value, like this:

select f from flt
where cast(f as binary(8)) = 0x3FBC71C71C71C71C



回答2:


is that literal a floating point literal? Or is it really a decimal literal that gets converted later?

If my literal is not a floating point literal, what is the syntax for making a floating point literal?

The 0.867153569942739 literal in SQL Server is a decimal type, not float. The engine automatically picks appropriate scale and precision to represent the given literal.

To write a literal of the float type you should use the scientific notation, like this: 0.867153569942739E0

This is documented in Constants (Transact-SQL)

decimal constants

decimal constants are represented by a string of numbers that are not enclosed in quotation marks and contain a decimal point. The following are examples of decimal constants:

1894.1204  
2.0  

float and real constants

float and real constants are represented by using scientific notation. The following are examples of float or real values:

101.5E5  
0.5E-2

The sp_describe_first_result_set can tell us the types of columns

EXEC sp_describe_first_result_set N'SELECT 0.867153569942739, 0.867153569942739E0'

It returns numeric(15,15) for the first column and float for the second.


If your column RandomGrouping is indexed, it is much more efficient to use a float literal, because when you wrap RandomGrouping in convert(), an index can't be used.

The following query will use an index:

SELECT *
FROM MyTable
WHERE RandomGrouping = 0.867153569942739E0

The following query will not use an index:

SELECT *
FROM MyTable
WHERE convert(binary(8), RandomGrouping) = @Y



回答3:


The problem is not whether it's a floating point literal or not.

The problem is that comparing two floats for equality in Sybase (or any DB server) is not deterministic, since 4.00000000000000000000... and 3.99999999999999999999... are the same exact number but aren't equal.

Your second solution is the only correct way to compare floats for "equality" (that is, are they the same up to a precision).

Why are you saying "approximately working" about your second approach?

Since you didn't provide the specific DB server you use, here's a fairly decent write-up of the problem (with basically the same conclusions as above) for MySQL

http://dev.mysql.com/doc/refman/5.0/en/problems-with-float.html



来源:https://stackoverflow.com/questions/3024306/tsql-make-a-literal-float-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!