Python Script - Math - Wrong results

python

#1

Hello everyone,

Im learning IronPython, so im trying to wirte down in python script some formulas which i did years before with code blocks and other nodes…
-So the weird thing is that the inputs for the code block and for python script are the same, but i get different results (look screen1 -yellow results)…
-in Screen3 you can see that the formula ist pretty much the same for “R1” and “R2” (please ignore the “SUM” (yellow) i will explain it later…
-so in Screen3 is the formula for “SUM” completly the same (yellow), but then the python script gives me an error…and i dont get any results for “R1” and “R2” also whats ist pretty weird.
-in the screen4 you can see the error m which is telling about a float in line 17.

So if would say the problems are because i still don´t handle the nomenclature of python, which is not the same for dynamo nodes in math , for example “Math.Pow” and “math.pow”
So if someone can help me i would be really thankfull, be patient with me…my englisch is not that good!

Thanks


#2


#3


#4


#5


#6

i m not sure but i think you need to unwrap elements from IN[2].

You are giving it a List now.

EDIT: Would you mind sharing your .dyn file? i would love to find out for you.


#7

I think it’s because you are trying to Add to lists. In Python, this will literally join the lists. What it is failing on though, and what is causing the issue is that you are also trying to use math,pow on a list.

Instead, try this…

def SumLists(R1,R2,val):
	arr = []
	for a,b in zip(R1,R2):
		arr.append(math.pow(a+b,val))
	return arr

OUT = SumLists(R1,R2,val)

Now, I noticed something interesting when doing this with Python (Dynamo 2.0.2). When I tried to divide a/b it returns the Floor value. ie 5/2 = 2 or -1/4 = 0 and as an integer. I’m not sure if it just me that is experiencing that though. @Kulkul, have you seen this before?


#8

My guess is, if you input integers you get integers out, if you transform them to doubles/floats then you’ll get the doubles/floats out as well :slight_smile:


#9

Huh, go figure. I thought if i was trying to divide a number by another and the result was not an integer it would automatically cast to double. :smile:


#10

Appears to be running into a limit with the number of digits as well… any time I see e^-13 I get worried. If you do the same in excel what values do you get, and is it within a rounding tolerance?


#11

yeah sure!

Thanks for you interest! 190210_math_python.dyn (16.0 KB)


#12

thank you for your advice…i will try it tomorrow or the day after it and tell if it helps


#13

Jacob…you dont have to get worried, and i also dont need to try excel…as i told, the method in green is cómpletly correct, i get the results im expecting …
but the method in pink (python) is giving the wrong results…
i already posted the script, take a look inside, maybe you understand better whats my problem


#14


try replacing the R1+R2 in python script with the circled formula in code block


#15

Dude look the third Image which i posted!
“-so in Screen3 is the formula for “SUM” completly the same (yellow), but then the python script gives me an error…and i dont get any results for “R1” and “R2” also whats ist pretty weird.”


#16

Because R1 and R2 are not values but lists…


#17

are you talking about what happens when SUM.append(math.pow((R1+R2),(-1,V))) or what do you mean right now?


#18

Yes you cannot use math operators on list objects


#19

but why ist it possible in a Block, and not with python? like in the block is also a math operator with list objects! and it works perfect


#20

It is possible. You just need to zip loop through the lists like my earlier example. As @Jonathan.Olesen and i said earlier, you can’t do it on the whole list object, but for each item in the list you can.