1
from pylab import *
def x(t) :
 if 0 <= t < 8 :
  return(2*t)
elif 8 <= t < 20 :
  return(t**3)

t = arange(5.0, 20, 0.3)
print([i for i in t])

Output is [5.0, 5.3, 5.6, 5.8999999999999995, 6.199999999999999, 6.499999999999999, 6.799999999999999, 7.099999999999999, 7.399999999999999, 7.699999999999998, 7.999999999999998, 8.299999999999997, 8.599999999999998, 8.899999999999999, 9.199999999999998, 9.499999999999996, 9.799999999999997, 10.099999999999998, 10.399999999999997, 10.699999999999996, 10.999999999999996, 11.299999999999997, 11.599999999999996, 11.899999999999995, 12.199999999999996, 12.499999999999996, 12.799999999999995, 13.099999999999994, 13.399999999999995, 13.699999999999996, 13.999999999999995, 14.299999999999994, 14.599999999999994, 14.899999999999995, 15.199999999999994, 15.499999999999993, 15.799999999999994, 16.099999999999994, 16.39999999999999, 16.699999999999992, 16.999999999999993, 17.299999999999994, 17.599999999999994, 17.89999999999999, 18.199999999999992, 18.499999999999993, 18.79999999999999, 19.09999999999999, 19.39999999999999, 19.699999999999992, 19.999999999999993]

What I want is [5.0, 5.3, 5.6, 5.9, 6.2, 6.5, 6.8, 7.1, 7.4, 7.7, 8.0, so on]

When it comes to 8.0, my output is 7.999999999999998 < 8. So wrong answer. I want 8.0. So that I can plot function.

plot(t, array([x(i) for i in t]))
Phyton12
  • 11
  • 1
  • 1
    With the precision your CPU is capable of, `7.999999999999998` _is_ 8.0. See: https://stackoverflow.com/questions/588004/is-floating-point-math-broken – ForceBru Dec 17 '20 at 08:11
  • Then how can I avoid this? – Phyton12 Dec 17 '20 at 08:17
  • You simply can't - this is how floating-point math works – ForceBru Dec 17 '20 at 08:17
  • How about using round function? Making plot(t, array([x(round(i,1)) for i in t])) makes sense? – Phyton12 Dec 17 '20 at 08:22
  • Makes sense, but 7.999999999999998 is already very, very close to 8.0 - you wouldn't see any difference in your plot if you use rounding – ForceBru Dec 17 '20 at 08:52
  • Thank you so much for your comments. I appreciate it. – Phyton12 Dec 17 '20 at 09:06
  • Unrelated to your question: [pylab is disapproved by matplotlib](https://matplotlib.org/3.3.0/api/index.html#module-pylab) and [`import *` is discouraged](https://stackoverflow.com/questions/2386714/why-is-import-bad) due to namespace cluttering. Don't do this. – Mr. T Dec 17 '20 at 09:51

1 Answers1

0

I guess a simple rounding off is all you need.

Change the last line to this:

print([round(i,1) for i in t])

Output:

[5.0, 5.3, 5.6, 5.9, 6.2, 6.5, 6.8, 7.1, 7.4, 7.7, 8.0, 8.3, 8.6, 8.9, 9.2, 9.5, 9.8, 10.1, 10.4, 10.7, 11.0, 11.3, 11.6, 11.9, 12.2, 12.5, 12.8, 13.1, 13.4, 13.7, 14.0, 14.3, 14.6, 14.9, 15.2, 15.5, 15.8, 16.1, 16.4, 16.7, 17.0, 17.3, 17.6, 17.9, 18.2, 18.5, 18.8, 19.1, 19.4, 19.7]

So in your case the code becomes something like:

from pylab import *
def x(t) :
    if 0 <= t < 8 :
        return(2*t)
    elif 8 <= t < 20 :
        return(t**3)

t = arange(5.0, 20, 0.3)
t = [round(i,1) for i in t]
print(t)

Now you can use this t and get the following plot:

enter image description here

Amit Amola
  • 2,301
  • 2
  • 22
  • 37