1

The MSDN documentation at POWER, indicates that the first parameter past to it, is, or should be, convertible to a float

 POWER ( float_expression , y )

but when you Run

 `Select power(19.8, 2) withoutCast, 
         power(Cast(19.8 as float), 2) withCast`

you get:

withoutCast     withCast
------------    ----------------------
392.0           392.04

The correct value of course, should be 392.04.
The function returns the wrong answer when the input parameter is not pre-cast to a float.

This apparently is documented by the statement in the MSDN docs that:

"Returns the same type as submitted in float_expression. For example, if a decimal(2,0) is submitted as float_expression, the result returned is decimal(2,0)."

However, if I submit 19.8 as the first expression, why is the function assuming it is a decimal(19,0) and truncating the fractional portion of the answer????

4

2 回答 2

3

Why does the function return a wrong answer when the input parameter is not pre-cast to a float?

No, the function is returning a value with the same number of decimals as the parameter unless you convert to a float. Try this and note the decimals in the return:

select power(2.3, 2), power(2.33, 2), power(2.333, 2)

You may be missing the concept of Significant figures in terms of what is being passed back. If you pass something in with an accuracy to 1 decimal value to a function, shouldn't it return back to that accuracy?


To take your example a bit further, try this:

Select power(19.8, 6) withoutCast, power(Cast(19.8 as float), 6) withCast

Here are the results:

withoutCast     WithCast
60254729.6      60254729.561664

Now, do you really think that value would be accurate to 6 decimal places given that I just raised it to the 6th power? This is part of the context you are missing is how far can I go and still have accuracy with my results as some may think it is accurate to the 5th or 6th decimal which may not be true if you are extrapolating your data.

于 2013-07-18T16:42:42.167 回答
0

It's returning the same precision as it was passed...

Select power(19.8, 2),power(19.80, 2),power(Cast(19.8 as float), 2) 

returns this:

392.0    392.04    392.04

casting to float is safest way to go.

于 2013-07-18T16:49:51.827 回答