word() function returns the different results depending on gnuplot version 5.06 and 5.22 - function

I recently updated gnuplot latest version, 5.22 and my code didn't work properly. I debugged and found the reasons.
str="1 2"
print word(str,3)+0
In the previous version, 5.06 or older, the print shows 0 values without error.
But the latest version got error, "Non-numeric string found where a numeric expression was expected"
Without +0, both results are the same, blank (no output), but the latest version treats it as string I think.
My code has lots of routine related to word(), so how do I resolve this problem in the new version?

Your code seems to make two potentially dangerous assumptions:
that requesting the third element from a list of two elements returns an empty string, rather than causing an error, and
that converting that empty string to a number will yield 0.
Assumption 1 seems to still hold in gnuplot 5.2.2, but assumption 2 does not. If you really wanted that then you could create a wrapper
f(x) = (x eq "" ? 0 : x)
and use f(word(str,3)) instead of word(str,3). However, there might be a better way to deal with non-existing elements.

Use words to check the index:
w2num(list, i) = (i > 0 && i <= words(list)) ? word(list, i)+0 : 0
Example:
w2num(list, i) = (i > 0 && i <= words(list)) ? word(list, i)+0 : 0
l = "10 20"
do for [i=-1:3] { print w2num(l, i) }
prints
0
0
10
20
0

Related

Use of function / return

I had the task to code the following:
Take a list of integers and returns the value of these numbers added up, but only if they are odd.
Example input: [1,5,3,2]
Output: 9
I did the code below and it worked perfectly.
numbers = [1,5,3,2]
print(numbers)
add_up_the_odds = []
for number in numbers:
if number % 2 == 1:
add_up_the_odds.append(number)
print(add_up_the_odds)
print(sum(add_up_the_odds))
Then I tried to re-code it using function definition / return:
def add_up_the_odds(numbers):
odds = []
for number in range(1,len(numbers)):
if number % 2 == 1:
odds.append(number)
return odds
numbers = [1,5,3,2]
print (sum(odds))
But I couldn’t make it working, anybody can help with that?
Note: I'm going to assume Python 3.x
It looks like you're defining your function, but never calling it.
When the interpreter finishes going through your function definition, the function is now there for you to use - but it never actually executes until you tell it to.
Between the last two lines in your code, you need to call add_up_the_odds() on your numbers array, and assign the result to the odds variable.
i.e. odds = add_up_the_odds(numbers)

Is there a way to prevent invalid row range in powerbuilder

Is there a way to prevent invalid row range in powerbuilder.
IF dw_lista_campanias.GetSelectedRow(0) > 0 AND dw_lista_campanias.object.est_camp[dw_lista_campanias.GetRow()] = 'EO020' THEN
when dw_lista_campanias.object.est_camp index is 0 an exception is throw.
Invalid row range at line 193 in ue_opcion4 event of object w_os0210_mantenimiento_campanya.
You could put this statement in a TRY/CATCH block, but I'd think it'd be easier just to capture GetRow() into a variable and test it for 0 (which is a fairly normal state) before using it to access data.
Good luck.
Change your codes as below:
IF dw_lista_campanias.ROWCOUNT() > 0 THEN
IF dw_lista_campanias.GetSelectedRow(0) > 0 AND dw_lista_campanias.object.est_camp[dw_lista_campanias.GetRow()] = 'EO020' THEN
//PUT YOUR CODE HERE
END IF
END IF
Happy coding ( from pb developer :) )
I will assume there is a retrieve. E.g. ll_rowsrtn = this.retrieve().
If ll_rowsrtn > 0 then
//the getselectedrow script
End if
So the command will not execute unless datawindow has greater than 0 rows

Actionscript 3.0....x>y>z not working

probably a pretty simple question here, but I find it weird. Luckily I found a way around it, but the fact that what I did works and what I have in the title doesn't work is confusing the hell out of me!
I just have a simple if statement...then execute a function. This code works:
if (200 > (x-target.x) && (x-target.x) > 0)
fireWeapon();
yet this code doesn't!
if (200 > (x-target.x) > 0)
fireWeapon();
AS3 does not give me an error either....It just simply does an if statement for the condition
if (200 > (x-target.x))
and seems to ignore the statement where it must be greater than 0. I would like to use the shorter, more mathematically nice looking method in the future, so let me know if there is a way around doing the && sign! Thanks.
if (200 > (x-target.x) > 0) code is working. but what you think is different. computer in order to interpret one sentence. Is evaluated as follows.
1) 200 > ( x-target.x ) If 200 is larger than (x-target.x) return true, not false.
2) true(1) or false(0) > 0 If left-statement is true return true,(because 1 is larger than 0) not false.
As a result, If 200 is larger than (x-target.x) always return true, not false. In general, the syntax used in the computer language is not the same as mathematic syntax.
And you want x> y> z must change to x>y && y>z && x>z.

Flex Number.toFixed not giving expected result

I have a function that gets a numeric value (as Object) and returns a well formatted representation of that number. Because we can get very small numbers, in the process we use the Number object of flex. this is part of the code:
var numericValue:Number = Number(value.toString());
var fixed:String = numericValue.toFixed(precision);
This is the problem: there are situations that the numeric value is in the form of
5.684341886080802e-14
because we want to represent these numbers as 0 we use the above code. In this specific case, where precision is 0 we get an odd result
Initial Values:
value = 5.684341886080802e-14
percision = 0
Operation on values:
var numericValue:Number = Number(value.toString());
var fixed:String = numericValue.toFixed(precision);
Result:
fix = "1."
Why is this?
(BTW - on other numbers in the representataion of X.XXXXXXe-YY with percision bigger than 0 we get the correct result of 0)
This is a bug in Flash Player (FP-5141). It has been around for quite a while. The bug report says it is fixed, but it is not as of Flash Player 11.5.

Using a variable in a SQLAlchemy filter

I want to use a variable like this :
myVariable = 0.5
myQuery.filter(myTable.column1 == myVariable*myTable.column2)
I have then no results when I apply all() to myQuery.
If I replace the variable with its value, it is OK.
queryBidOffer = session.query(BidOffer.id, BidOffer.price, BidOffer.qmin, BidOffer.qmax)
queryBidOffer = queryBidOffer.join(Equipment).filter(BidOffer.equipment==Equipment.id, Equipment.equipment_type.in_(['L','P','W','M']))
queryBidOffer_day = queryBidOffer.filter(BidOffer.day == day)
queryBidOffer_hour = queryBidOffer_day.filter(BidOffer.start_hour == timeSlice)
queryBidOffer_hour = queryBidOffer_hour.join(EquipmentDayHour, BidOffer.equipment == EquipmentDayHour.equipment)
queryBidOffer_hour = queryBidOffer_hour.filter(EquipmentDayHour.day == day)
queryBidOffer_hour = queryBidOffer_hour.filter(EquipmentDayHour.hour == timeSlice)
factor1 = 1.00 - 0.07
queryBidOffer_hour = queryBidOffer_hour.filter(BidOffer.equipment == EquipmentDayHour.equipment, factor1*func.abs(EquipmentDayHour.ref_prog) == 930)
The problem is with the two last lines (factor1).
In the last line, if I replace factor1 by its value, it is OK.
IEEE floating point numbers as used by Python, and many databases do not always work as your school math. The reason why 0.93 worked was that that was the bitwisely exact value that you had originally stored in database. But slight error appears in the subtraction.
See this for example:
>>> print "%0.64f" % (1.0 - 0.07)
0.9299999999999999378275106209912337362766265869140625000000000000
>>> print "%0.64f" % (0.93)
0.9300000000000000488498130835068877786397933959960937500000000000
Read more here How should I do floating point comparison and then for very in depth essay you could read What Every Computer Scientist Should Know About Floating-Point Arithmetic...
One solution could be to use decimal.Decimal in Python and Numeric in SQL.
I've just tested out something similar to the code you are describing, but get results whether I use a variable or a literal:
myVariable = 0.5
myQuery.filter(myTable.column1 == myVariable*myTable.column2).all()
vs.
myQuery.filter(myTable.column1 == 0.5*myTable.column2).all()
Can you post up the literal SQL that is being genarated? i.e. the results of
print myQuery
for both of those?