Showing posts with label float. Show all posts
Showing posts with label float. Show all posts

Monday, March 12, 2012

Problems with float results in calculation.

I have a stored procedure that uses the LOG10 function to calculate a logarithmic average. The first part of the calculation takes 1 divided by the number of results achieved per group. This always results in something like 0.2 or 0.3. Now, the problem is that SQL Server rounds off the result to the nearest 0, which always results in a result of 0. This then causes problems with the LOG10 function which results in a "Domain error" message. Anyone know of a way I can tell SQL to keep the value of 0.3 or 0.2 when it is calculated and not do a rounding? I've tried to convert the result of the calculation to float or real, but this did not help. Anyone know how to fix this?

Thanks,

Bcs you are doing integer division.

use the following sampel to fix yours..

Code Snippet

Select

1/Count(*) [1/Count(*)], --Wrong, Integer Division

1.0/Count(*) [1.0/Count(*)], --Partially Correct

1.0/Cast(Count(*) as Float) [1.0/Cast(Count(*) as Float)], --Perfect Expression

Log10(1.0/Cast(Count(*) as Float)) [Your Expression] --This is your answer

From

Sysobjects

|||Thanks a lot. It helped.

Friday, March 9, 2012

Problems with float results in calculation.

I have a stored procedure that uses the LOG10 function to calculate a logarithmic average. The first part of the calculation takes 1 divided by the number of results achieved per group. This always results in something like 0.2 or 0.3. Now, the problem is that SQL Server rounds off the result to the nearest 0, which always results in a result of 0. This then causes problems with the LOG10 function which results in a "Domain error" message. Anyone know of a way I can tell SQL to keep the value of 0.3 or 0.2 when it is calculated and not do a rounding? I've tried to convert the result of the calculation to float or real, but this did not help. Anyone know how to fix this?

Thanks,

Bcs you are doing integer division.

use the following sampel to fix yours..

Code Snippet

Select

1/Count(*) [1/Count(*)], --Wrong, Integer Division

1.0/Count(*) [1.0/Count(*)], --Partially Correct

1.0/Cast(Count(*) as Float) [1.0/Cast(Count(*) as Float)], --Perfect Expression

Log10(1.0/Cast(Count(*) as Float)) [Your Expression] --This is your answer

From

Sysobjects

|||Thanks a lot. It helped.

problems with float numbers

hi

im using a stored procedure to insert float numbers in a data base...the parameter i send to the stored procedure is a float but every time i use it, it inserts a number with a lot of extra numbers i mean if i try to insert 5.3 it inserts 5.30000019073486 any idea why this is happening?

thak you

Himarcelocr2,

Have you done any sort of calculation before add/update or during you retrieve the float value?

Anyway, try this before you update your float value:

Math.Round(yourFloatValue, numberOfDecimal)

Hope this is help

|||

What is the datatype of the column in the table that will have these values? If you only need 3 digits after the decimal use the decimal datatype and set the precision to 3. You can do this in the column properties under design view. Enterprise Mgr -> Right click on the table -> Design View.

|||

hi

thank you to both of you

i tried with the math.round, but it converts the numbers to integers, and i need the numbers to be float numbers...

i cant change the type in the sqlserver because i need it to be a float number

is there a way to show only 3 or 4 numbers after the ","?

thank you

|||

Hello,

You should let is save like that only and should use round(SQL function-round(fieldname,4)) in Stored Procedures where you retrive data.

|||

Hi,

The Float datatype in both SQL Server and .NET framework is a floating point value. It is not accurate but an approximate value. This is defined by way that float is stored in memory.

However, if you need an accurate value, try to use Decimal instead in both SQL Server and .NET framework.

HTH. If this does not answer your question, please feel free to mark the post as Not Answered and reply. Thank you!