I would like to know if there are any differences in between the assignment operators ,= and := in Oracle plsql.
some times i got results faster from only equal sign rather than using :=. Are there cases where they can give different results or different performance?
:= assignment operator
= relational operator
Source: http://docs.oracle.com/cd/B10501_01/appdev.920/a96624/02_funds.htm
Example:
1) IF a = b THEN
2) l_variable NUMBER := 10;
The delimiters are used in a different way, you cannot replace one with the other.
Related
I want to realize this function:
Do[
expr(1),
expr(2)...
expr(n),
{i,1,j}]
to execute expr(k), the result of expr(k-1) is required, so the function can not realized by simply multiple layer of do loop. How can I execute the function by do loop? Or by other loop in mathematica?(I also notice that both for and while loop can only support one expr just as do loop)
Try separating by semicolon rather than comma. Like:
y = 0
Do[y += x; Print[y], {x, 1, 5}]
Do loops, and many of the other control-flow constructs from imperative programming languages, are almost always not the right answer to Mathematica programming questions.
You don't tell us what expr is supposed to calculate so it's difficult to provide more than a very general answer ... so I'll use the factorial as a simple example of how one might program a function where expr[n] depends on expr[n-1]
fact[0] = 1
fact[n_] := n*fact[n-1]
Here, I've defined the factorial function in two rules; the first establishes the base case, and the second establishes the case for values other than 0. To avoid situations where the function is fed bad data we'd probably prefer a formulation such as
fact[0] = 1
fact[n_Integer /; n > 0] := n*fact[n-1]
In this version the function fact will only operate on positive integers or on 0.
(Note: to those knowledgeable about Mathematica: Yes I know that this is not a good way to program the factorial function, and that there is a built-in function for calculating factorials. But this is supposed to help someone who appears to be a complete novice.)
I have written some code which assigns variables using the results of condition expressions without the explicit use of IF-ELSE statements.
In the simplest form, the problem looks like this:
Version 1
if (x < K)
y = A;
else
y = B;
I've seen a "trick" in the past in which people accomplish the same task in one line without the conditional like this:
Version 2
y = (x < K) * A + !(x < K) * B;
This approach extends relatively easily to handle IF-ELSE IF-ELSE assignments. The trick is to ensure that the conditions are all mutually exclusive.
From a unit testing perspective, I'm required to achieve 100% code path coverage.
My coworkers agree that the Version 2 is more elegant, but they contend it is less readable. Furthermore, they argue that I am "side-stepping" the path coverage requirement and that I would be able to achieve 100% path coverage by "hiding" the conditional logic inside the single line of code without actually exercising both conditions ((x < K) and !(x < K)).
I argue that I am able to blend Boolean algebra and numeric algebra to perform variable assignment because the computer treats Boolean 'true' and 'false' as '1' and '0' which can be multiplied by 'float' and 'int' variables. To me, it becomes simply an arithmetic expression with zeros and ones multiplying variables.
Why am I doing this?
I am doing this blend of Boolean and numeric algebra to minimize the number of IF-statements, minimize lines of code, and general code cleanup. Obviously performance can be improved by saving the result of the condition to a variable and referencing.
The Question
Is this practice (and ternary operators) frowned upon from a unit testing perspective?
If this question is too subjective, please suggest edits.
I'd suggest avoiding it (this trick is actually useful when the intention is to avoid branching, which may be the context you've seen it in). Given that the language doesn't have a conditional operator, you should be able to define the equivalent of
cond(bool, x, y) { if (bool) return x; else return y; }
yourself and write y = cond(x < K, A, B). It's more readable, harder to make a mistake when writing, is usable with non-number types, and is considered correctly in path coverage. It evaluates both sides, unlike the actual conditional operator (unless the language has macros or lazy evaluation), but so does the described trick.
I'm working with process scheduling software called Appworx. In it, each process and subprocess can have an arbitrary number of "conditions", which if true, some conditional action is taken.
One of the possible conditional actions is a goto statement, where a plain integer is the label (each condition being numbered starting at 1). I'd like to use this feature to evaluate and run a few tasks in a loop, but you can only goto higher-numbered conditions (Don't ask me why... this seems to ruin most of the utility).
I have reason to believe that all of this is evaluated by Oracle on the backend. And having looked at the schema for Appworx, it appears that the goto labels are all NUMBER(12,0). I suspect that the logic that checks whether a label is lower than the current condition is something like:
where label > current_condition
So, if I were to supply a goto with a high enough value, I think it would cheat the checking and allow me to do simple loops. At least if Oracle used normal integers. Is it possible to overflow them, and what value would I use to overflow the value back to 1?
I suppose the Oracle version matters quite a bit, if so, it's 11g.
PS Also, if anyone would care to re-tag this for me, please add "appworx"
Oracle numbers are in fact floating point numbers with 40 decimal digit significand.
So, they can't be overflowed.
(10^40-1) is the maximal integer number which can be increased by 1.
proof
NUMBER(12,0) is a subtype of type NUMBER.
That is, it consists of NUMBER type and a restriction checker.
Well, that depends on your definition of 'overflow'. If you define 'overflow' as 'find a value n where n + 1 < n', then no, there's no such value. If you define 'overflow' as 'raises an exception', then yes, it's quite possible to perform an operation on a NUMBER(12,0) where an exception is raised.
Run the following:
DECLARE
n NUMBER(12, 0);
BEGIN
n := 999999999999; -- Twelve 9's
DBMS_OUTPUT.PUT_LINE('1 : n=' || n);
n := n + 1;
DBMS_OUTPUT.PUT_LINE('2 : n=' || n);
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Exception: ' || SQLCODE || ' ' || SQLERRM);
END;
As you can see, the following exception is thrown when attempting to execute "n := n + 1":
ORA-06502: PL/SQL: numeric or value error: number precision too large
So it's quite possible to overflow a subtype of NUMBER. However, given that you're hoping to find a value n where n + 1 < n, I think you're out of luck.
And if you really want to provoke this behavior using the base NUMBER type, just execute
n := POWER(10, 126);
Of course, for truly nasty behavior in NUMBER you need to get it to produce a NaN (Not a Number):
n := 9999999999999999999999999999999999999999 * POWER(10, 125);
DBMS_OUTPUT.PUT_LINE('n=' || n);
produces
n=~
WTF?!? '~'? What the heck is '~'? Well, it appears that this is Oracle's way of printing a NaN. And the really fun part? Once you've got a NaN in a variable, any operation you perform on that variable will produce another NaN. Quietly. Silently. Without warning. Without recourse. Try:
DBMS_OUTPUT.PUT_LINE('n * 1234=' || n * 1234); -- produces n * 1234=~
DBMS_OUTPUT.PUT_LINE('n / 5678=' || n / 5678); -- produces n / 5678=~
Hey - have fun sweating your financials! :-)
In actual practice you're very unlikely to encounter this behavior, but it's the kind of thing you really need to be aware of - not only because encountering it can really ruin your month, but because (and you can count on this) next week the clueless guy in the cube by the bathroom is going to be asking about this - and you will now know all about it. (And you will now be able to rest easy, comfortable in the knowledge that this guy really is clueless and thus deserves to be parked in The Cube From Hell. I mean, you picked up on this on StackOverflow, right? So how hard can it be? :-)
Share and enjoy.
Some C-like languages allow multiple statements in the update part of a for statement, e.g.
for (int i = 0; i < limit; i++, butter--, syrup--, pancakesDevoured++)
Java explicitly defines the order for such statements in JLS 14.14.1.2 and 15.8.3 of ECMA-334 (C#) says "the expressions of the for-iterator, if any, are evaluated in
sequence" which I'm reading as left-to-right.
What languages, if any, allow multiple statements in the update part of a for loop but either don't define an ordering for such statements or use an order other than left to right?
edit: removed the C tag since that started a sequence point discussion and there's plenty of that already.
Technically, that's a single expression, not one or more statements, though the distinction isn't super important.
Good old C makes only limited promises about what order expressions like this will be evaluated in. In your example, the order doesn't matter. But consider this expression:
a[i++] = i
If i was 1 before evaluating this expression, should a[1] now equal 1 or 2? As I understand the C specification, the behavior of this expression is undefined, meaning that what you get depends on which compiler you use.
Thanks to #mizo for a readable reference on sequence points, and to #ChrisDodd for pointing out that if you're making simple independent assignments that are separated by the comma operator, C and C++ do fully specify a left-to-right evaluation order.
When looking at pseudocode (actually, on the Wikipedia article on A*), i came across the use of := to assign or initialize a variable. What does this mean? Is part of some kind of set notation? If it's something complicated, how would one go about implementing this in C++ or Java? Thanks.
:= indicates assignment of a variable. := is used when = is a test for equality (rather than the standard == seen in most modern programming languages), not an assignment. In Pascal, for instance, := is used for assignment, and = is used to test for equality. See the "Notation" section of the assignment article for a list of notations for assignment.