Numbers changes after list repetition? [duplicate] - python

This question already has answers here:
List of lists changes reflected across sublists unexpectedly
(17 answers)
Why does floating-point arithmetic not give exact results when adding decimal fractions?
(31 answers)
Closed 5 years ago.
I have this function:
def generateRepetition(toR, rate):
return [toR]*rate
If I call it with toR=0.234 the function returns an array in which the number is repeated for rate parameter, however, the number changes in this way:
[0.23399999999999999, 0.23399999999999999, 0.23399999999999999,...]
I do not understand why, anybody has a solution?

Related

Difference between an Array and a List [duplicate]

This question already has answers here:
Python list vs. array – when to use?
(11 answers)
Array versus List<T>: When to use which?
(16 answers)
Closed 4 months ago.
I have values from 0-300, I must only project values >50 and <200,
I can do this with both List and Array but I am forced to use a Array for this task,
what is the exact difference between the two in terms on ability?
I was able to do it with both List and Array but still confused between the use of list if you can do the same thing with an Array

how do i convert the string '8 * 2' into an integer in python [duplicate]

This question already has answers here:
Evaluating a mathematical expression in a string
(14 answers)
How to calculate an equation in a string, python
(2 answers)
Closed 6 months ago.
Question is in the title.
How do i convert a string (8*2) for example into an integer sum.
I have googled around a lot and found no way to solve this isue and i cannot directly get the sum as integers.
Edit: I tried eval but it won't work because the sum has an x instead of a *

How does Python3 compare floats when using `set`? [duplicate]

This question already has answers here:
Set of floating point numbers
(3 answers)
What is the best way to compare floats for almost-equality in Python?
(18 answers)
Checking for NaN presence in a container
(2 answers)
Closed 1 year ago.
The set function in Python relies on the existence of a robust equality operator existing on the elements given to set.
Suppose we had a list of floating point numbers, xs, and we called set(xs). How will set compare the elements of xs? Will it check to see if two elements are within machine epsilon, or will it perform bitwise equality?

In Python, changing the order of float addition changes the output value. How? [duplicate]

This question already has answers here:
Why does changing the sum order returns a different result?
(7 answers)
Is floating point math broken?
(31 answers)
Closed 2 years ago.
I am working on some floating point addition in Python, i found this difference, changing the order of addition changes the value.
v1=2.7776548790102065
v2=2.932026860135167
v3=-2.5635999386901154
v4=-5.884153623433478
v5=0.16152830205880864
v6=2.614447767673556
v7=5.651999753771971
v8=-7.074990233473147
v9=12.624973219138516
print(v1+v2+v3+v4+v5+v6+v7+v8+v9) # 11.239886986191486
print(v1+v4+v7+v2+v5+v8+v3+v6+v9) # 11.239886986191484
can anyhow suggest me how to rectify this?

Avoid printing exponential value [duplicate]

This question already has answers here:
How to suppress scientific notation when printing float values?
(16 answers)
Convert Scientific Notation to Float
(3 answers)
Closed 3 years ago.
Is there a way to print the actual value instead of exponential value?
>>> val=0.00004
>>> print(str(val))
4e-05
As posted in here: How do I suppress scientific notation in Python?
print('{0:f}'.format(val))
Try this,
print('{:f}'.format(val))

Categories