When I tried to update some tables value I got this error.
I have table with column some float values
Values
---------------------
11.7830808596997
-9.41220524769667
1.70771403155729
-26.1598813945345
1.40722323420305
0.315202798180501
-3.27870943910565
4.49832736699458
-21.8406197004573
12.6569938818624
10.3327211608816
-14.9366297400332
-1.96665717283736
5.90430556370099
1.59122918690946
1.01784176743728
-41.3800628432377
Now I want to change to power of 0.3333 with all that value.
So I write a statement
update table
set value = power(value, 0.3333)
But I get an error
Msg 3623
An invalid floating point operation occurred
Can you suggest how I can fix that? I want to do something like
(1 + value) ^ 1/3 - 1
in T-SQL
I think this is the logic you want:
select sign(val) * exp(log(abs(val)) / 3)
from (select -8 as val) t
The issue is that you cannot raise negative numbers to fractional powers because the result is generally an imaginary number, not a floating point number. This simple converts the value to a positive number and remembers the sign.
You could also use:
select sign(val) * power(abs(val), 1.0 / 3)
Here is a db<>fiddle.
Related
I'm trying to depreciate the value of an item in a straight line over time
If I use itemValue2 = itemvalue1 - itemvalue1*item-Age/item-Life, It works perfectly BUT when I originally
used itemValue2 = itemvalue1 * ( 1 - item-Age/item-Life) it constantly evaluated to itemValue1 .
Why would this be ? is it the type of the number 1 ?
itemValue1 and itemValue2 are Doubles, item-Age and item-Life are Longs
Or is it some maths rule that I haven't understood?
Like mentioned in the comments, Longs that divide will result in a truncated Long value. If your resulting value would've been been less than 1, then the result is a truncated value of 0. That leaves itemValue1 to be multiplied by 1, which is itself. You will have to cast to number types that use decimals without truncating like Double or Float.
This thing called Mathematical precedence in kotlin, mean when you create operation like this 1+2*3 the expected result is 9 but not, 7 is the result, in kotlin be the priority for the multiply's * and the divide's / then came the plus + and minus -.
To resolve this issue you need to add the brackets () it's had priority before any operation symbol in the language.
And the operation will be (1+2)*3it will gave result 9.
I get the following exception while excecuting this sql command:
SELECT (ACOS(SIN(PI()*51.0026829600855/180.0)*SIN(PI()*51.0026829600855/180.0)+COS(PI()*51.0026829600855/180.0)*COS(PI()*51.0026829600855/180.0)*COS(PI()*13.7910680057092/180.0-PI()*13.7910680057092/180.0))*6371) AS foo
System.Data.Entity.Core.EntityCommandExecutionException
An invalid floating point operation occurred”
How do I have to change the query to make it run? My approach was
ROUND( statement, 2)
but it doesn't work.
It's a location search by longitude and latitude
Update: doesn't work with SQL Server 2017 aswell http://sqlfiddle.com/#!18/9eecb/38598
Round the statement inside ACOS() first
SELECT (ACOS( ROUND(SIN(PI()*51.0026829600855/180.0)*SIN(PI()*51.0026829600855/180.0)+COS(PI()*51.0026829600855/180.0)*COS(PI()*51.0026829600855/180.0)*COS(PI()*13.7910680057092/180.0-PI()*13.7910680057092/180.0), 15))*6371) AS foo
it's because of floating conversation in sql. without rounding, sql server assumes that the input value a little bit bigger than 1, you can round the input, or subtract a little value like 0.00000000000001 before passing it to acos()
The result for this query is 6371:
SELECT (
SIN(PI()*51.0026829600855/180.0)*SIN(PI()*51.0026829600855/180.0)+COS(PI()
*51.0026829600855/180.0)*COS(PI()*51.0026829600855/180.0)*COS(PI()
*13.7910680057092/180.0-PI()*13.7910680057092/180.0)
)*6371
AS foo
However, 6371 is not eligible for ACOS. ACOS shoulde be lower or equal to 1.
For example:
SELECT ACOS(1) -- OK
SELECT ACOS(1.1) -- An invalid floating point operation occurred.
UPDATE:
This query could return a value which is greater than 1. So try to use CASE statement:
SELECT
(
ACOS
(
CASE WHEN
(SIN(PI()*51.0026829600855/180.0)*SIN(PI()*51.0026829600855/180.0)
+COS(PI()*51.0026829600855/180.0)*COS(PI()*51.0026829600855/180.0)
*COS(PI()*13.7910680057092/180.0-PI()*13.7910680057092/180.0)) > 1
THEN 1
ELSE
(SIN(PI()*51.0026829600855/180.0)*SIN(PI()*51.0026829600855/180.0)
+COS(PI()*51.0026829600855/180.0)*COS(PI()*51.0026829600855/180.0)
*COS(PI()*13.7910680057092/180.0-PI()*13.7910680057092/180.0))
END
)
*6371) AS foo
SELECT CAST ((SUM(r.SalesVolume)/1000) AS decimal(3,3)) FROM RawData r
The above is a part of a query that I am trying to run but returns an error:
Lookup Error - SQL Server Database Error: Arithmetic overflow error converting int to data type numeric.
Not sure what this means.
The result column looks like(Without dividing by 1000 and casting):
Total_Sales_Volume
64146
69814
68259
56318
66585
51158
44365
49855
49553
88998
102739
55713
Tried casting as float but doesnt help.
The Problem is decimal(3,3) --> this means a number with 3 digit, 3 of them behind the decimal point. If you want a number like this 1234567.123 you would have do declare it as decimal(10,3)
Try this:
SELECT CAST ((SUM(r.SalesVolume)/1000.0) AS decimal(6,3)) FROM RawData r
decimal(3,3) means that you allow numbers with 3 digits in total, and 3 of these are behind the comma ... I think you meant decimal(6,3)
EDIT: In addition, you need to to divide by 1000.0, not by 1000.
If you divide by 1000, it is an integer division.
If you divide by 1000.0, then it becomes a decimal division, with commas.
Try following:
SELECT CAST ((SUM(r.SalesVolume)/1000) AS numeric(6,3)) FROM RawData r
I'm getting the error message
Msg 3623, Level 16, State 1, Line 25
An invalid floating point operation occurred.
From SQL Server 11.0.2100 (on an AWS RDS instance) for the following query:
SELECT SUM(LOG(col + 1)) FROM MyTable;
All values for col are within the range -0.1 and 0.1.
If I remove the SUM from the query, it runs fine, which implies the inputs to LOG are correct:
SELECT LOG(col + 1) FROM MyTable;
Also, if I select the above into a temp table, then SUM over the values in the temp table, that also works fine, which implies there's nothing wrong with the SUM:
SELECT LOG(col + 1) thelog INTO #x FROM MyTable;
SELECT SUM(thelog) FROM #x;
DROP TABLE #x;
It's only when I run SUM and LOG together that I get an issue.
Why is this happening? Is it possible that SQL Server is somehow rearranging the +1 to be outside of the LOG, so that the LOG input is out of range?
Note: Putting a CASE WHEN col > -1 inside the query also fixes it, but this shouldn't be required since all values for col + 1 are within range. I'm wondering what the underlying reason for this might be...
Floating point values are notorious for rounding issues because they are stored as binary fractions which often don't have an exact decimal equivalent. I think what's happening here is that one of your values is extremely close to -1 (e.g. -0.99999999999999999999), and when you add 1 it could sometimes be rounded to 0. The LOG function is undefined for an input of 0, so the result is An invalid floating point operation occurred.
SELECT LOG(0) -> An invalid floating point operation occurred.
You should handle the case for the input of LOG function.
As LOG(0) is undefined.
Using SQL Server 2008 R2 when I enter the following query:
SELECT CAST(POWER(2.0, 63.0) AS BIGINT);
Which yields the result:
9223372036854775800
However, using the Windows desktop calculator and raising 2 to the 63 yields:
9223372036854775807
Can someone please explain the difference -- or is there some internal conversion that SQL Server is doing? ... or am I missing something else?
The range of BIGINTin MS Sql Server is:
-2^63 (-9,223,372,036,854,775,808) to 2^63-1 (9,223,372,036,854,775,807)
And your calculator is giving you the wrong number, because 2^63 can't have an odd number for its right-most digit.
The POWER function in SQL Server (http://technet.microsoft.com/en-us/library/ms174276.aspx), returns the same type as its first argument.
The correct way to write this query is:
DECLARE #foo REAL = 2.0
SELECT CAST(POWER( #foo, 63.0 ) AS BIGINT)
By which, you will get Arithmetic overflow error converting expression to data type bigint. error message.
And about the reason that's
http://www.extremeoptimization.com/resources/Articles/FPDotNetConceptsAndFormats.aspx
And regarding the question of why POWER function is returning a wrong number? As #simonatrcl mentioned in his answer, there is arithmetic problems with floating-point numbers which sometimes result in invalid result. You can read about floating-point numbers and the problems with them here:
http://www.extremeoptimization.com/resources/Articles/FPDotNetConceptsAndFormats.aspx
You can also check the boundaries for integer types in MS Sql Server here:
http://technet.microsoft.com/en-us/library/ms187745.aspx
Power will be returning a FLOAT. Floating point numbers are not accurate beyond certain limits, and will drop a bit of accuracy (if you've ever has a negative 0 problem you'll know what I mean!).
That's what you're getting here...
As far as the calculator goes and tested on XP, Win7 and Win8.1:
2^63 = 9223372036854775808 (obviously)
As far as MSSQL goes:
The upper limit of a BIGINT is defined as 2^63-1, meaning 1 less than 2^63
Now if you would like MSSQL to calculate that for you one would be tempted to write something like:
SELECT POWER(CAST(2 AS BIGINT), 63) - 1
The result would be a bigint because you've cast the first argument of the power to a bigint. MSSQL will first calculate the power and then subtract 1. However, since the result of the power would exceed the range of a bigint, this statement will fail: Arithmetic overflow error converting expression to data type bigint.
So let us invoke some math to solve this. I assume everyone agrees with
2^4 = 2 * 2 * 2 * 2 = 2 * (2^3) = 2^3 + 2^3
and thus
2^4-1 = 2 * 2 * 2 * 2 - 1 = 2 * (2^3) - 1 = 2^3 + 2^3 - 1
That's what we're going to make use of...
SELECT POWER(CAST(2 AS BIGINT), 62) + (POWER(CAST(2 AS BIGINT), 62) - 1)
This results in 9223372036854775807 which is indeed the upper limit of a bigint.
Note that the () around the subtraction is really needed. Otherwise the addition of the result of the two powers would be done first, again resulting in an overflow.