Invalid floating point operation using SQRT() - sql-server

I receive an invalid floating point operation error when I SQRT(X) a value.
Under which reasons does this error occur, and what could I to to the X value to prevent this error?
For some context, the X value is a calculated average sales for stock items.
I have tried SQRT(ABS(x)) with no luck.
Thanks

As far as I know, the only case when SQRT(X) gives the error "An invalid floating point operation occurred" is when X is negative. However, you already fixed this by using the ABS function like this: SQRT(ABS(X)).
So, my guess is that the error does not really come from the SQRT function but from something else nearby. Let's look at the expression you gave:
SQRT(2*50*ABS(X)) / NULLIF(B*0.2/12,0))
This expression obviously has an extra right parenthesis. This makes me think that it is only a part of a larger expression, and that the larger expression is the reason for the error.
For instance, if B is 0 then the NULLIF() becomes NULL. You divide by NULL, thus getting a NULL result. Now, what do you do with this result? Maybe some more calculations that does not handle the NULL well?
There is a lot of guessing here. If my guesses did not point you in the right direction, then it would be helpful to know which values of B and X that give the error, and also the full statement that includes the expression.

There is a T_SQL command that calculates the type of the variables. You can view the type of variable before calculating its square root.
The command is :
SQL_VARIANT_PROPERTY
(See the site https://blog.sqlauthority.com/2013/12/15/sql-server-how-to-identify-datatypes-and-properties-of-variable/)
On the other hand, I'm sorry to tell you that I do not know if it works with the 2018 version, because I do not have the means to check it. But you can find the equivalent command for your SQL Server version.
Hope that can help.

Related

Not-safe expression in datalog

Why is this goal not considered safe?
MANAGER(Name) :- WORKER(Name, Age, _ ), ¬ SUBORDINATE (_, Name), Age <= 40
Our teacher says that it is because SUBORDINATE is negate, and so it can not have undefined (_) spaces, but it seems to be logic for me this expression.
Anyone that can help me?
The safety requirements in Datalog are intended to prevent infinite results. If you have a variable that occurs in the head and only negated in the body, then it can be bound to infinitely many values, which would obviously be a problem.
The specific requirements for safety are hard to precisely formulate, so usually you see the requirements simplified to 'every variable has to occur positively'. This is a bit more restrictive than needed.
The most informative answer to the question would be that the rule is technically unsafe, but that it does not have an infinite result. Some Datalog engines would allows this rule and return the finite result.
This rule is perfectly safe and it does not produce an infinite relation. It is an implementation deficiency of the Datalog engine you are using.
In general, an easy way to handle _ is to convert it into a fresh variable. This makes the implementation of the engine easy, but probably is the reason why this clause throws an error. If it was a variable, there would be an infinite number of values SUBORDINATE's first parameter cannot be.

Spring Expression (SpEL) - Rounding

Is there a way to specify rounding precision when evaluating math operation using SpEL?
For example
ExpressionParser parser = new SpelExpressionParser();
Expression exp = parser.parseExpression("2/3");
exp.getValue(Double.class); //returns 0.0 instead of 0.666666667
Or is this a limitation in SpEL?
Thanks
This works for me:
"2.0/3"
and result is 0.6666666666666666.
Does it make sense?
The point of the getValue(Double.class) is eliminated here since your 2/3 is just an operation between two integers and the result is an integer as well - 0.
And only after that this result is converted to expected double as a 0.0.
You need to explicitly say in the expression that you are going to deal with doubles.
You may consider this as a limitation, but getValue(Double.class) is not a casting operation like in Java. It is a post-conversion. Therefore precision is just lost because your expression evaluates to integer anyway.
This is for searchers as me - who googled "SpEL rounding precision". If you use it already in script/expression like {{car.price}} you can add .doubleValue(). So it will be {{car.price.doubleValue()}} and you can do also {{car.price.doubleValue()+conditioner.price.doubleValue()}}. It gives more precision..

Unwanted Real number generated

I have a weird problem if you can call it a problem that is.
Sorry in advance, the database is in french.
I have a table which hold the time a user passed on a specific task
I want to sum the time passed for every task
I'm able to get a sum from the database but the data is kind of wierd
The field is a real number to start with
Example, if I sum 0,35 + 0,63 + 1 I should get 1,98 Data without a sum:
But instead Access give me 1,97999998927116 Data with sum:
If I was to sum only integer the number would be correct
I know I could simply use a round function to get rid of it.
But I would like to know why it does this.
This is because Sum uses floating-point arithmetic if you execute it on a column that is defined as a Single or a Double
Floating-point arithmetic is often inaccurate.
You can avoid these kinds of errors by defining your column as a Decimal or as Currency

multiple checks in a 'if statement' in C language but not working

I have my code something like this.
int L=25;
float x;
Value to x is allotted by long calculation
if(x<=L)
x=x-L;
But it is not changing the value when x=L.
I have also tried
if(x>L || x==L)
Even in this case, value of x does not change for x=L.
Please help
Either x is slightly greater than 25 and you have been fooled into thinking it is exactly 25 by software that does not display the entire value correctly or the code being executed and the values being used differ from what you have shown in this question.
EDIT: Contrary to my initial view, and of some others, the issue isn't do with comparing different types. As per the comments, the most recent C standard that seems to be out there and free (http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf) makes it clear that comparison will force type conversion, generally towards the higher precision type.
AS an aside, in my personal view, it is still wiser to make these conversions explicit because then it is clear as you scan the code what is going on. The issue here is probably the one highlighted by another answerer.
It is quite possible the issue is with your typing. It is best to be explicit:
int L=25;
float x;
// Value to x is allotted by long calculation
if (x <= ((float)L)) {
x = x - ((float)L);
}

Exponential issue in postgresql

I'm facing an issue with the exponential function in postgresql. If I use this select statement select exp(5999), I'm getting:
ERROR: value out of range: overflow
SQL state: 22003
If I use this select statement select exp(5999.1), I'm getting the exponential result.
In other case if I use this statement select exp(9999.1), I'm getting the following error:
ERROR: argument for function "exp" too big
SQL state: 22003
Please let me know why this issue happening and what is the solution for this kind of issue?
I think your first problem is caused by the fact that the output type of exp() is the same as the input type. Because you're using an integral value, it's complaining that the result won't fit in an integer.
That's likely why exp(5999.1), and probably exp(5999.0) works, the floating point type has a larger range.
Your second error is slightly different, it's complaining not of overflow during the calculation, but of the fact the input argument is too large. It's possible that it has some sanity checking on the input somewhere.
Even floating point values run out of range eventually. e9999 is somewhere around 104300, a rather large number and probably well beyond what you'd expect to see in a database application.
In fact, I'd be interested in the use case of such large numbers in a database application. This sounds like something better suited to a bignum package like MPIR.
If you pass an INTEGER argument the exp() function will try to return double precision value. Just above value n=709 it will reach the limit of 64-bit floating point number (about 10^308) and fail to calculate the e^n. The solution is to pass your argument with NUMERIC type:
SELECT EXP(710); -- failure!
SELECT EXP(710::NUMERIC); -- OK
SELECT EXP(5999.1::NUMERIC); -- huge but OK
EDIT!
As for the ERROR: argument for function "exp" too big SQL state: 22003. I've tried to write a work-around. Just run this:
SELECT n, POWER(EXP(1::NUMERIC), n) FROM (VALUES(9998), (9999), (10000)) AS foo (n)
and it will work. But then change 9999 to 9999.1 and you will get that stupid error again. This is ridiculous! 9999.1 is too big but 10000 is fine :D It looks like Postgres doesn't like decimal point in POWER()'s argument. Sorry but I can't fix that.
One solution would be to use the arithmetic property of power and write POWER(POWER(EXP(1::NUMERIC), n*10), 0.1) but that value combination is still too big for Postgres' implementation of power. Good luck with your battle.

Resources