This question already has answers here:
Why does the division get rounded to an integer? [duplicate]
(13 answers)
Closed 5 years ago.
↑
Yes, it does.
Python 2.7.13
I want to input 0 for the first prompt, then .37 for then next, so it comes out to .481
For some reason, and I know I've messed up, I cannot add the 'second' variable defined above, and the 'overallheight' value is not working:
u1 = 12.5
first = .481
u2 = 2
length = u1 * 12
second = u2 / 64
overallheight = first - second #this line is not subtracting
height_multiply = overallheight / .3075
width_multiply = length / 108
def calc():
x = float(raw_input("first prompt"))
y = float(raw_input("second prompt"))
y1 = (y - .0625) * height_multiply
x1 = x * width_multiply
y2 = y1 + second #this is the 'second' that needs to be added
print("(%s,%s)") % (x1, y2)
while 1>0:
calc()`
You are using Python 2, so / used with integers will do an integer division.
second = u2 / 64 with u2 = 2 is 2 / 64 which is 0.
To have float divistion, you can use from __future__ import division or use floats explicitely : u2 = 2.0.
Related
So for context, I'm working on a program that requires the Guass formula. It's used to find for example, 5 + 4 + 3 + 2 + 1, or, 8 + 7 + 6 + 5 + 4 + 3 + 2 + 1.
The formula is (n*(n + 1))/2,
I tried to incorporate this into a for loop, but I'm getting an error stating:
"'float' object cannot be interpreted as an integer"
This is my code:
# Defining Variables #
print("Give me a start")
x = int(input())
print("Give me a delta")
y = int(input())
print("Give me an amount of rows")
z = int(input())
archive_list = []
f = z + 1
stop = z*f
final_stop = stop/2
# Main Logic #
for loop in range(1,final_stop,1):
print("hi")
I would appreciate a response on why it wasn't working as well as a fixed code.
Thanks in advance!
As #ForceBru noted in his excellent comment, the problem is that the endpoint final_stop is a float, instead of an int.
The reason is because when computing it you used a single / instead of double.
If you replace
final_stop = stop/2
with
final_stop = stop//2,
then it should work fine.
I've created a formula with polynomial regression to calculate a drag point from an input. I tested the formula in a separate file with the input, and the output appears to be working as intended. However, in the actual file, it seems to be outputting tremendously incorrect numbers. My first thought was that the output style for OpenCV was in some odd format for the numbers, but when I printed the values they were just fine. It appears to work just fine with smaller formulas, but gives wild values for my created formula. Any help would be appreciated. Below are the formulae for calculating the x and y with a test input and a correct output, vs what I'm getting in the actual file. Thanks.
def triang_x(x1):
x2 = (-6.8808613455609384e+005) + (3.2312735687925651e+003 * x1)
+ (-6.0019455279289815e+000 * x1**2) + (1.0786025430222985e-002 * x1**3)
+ (-2.8031333306353576e-005 * x1**4) + (4.7125204049478773e-008 * x1**5)
+ (-3.8733353649642116e-011 * x1**6) + (1.4733899082497896e-014 * x1**7)
+ (-5.1033986030610612e-018 * x1**8) + (2.7445807881521161e-021 * x1**9)
+ (1.4039325861603808e-024 * x1**10) + (-7.8303365296494140e-027 * x1**11)
+ (2.0700235162417034e-029 * x1**12) + (-2.5633522287710555e-032 * x1**13)
+ (1.4236656075804622e-035 * x1**14) + (-2.9388878284829885e-039 * x1**15)
return x2
def triang_y(y1):
y2 = ((2.9536073194970668e+004) + (-7.2584981060026985e+002 * y1)
+ (5.9991721893954519e+000 * y1**2) + (-1.4273839368311947e-002 * y1**3)
+ (-6.1205911580247642e-005 * y1**4) + (4.1603526512587676e-007 * y1**5)
+ (-6.9546008738218303e-010 * y1**6) + (-1.1072665851528698e-013 * y1**7)
+ (-6.4446469064614884e-016 * y1**8) + (8.0190196135612936e-018 * y1**9)
+ (-8.8768139841444641e-021 * y1**10) + (-1.3685149110264805e-023 * y1**11)
+ (1.3193560897991867e-026 * y1**12) + (5.4138560249698032e-029 * y1**13)
+ (-9.5141032455036651e-032 * y1**14) + (4.4497796299711634e-035 * y1**15))
return y2
# Test Values are x:1116 y:398
#Correct Output is x:900.892612375319 y:889.0486684303542
#Output received in main is 10415680.385044796 -167144.0485716732
The way your question is posed currently, I can't make heads or tails of it.
I can tell you that your first function has an error: you're trying to assign a sum of 16 summands but when you break that line, everything following will not be part of that sum. You should have gotten an IndentationError... Unless the code in your question is indented differently from what you actually ran on your own computer. If you indented that just right, you would simply have a bunch of 2-summand additions going off into nowhere because it's perfectly legal in python to have an expression whose value you don't assign to anything. To fix that, put parentheses ( and ) around the entire expression on the right-hand side of the assignment.
Your second function looks okay and appears to work as you want it. I can't reproduce the "wrong values" you state in your question.
Beyond that... instead of writing such a huge python expression, simply use numpy to express the coefficients and the evaluation of the polynomial:
import numpy as np
coeffs_x = np.float64([
-6.8808613455609384e+005, # 0
+3.2312735687925651e+003, # 1
-6.0019455279289815e+000, # 2
+1.0786025430222985e-002, # 3
-2.8031333306353576e-005, # 4
+4.7125204049478773e-008, # 5
-3.8733353649642116e-011, # 6
+1.4733899082497896e-014, # 7
-5.1033986030610612e-018, # 8
+2.7445807881521161e-021, # 9
+1.4039325861603808e-024, # 10
-7.8303365296494140e-027, # 11
+2.0700235162417034e-029, # 12
-2.5633522287710555e-032, # 13
+1.4236656075804622e-035, # 14
-2.9388878284829885e-039, # 15
])
triang_x = np.polynomial.Polynomial(coeffs_x)
coeffs_y = np.float64([
+2.9536073194970668e+004, # 0
-7.2584981060026985e+002, # 1
+5.9991721893954519e+000, # 2
-1.4273839368311947e-002, # 3
-6.1205911580247642e-005, # 4
+4.1603526512587676e-007, # 5
-6.9546008738218303e-010, # 6
-1.1072665851528698e-013, # 7
-6.4446469064614884e-016, # 8
+8.0190196135612936e-018, # 9
-8.8768139841444641e-021, # 10
-1.3685149110264805e-023, # 11
+1.3193560897991867e-026, # 12
+5.4138560249698032e-029, # 13
-9.5141032455036651e-032, # 14
+4.4497796299711634e-035, # 15
])
triang_y = np.polynomial.Polynomial(coeffs_y)
print(triang_x(1116), triang_y(398))
# => 900.8926123741549 889.0486684304415
I have an array of values x and y and have a function f(x,y). I want to get the value of f(x1,y1) corresponding to (x1,y1). How can we get it?
khmax = np.arange(0,0.5,0.001)
Ncmax = np.arange(0,0.5,0.001)
[X, Y] = np.meshgrid(Ncmax,khmax)
sum_real = 0
sum_imag = 0
for l in range (0,N): # C_jl * P_lj
sum_imag = sum_imag + (matrix_C[j_node-1][l])*(np.sin(Y*(x_j(l+1)-x_j(j_node)) / hmax))
sum_real = sum_real + (matrix_C[j_node-1][l])*(np.cos(Y*(x_j(l+1)-x_j(j_node)) / hmax))
Aj_real = (X * sum_real)
Aj_imag = (X * sum_imag)
G_imag = -Aj_imag + (2 * (Aj_real) * (Aj_imag)) / 2 - ((3 * ((Aj_real)**2) * (Aj_imag)) -((Aj_imag)**3)) + ((4*(Aj_real)*(Aj_imag))*((Aj_real)**2 - (Aj_imag)**2))/24
G_real = 1 - (Aj_real) + (((Aj_real)**2 - (Aj_imag)**2)/2) - ((((Aj_real)**3 - 3*(Aj_real)*((Aj_imag)**2)))/6) + ((((((Aj_real)**2 - (Aj_imag)**2 )**2- 4*((Aj_real)**2)*((Aj_imag)**2))))/ 24)
mod_G = (((G_real)**2) + ((G_imag)**2))**(0.5)
In this code mod_G is a function of (khmax, Ncmax). I want to get the value of mod_G corresponding to (khmax,Ncmax) suppose(0.1,0.1). I don't want to put the value of (khmax, Ncmax) into the function directly(i.e not replacing khmax with 0.1 and Ncmax with 0.1). How can I get mod_G without doing this?
Could you try defining it as an actual python function, i.e. using the def keyword and then calling it with the two parameters? This way you could just easily call it as mod_G(0.1,0.1) without changing anything else.
This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 3 years ago.
The following code gives an AssertionError in the last line.
However both values are printed as 10.0833333333.
p = [0,1,2,3,4,6,2,2,4,1,2,4]
cell11 = p[0:3]
cell12 = p[3:6]
cell21 = p[6:9]
cell22 = p[9:12]
x11 = sum(cell11)/float(3)
x12 = sum(cell12)/float(3)
x21 = sum(cell21)/float(3)
x22 = sum(cell22)/float(3)
x1dot = (x11+x12)/float(2)
x2dot = (x21+x22)/float(2)
xdot1 = (x11+x21)/float(2)
xdot2 = (x12+x22)/float(2)
xdotdot = (x1dot+x2dot)/float(2)
assert(xdotdot == (xdot1+xdot2)/float(2))
n=3
x11diff = ((x11-xdotdot) - (x1dot-xdotdot) - (xdot1-xdotdot))**2
x12diff = ((x12-xdotdot) - (x1dot-xdotdot) - (xdot2-xdotdot))**2
x21diff = ((x21-xdotdot) - (x2dot-xdotdot) - (xdot1-xdotdot))**2
x22diff = ((x22-xdotdot) - (x2dot-xdotdot) - (xdot2-xdotdot))**2
ssaxb = n*(x11diff+x12diff+x21diff+x22diff)
print str(ssaxb)
print str(10+(1/float(12)))
assert(ssaxb == 10+(1/float(12)))
Are ssaxb and 10+(1/float(12)) somehow stored as slightly different values?
As pointed out by #Chris_Rands printing the values will give you the reason why assert statement is failing as float values are different
import numpy as np
print(ssaxb, 10+(1/float(12))) #(10.08333333333333, 10.083333333333334)
assert(np.isclose(ssaxb, 10+(1/float(12))))
assert(round(ssaxb,7),round(10+(1/float(12)),7))
This question already has answers here:
Time to decimal time in Python
(3 answers)
Closed 9 years ago.
How can I convert a string like "12:30" or "5:51:23" into a decimal number representing the amount of elapsed hours in Python?
Fairly simple string-splitting and math:
def time_string_to_decimals(time_string):
fields = time_string.split(":")
hours = fields[0] if len(fields) > 0 else 0.0
minutes = fields[1] if len(fields) > 1 else 0.0
seconds = fields[2] if len(fields) > 2 else 0.0
return float(hours) + (float(minutes) / 60.0) + (float(seconds) / pow(60.0, 2)
If only a single number is given like "3", this will return 3.0. If two colon-delimited values are given like "4:57", this will return 4.95. If three colon-delimited values are given like "14:36:27", this will return 14.6075.
Possible Solution
>>> time_st = ["12:30" , "5:51:23"]
>>> HMS = [60*60, 60, 1]
>>> for t in time_st:
dec_time = sum(a * b for a,b in zip(HMS, map(int, t.split(":"))))
dec_time /= 3600.
print "{} = {}".format(t, dec_time)
12:30 = 12.5
5:51:23 = 5.85638888889
00:00 = 0.0
23:59 = 23.9833333333
>>> float('5:51:23'.split(':')[0])
5.0