My macro is not assignable - objective-c

I'm trying to define a macro that returns an absolute value of numbers. Here how it looks like:
#define ABSOLUTE_VALUE(v) ( (v) < 0 ? (v) *= -1 : (v))
It works fine when I insert just a single number. The problem is, when I try to insert an expression (number + anotherNumber, for example), the compiler throws an error, saying that the expression is not assignable. I can't figure out why it does that. If you know the reason of the error, I would appreciate your help.

What's going on here is that the entire expression number + anotherNumber is getting passed to your macro, so anywhere there is v, it's not the result v = number + anotherNumber, rather, v is replaced by number + anotherNumber before evaluation. So:
(v) *= -1
Becomes
(number + anotherNumber) *= -1
Since number + anotherNumber is yet another number, that part of the code is trying to assign the value of -1 * (number + anotherNumber) to number + anotherNumber , which results in the error you are seeing, because you can't assign something to an expression.

From one of your comments:
The problem is that I'm trying to assign the result of (number + anotherNumber) * (-1) to an expression, which is number + anotherNumber. But shouldn't the + fire first, before the *= operator?
You are misunderstanding assignment. The left hand side (LHS) of an assignment must be an lvalue, which means something like a variable, which references a storage location into which values can be stored.
A value is just that, it is not associated with any storage location, you cannot assign to it. The result of your expression number + anotherNumber is a number, it cannot go on the LHS of an assignment.
Your macro would work if you simply replace =* with *.
HTH

Related

Ada - Operator subprogram

Create a subprogram of type operator that receives two integers and sends them back
the negative sum of them. I.e. if the sum is positive it will be
the result is negative or if the sum is a negative result
positive. Ex. 6 and 4 give -10 as a result and 2 and -6 give 4.
For instance:
Type in two integers: **7 -10**
The negative sum of the two integers is 3.
Type in two integers: **-10 7**
The positive sum of the two integers is -3.
No entries or prints may be made in the subprogram.
So I attempted this task and actually solved it pretty easily using a function but when it came to converting it to an operator I stumbled into a problem.
This is my approach:
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
procedure Test is
function "+" (Left, Right : in Integer) return Integer is
Sum : Integer;
begin
Sum := -(Left + Right);
return Sum;
end "+";
Left, Right : Integer;
begin
Put("Type in two integers: ");
Get(Left);
Get(Right);
Put("The ");
if -(Left + Right) >= 0 then
Put("negative ");
else
Put("positive ");
end if;
Put("sum of the two integers is: ");
Put(-(Left + Right));
end Test;
My program compiles but when I run it and type two integers it says:
raised STORAGE_ERROR: infinite recursion
How do I solve this problem using an operator? I managed to tackle it easily with the procedure- and function subprogram but not the operator. Any help is appreciated!
You can use the type system to solve this without using a new operator symbol.
As a hint, operators can overload on argument and return types. And a close reading of the question shows the input type is specified, but not the output type. So, how about this?
type Not_Integer is new Integer;
function "+" (Left, Right : in Integer) return Not_Integer is
Sum : Integer;
begin
Sum := -(Left + Right);
return Not_Integer(Sum);
end "+";
As the two "+" operators have different return types, there is no ambiguity between them and no infinite recursion.
You will have to modify the main program to assign the result to a Not_Integer variable in order to use the new operator.

Maths Rules in Kotlin

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.

Invalid data conversion: Parameter instance 50.0/100 is invalid for the requested conversion. ERRORCODE=-4461, SQLSTATE=42815

String expression = CHEMICAL_REORDERPOINT + "*" + searchRequest.getReorderPercentage() + "/100)";
Before :
String expression = CHEMICAL_REORDERPOINT + "*" + searchRequest.getReorderPercentage() + "/100)";
searchRequest.getReorderPercentage() comes dynamically from browser after submit value.
Lets take value for searchRequest.getReorderPercentage() = 50
so String expression = CHEMICAL_REORDERPOINT*50/100;
This is getting populated in a prepared statement of JDBC in my application, so to maintain the prepare statement rule i have used in below way:
After :
String expression = CHEMICAL_REORDERPOINT + "*?)"
String str = searchRequest.getReorderPercentage() + "/100";
params.add(str)
here params is a list from which the parameters will be iterated and will be placed in postion parameters of prepare statement while executing it.
But now i m getting exception like
Invalid data conversion: Parameter instance 50.0/100 is invalid for the requested conversion. ERRORCODE=-4461, SQLSTATE=42815
Pls can any one help me out. Thanks
Your parameterized query string should look like "whatever is in CHEMICAL_REORDERPOINT * (? / 100)", and you should set the percentage value using setDouble() or setFloat(), not as a String. Right now you are trying to tell DB2 to multiply whatever by a string, which doesn't make much sense.
If I had to guess I would say it is an auto conversion issue.
You had something like
5 * 50.0 / 100
and it worked.
No you have
50.0 / 100
and it fails.
It seems in the first it converted all operands to integers and in the second it converts them to floating point. But 100 is not a valid floating point constant. So change your new output to
50.0 / 100.0
or
50 / 100

How do I perform a LIKE query on a PostgreSQL primary id column?

If I have a number (such as 88) and I want to perform a LIKE query in Rails on a primary ID column to return all records that contain that number at the end of the ID (IE: 88, 288, etc.), how would I do that? Here's the code to generate the result, which works fine in SQLLite:
#item = Item.where("id like ?", "88").all
In PostgreSQL, I'm running into this error:
PG::Error: ERROR: operator does not exist: integer ~~ unknown
How do I do this? I've tried converting the number to a string, but that doesn't seem to work either.
Based on Erwin's Answer:
This is a very old question, but in case someone needs it, there is one very simple answer, using ::text cast:
Item.where("(id::text LIKE ?)", "%#{numeric_variable}").all
This way, you find the number anywhere in the string.
Use % wildcard to the left only if you want the number to be at the end of the string.
Use % wildcard to the right also, if you want the number to be anywhere in the string.
Simple case
LIKE is for string/text types. Since your primary key is an integer, you should use a mathematical operation instead.
Use modulo to get the remainder of the id value, when divided by 100.
Item.where("id % 100 = 88")
This will return Item records whose id column ends with 88
1288
1488
1238872388
862388
etc...
Match against arbitrary set of final two digits
If you are going to do this dynamically (e.g. match against an arbitrary set of two digits, but you know it will always be two digits), you could do something like:
Item.where(["id % 100 = ?", last_two_digits)
Match against any set or number of final digits
If you wanted to match an arbitrary number of digits, so long as they were always the final digits (as opposed to digits appearing elsewhere in the id field), you could add a custom method on your model. Something like:
class Item < ActiveRecord
...
def find_by_final_digits(num_digits, digit_pattern)
# Where 'num_digits' is the number of final digits to match
# and `digit_pattern` is the set of final digits you're looking fo
Item.where(["id % ? = ?", 10**num_digits, digit_pattern])
end
...
end
Using this method, you could find id values ending in 88, with:
Item.find_by_final_digits(2, 88)
Match against a range of final digits, of any length
Let's say you wanted to find all id values that end with digits between 09 and 12, for whatever reason. Maybe they represent some special range of codes you're looking up. To do this you could do another custom method to use Postgres' BETWEEN to find on a range.
def find_by_final_digit_range(num_digits, start_of_range, end_of_range)
Item.where(["id % ? BETWEEN ? AND ?", 10**num_digits, start_of_range, end_of_range)
end
...and could be called using:
Item.find_by_final_digit_range(2, 9, 12)
...of course, this is all just a little crazy, and probably overkill.
The LIKE operator is for string types only.
Use the modulo operator % for what you are trying to do:
#item = Item.where("(id % 100) = ?", "88").all
I doubt it "works" in SQLite, even though it coerces the numeric types to strings. Without leading % the pattern just won't work.
-> sqlfiddle demo
Cast to text and use LIKE as you intended for arbitrary length:
#item = Item.where("(id::text LIKE ('%'::text || ?)", "'12345'").all
Or, mathematically:
#item = Item.where("(id % 10^(length(?)) = ?", "'12345'", "12345").all
LIKE operator does not work with number types and id is the number type so you can use it with concat
SELECT * FROM TABLE_NAME WHERE concat("id") LIKE '%ID%'

How to do calculations with database values in Classic ASP?

I have to do a calculation to an integer value in classic asp, but i keep getting "type mismatch" error on this line: X = tempID + 1
this is my code:
Set TheRS = Conn.Execute("select top 1 xx from W_TABLE order by IDX desc")
tempID = TheRS("xx")
X = tempID + 1
TheRS.Close
Set TheRS = nothing
tempX is of type "User-defined Type"... i'm assuming that's why it wont let me add 1 to it... how can i make this happen?
I've had this problem with numeric/decimal columns. ADO returns the value as a Decimal type, but VBScript doesn't really support the Decimal type. About the only thing you can do with a variable that contains a Decimal type value is convert it using CInt, CDbl, or any of the other conversion functions. My guess is that if you were to inspect the value of VarType(tempID), you would see 14, which corresponds to vbDecimal.
To work around this, you should convert the value using CDbl (or CInt if you don't care about the decimal portion).
Check if the recordset is end of file first. (If TheRS.Eof Then)
It seems to me "xx", whatever that is, isn't actually an integer. Are you sure your query is returning a numerical answer?
Chances are that's why you can't add 1 to it. Try cast it as an integer first: X = CInt(tempID) + 1
Response.Write(tempID) before adding 1 and see what value it has
Not to be rude but that code is indecipherable! W_TABLE? IDX? xx? These are all terrible variable and schema names. I sincerely hope that you have just sanitized your real, appropriately named variables for more abstract, confusing ones.
If you don't want to bother what your database query returns this should do the trick:
X = CInt("0" & tempID) + 1
got it working by changing the select.
select top 1 xx+1 from W_TABLE order by IDX desc
thanks everyone!