Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I encountered a floating point problem when dealing with flow:
a = "0.0003"
b = pow(10, 4)
c = int(float(a) * b)) // int so i don't want the decimal places behind
i got the following result 2.9999999999999996 which is wrong
I also tried decimal:
c = int(Decimal(a) * Decimal(pow(10, 4)))
but still got the wrong result 2.999999999999999737189393390
Expected result is 3
In Javascript we can use bignumber.js
Is there a way i can avoid this kind of problem in python? maybe some library like in javascript
Thanks
Make sure a is the string, not the float. Then it should work.
int(Decimal('0.0003') * pow(10, 4))
# returns 3
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 months ago.
Improve this question
does anyone know why I can't subtract a variable from another variable if both are defined as numbers?
A = 100
B = int(input("Amount? "))
A - B
print(A)
This is a very simplified version of what it actually is but still the same concept, I am quite new to coding so sorry if this is really dumb of me to ask. Thanks :)
Replace A - B to A = A - B or use the shorthand notation A -= B which is the same as the first alternative.
You are never assigning the value to A.
Here is the corrected code:
A = 100
B = int(input("Amount? "))
A = A - B #Issue was here
print(A)
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
def kaka(name):
r=''
for ch in name:
r=r+ch*3
return r
Output:
>>> kaka('Mississippi')
>>> 'MMMiiissssssiiissssssiiippppppiii'
But for this code:
def kaka(name):
for ch in name:
r=''
r=r+ch*3
return r
I am getting output as: iii
That's because in your second code you're re-assigning r back to the empty string ''. Thus you only get the final character multiplied 3 times (which for Mississippi is i).
You are getting 2 different outputs because in the first code you are initialising the value of r i.e r = '' outside the for loop and in the second program you are initialising value of r inside the for loop.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
100_year = date.today().year - age + 100
^
SyntaxError: invalid decimal literal
I'm trying to understand what the problem is.
Python identifiers can not start with a number.
The 'arrow' points to year because underscore is a valid thousands separator in Python >= 3.6, so 100_000 is a valid integer literal.
Sadly in python you cant make variables that start with numbers so therefore you could use something like this:
hundred_year = date.today().year - age + 100
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I'm wondering if there is some built in function in python that will change whatever the sign of an integer is to its opposite.
example of what I'm looking for:
num = 1
num2 = -2
# where flip() is a hypothetical function
flip(num)
flip(num2)
print(num)
print(num2)
>>> -1
>>> 2
I know how to create my own function to do this, but it seems there may be a built-in, or some short hand way to go about it.
So this was actually way more simple than I was trying to make it.
num1 = 1
num1 = -num1
print(num1)
>>> -1
num1 = -num1
print(num1)
>>> 1
Two negatives equal a positive
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
Hi I would like to store strings on a array. This strings are produced in this loop:
while (count < ts ):
dt=tb
t1=count+180
t2=t1+360
dt1=dt+t1
dt2=dt+t2
slice=stream.slice(dt1, dt2)
B=str(dt1)
E=str(dt2)
slice.write(station+'_'+comp[i]+'_'+B+'_'+E, format="MSEED")
count = count + 360
bb=[]
name=station+B+'_'+E
a=[str(name)]
bb.append(a)
But it doesn't work. The variable name is from type:
name=2011-05-22T23:54:00.000000Z_2011-05-22T23:59:59.984000Z
And I would like to have an array like that:
bb=[2011-05-22T23:42:00.000000Z_2011-05-22T23:48:00.000000Z, 2011-05-22T23:48:00.000000Z_2011-05-22T23:54:00.000000Z, 2011-05-22T23:54:00.000000Z_2011-05-22T23:59:59.984000Z]
But what bb returns me is an array with the last element called:
bb=[2011-05-22T23:54:00.000000Z_2011-05-22T23:59:59.984000Z]
If I do it manually:
bb.append('2011-05-22T23:54:00.000000Z_2011-05-22T23:59:59.984000Z')
It works perfectly because I put the ''. But I need to it in a automatic way.
Any suggestion?
Thanks in advance!
Declare bb outside the loop and a will be a list. You will get a list of lists(not in the way you asked for)