Double or float - make your mind up!

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I use floats in SQL server as they are its standard datatype for dealing with non-integral numbers
I thus thought it would be logical to use the SqlDbType.Float for accessing parameters of this type in ADO.NET, from C#
However, when I declare an SqlParameter with SqlDbType.Float as its datatype, its Value property after the command has executed can't be cast to a float, only a double. I'm OK with this if this is what the DB engine wants to do - but is it really correct?????? I know I could cast to a float but would there be much point if I'm only doing comparisons, not much maths. I'm mainly after fast performance

Thanks!
 
B0nj:

I'm not sure what the problem is. I have a Float field in SQL Server and
for instance, the field is field2 the float, I have no problem conversion to
Double:

Private Sub btn_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btn.Click
ds = New DataSet
SqlDataAdapter1.Fill(ds, "MyTestTable")
dg.DataSource = ds.Tables(0)
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim d As Double = CType(ds.Tables(0).Rows(1)(2), Double)
MessageBox.Show(d.ToString)
End Sub

Am I undestanding the problem correctly?
B0nj said:
I use floats in SQL server as they are its standard datatype for dealing with non-integral numbers.
I thus thought it would be logical to use the SqlDbType.Float for
accessing parameters of this type in ADO.NET, from C#.
However, when I declare an SqlParameter with SqlDbType.Float as its
datatype, its Value property after the command has executed can't be cast to
a float, only a double. I'm OK with this if this is what the DB engine wants
to do - but is it really correct?????? I know I could cast to a float but
would there be much point if I'm only doing comparisons, not much maths. I'm
mainly after fast performance.
 
No - you're not really understanding the problem - there ISN'T a *problem* as such, more of a curiosity
It's just a question. I'm not having a problem getting my code to do what I want

I've got a value that's defined as "float" in SQL server, and the only thing it will directly cast to in C# (coming off an SqlParameter.Value property) is "double". I *could* then cast it to a float if I wanted to, but like I say I don't need to. I just WONDERED why this was the case, it's not particularly important, just curious. Is it that "double" in .NET is the equivalent of "float" in SQL server maybe? If so, what's SQL server's equivalent of .NET's "float" ??

Thanks!
 
The basic gist is the C# double and SQL float (at least as default) match in
size.

LONG ANSWER:
-------------------
Other than naming convention, I am not sure I see the problem.

Float (SQL Server) : - 1.79E + 308 through 1.79E + 308
Double (C#) : +/- 5.0 × 10E-324 to +/- 1.7 × 10E308

In default mode, both are equivalent. For huge precision, you will end up
having to watch your insert, but I seriously doubt you are going to end up
on the extreme end with most application. If the default were float (SQL)
and float (C#), you would have to do a lot of checking when pulling numbers
from SQL Server, as C# floats are:

+/- 1.5 × 10E?45 to +/- 3.4 × 10E38

Microsoft has matched the sizes of the SQL default float to its equivalent
C# default, which is double. I will admit it is confusing, as you would
think a default float would be the same in both. Unfortunately, .NET is a
conglomeration of languages, each of which had to make some compromises to
keep consistency across languages. SQL Server was originally a co-project
(Sybase and Microsoft). Going back and changing types radically in either
system (primarily VB and SQL Server) would create too much confusion (look
at what happened when the VB Integer was increased). Since C# and VB share
(mostly) the same exact type names, it gets more understandable. The SQL and
Visual Tools teams work together on some things (Whidbey/Yukon comes to
mind), but they are still autonimous.

Wow, too much information.



--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

**********************************************************************
Think Outside the Box!
**********************************************************************
B0nj said:
I use floats in SQL server as they are its standard datatype for dealing with non-integral numbers.
I thus thought it would be logical to use the SqlDbType.Float for
accessing parameters of this type in ADO.NET, from C#.
However, when I declare an SqlParameter with SqlDbType.Float as its
datatype, its Value property after the command has executed can't be cast to
a float, only a double. I'm OK with this if this is what the DB engine wants
to do - but is it really correct?????? I know I could cast to a float but
would there be much point if I'm only doing comparisons, not much maths. I'm
mainly after fast performance.
 
SQL Float maps to Double
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/
frlrfsystemdatasqldbtypeclasstopic.asp Here's a conversion list. SQL
SErver's eqivalent of Float is double in .NET. The conversion sheet above
will help explain this.. but float in .NET doesn't map back directly to a
SQLDBType. All SqlDbTypes map to a .NET type, but not all .NET types map
back to A SqlDbType
B0nj said:
No - you're not really understanding the problem - there ISN'T a *problem* as such, more of a curiosity.
It's just a question. I'm not having a problem getting my code to do what I want.

I've got a value that's defined as "float" in SQL server, and the only
thing it will directly cast to in C# (coming off an SqlParameter.Value
property) is "double". I *could* then cast it to a float if I wanted to, but
like I say I don't need to. I just WONDERED why this was the case, it's not
particularly important, just curious. Is it that "double" in .NET is the
equivalent of "float" in SQL server maybe? If so, what's SQL server's
equivalent of .NET's "float" ???
 
Back
Top