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))
Related
This question already has answers here:
Iterate through adjacent pairs of items in a Python list [duplicate]
(5 answers)
Closed 1 year ago.
lx = [92770.0, 90204.9, 89437.3, 88868.4, 88298.8, 87505.0, 86792.7, 85938.7, 84652.2, 82803.4, 80174.3, 76559.3, 71584.5]
Lx = []
I want Lx to be a list containing the average of every two adjacent numbers in lx.
eg Lx = [(92770.0 + 90204.9)/2, (90204.+89437.3)/2, etc.....
Please help
lx = [92770.0, 90204.9, 89437.3, 88868.4, 88298.8, 87505.0, 86792.7, 85938.7, 84652.2, 82803.4, 80174.3, 76559.3, 71584.5]
Lx = [sum(x) / 2 for x in zip(lx, lx[1:])]
print(Lx)
Prints:
[91487.45, 89821.1, 89152.85, 88583.6, 87901.9, 87148.85, 86365.7, 85295.45, 83727.79999999999, 81488.85, 78366.8, 74071.9]
Here is the solution
lx = [92770.0, 90204.9, 89437.3, 88868.4, 88298.8, 87505.0, 86792.7, 85938.7, 84652.2, 82803.4, 80174.3, 76559.3, 71584.5]
Lx = []
for i in range(len(lx)-1):
Lx.append((lx[i]+lx[i+1])/2)
print(Lx)
A solution using numpy broadcasting would be:
result = (np.array(lx[:-1])+np.array(lx[1:]))/2
How about this ? sorry for noob way
for i in range(len(lx)-1):
Lx.append((lx[i] + lx[i+1]) / 2)
#Y=mx+b
x1, y1 = input("X1,Y1: ").split(",")
x2, y2 = input("X2,Y2: ").split(",")
print("\n")
Xdif = (int(x1) - int(x2))
Ydif = (int(y1) - int(y2))
Yslope = (int(Ydif) * int(x1))
if(Xdif == 0):
print("Slope is Undefined")
else:
Slope = (int(Yslope) / int(Xdif))
if(int((Slope*10) % 10) == 0):
SlopeN, bad = str(Slope).split(".")
print("Slope:",SlopeN)
else:
print("Slope:",Slope)
Why = (int(y1) - int(Slope))
print(Why)
I'm new to stack overflow but have been using python for about two months now. I'm relatively experienced but have no idea why when printing the variable "Why"it automatically rounds. I am creating a script to find the slope-intercept form from two points on a graph. Any help is appreciated.
-Noah
Edit changed variable name
int(n) will return an Integer. If n is a float, it will be truncated.
The difference between two integers is (surprisingly...) an integer, so yes the final result is a truncated Integer.
Consider using float(n) instead of int(n).
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.
This question already has answers here:
How can I print multiple things (fixed text and/or variable values) on the same line, all at once?
(13 answers)
How to write multiple strings in one line?
(12 answers)
Closed 5 years ago.
I would like to write two variable in a file. I mean this is my code :
file.write("a = %g\n" %(params[0]))
file.write("b = %g\n" %(params[1]))
and what I want to write in my file is :
f(x) = ax + b
where a is params[0] and b is params[1] but I don't know how to do this ?
Thank you for your help !
If all you want to write to your file is f(x) = ax + b where a and b are params[0] and params[1], respectively, just do this:
file.write('f(x) = %gx + %g\n' % (params[0], params[1]))
'f(x) = %gx + %g' % (params[0], params[1]) is simply string formatting, where you're putting a and b in their correct spaces.
Edit: If you're using Python 3.6, you can use f-strings:
a, b = params[0], params[1]
file.write(f'f(x) = {a}x + {b}\n')
"f(x) = {a}x + {b}".format(a=params[0], b=params[1])
Is a clean solution
sorry I don't know Python, but I guess this
f = open('file', 'w')
x = 0;
a = 0;
b = 0;
result = a*x+b
a = str(a)
b = str(b)
x = str(x)
result = str(result)
f.write("f("+x+")="+result) #this is if you want result to be shown
print("f("+x+")="+result)
#or
f.write("f("+x+")="+a+""+x+"+"+b) #this is if you want actually show f(x)= ax+b
print("f("+x+")="+a+""+x+"+"+b)
again I don't know Python, but this is what I come up with by using : https://repl.it/HARP/1
I hope this helps
You target is to achieve is to write the equation below to be written inside the file.
f(x) = ax + b where a is params[0] and b is params[1]
What you should do is
file.write('f(x) = %gx + %g' % (param[0], param[1]))
which will write
"f(x) = 2x + 3" # if params[0] and params[1] are 2 and 3 resp
What you are doing is
file.write("a = %g\n" %(params[0]))
file.write("b = %g\n" %(params[1]))
This will write in the file as:
a = 2
b = 3
if params[0] and params[1] are 2 and 3 respectively
This question already has answers here:
How do I clone a list so that it doesn't change unexpectedly after assignment?
(24 answers)
Closed 7 years ago.
Given below is my code for the kadane's algorithm in Python 2.7 for returning the maximum sub array. Although, i'm getting the correct maximum sum(MSS variable) ,for the given example list,
it's returning the wrong sub array. Could someone please explain to me why ?
A = [-2,1,-3,4,-1,2,1,-5,4]
M = max(A)
L = len(A)
if(M < 0):
print M
ans = []
subans = []
MSS,subsum,i = 0,0,0
while(i<L):
subans.append(A[i])
subsum = sum(subans)
if(subsum<0):
subans=[]
i+=1
else:
if(subsum>MSS):
MSS=subsum
ans=subans
i+=1
else:
i+=1
print ans
Your issue is because when you do -
ans=subans
You are just storing the reference of subans to ans , when you change something within subans, the changes also reflect in ans (as they are the same reference).
You need to store a copy of subans in ans , instead of the direct reference.
Example -
ans = []
subans = []
MSS,subsum,i = 0,0,0
while(i<L):
subans.append(A[i])
subsum = sum(subans)
if(subsum<0):
subans=[]
i+=1
else:
print('subsum - ' + str(subsum))
print('MSS - ' + str(MSS))
if(subsum>MSS):
MSS=subsum
ans=list(subans) #more ways to do this, like subans[:] also works, and copy.copy(subans) , etc.
i+=1
else:
i+=1