SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Power(n,n) result VS Windows Calculator
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

neo_bagsjol
Starting Member

6 Posts

Posted - 03/05/2014 :  21:56:52  Show Profile  Reply with Quote
Hello guys

i would like to ask why the result from Power function of sqlserver is different from the windows calculator for example

SQL RESULT

SELECT POWER('1.0116666666666667','120')

RESULT IS: 4.02247064115812

WINDOWS CALCULATOR
1.0116666666666667 ^ 120

RESULT IS:4.0224706411581114419518589580497

Is there a way to compute result like windows calc from sqlserver coz
i'm having different result when i run my script using Power function at the end of result i got -0.01 instead of 0.00

i need your help please i been doing this for 2 weeks still i got negative result

thanks guys

maunishq
Yak Posting Veteran

Canada
71 Posts

Posted - 03/06/2014 :  11:04:29  Show Profile  Reply with Quote
Can you edit your script? If yes, then limit the power portion to the same number of decimals SQL is showing. e.g CAST ... AS NUMERIC(20,14)

=======================
Not an Expert, Just a learner.
!_(M)_!

Edited by - maunishq on 03/06/2014 11:04:58
Go to Top of Page

Lamprey
Flowing Fount of Yak Knowledge

4614 Posts

Posted - 03/06/2014 :  11:26:39  Show Profile  Reply with Quote
I don't think there is much you can do other than write your own Power function. The built-in function uses a float. Since that is an approximate data type, you won't get very precise results.
Go to Top of Page

Lamprey
Flowing Fount of Yak Knowledge

4614 Posts

Posted - 03/06/2014 :  11:33:55  Show Profile  Reply with Quote
I was just curious so I wrote a quick loop to see what the results would be. Due to the way SQL Server handles Decimal/Numeric multiplication you need to set the scale really high to get a precise result:
DECLARE @Val DECIMAL(38, 37) = 1.0116666666666667;
DECLARE @Exponent INT = 120;
DECLARE @Result DECIMAL(38, 37);

WHILE @Exponent - 1 > 0
BEGIN
	SET	@Result = 
		CASE 
			WHEN @Result IS NULL THEN @Val * @Val
			ELSE @Result * @Val
		END
	SET @Exponent = @Exponent - 1
END

SELECT @Result

RESULT: 4.0224706411581114419518589580496877300
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.09 seconds. Powered By: Snitz Forums 2000