Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (16.4k points)

I'm utilizing the arange function to characterize my for loop iterations and getting startling outcomes. 

i = arange(7.8,8.4,0.05)

print i

Gives the following:

[ 7.8   7.85  7.9   7.95  8.    8.05  8.1   8.15  8.2   8.25  8.3   8.35 8.4 ]

But still, utilizing the stop value of 8.35 as follows

i = arange(7.8,8.35,0.05)

yields the following

[ 7.8   7.85  7.9   7.95  8.    8.05  8.1   8.15  8.2   8.25  8.3 ]

However, I need my range to end at 8.35! I realize I can utilize the stop estimation of > 8.35 and < 8.4 to accomplish my outcome, however for what reason is it unique and in my mind, conflicting?

I'm using the 2.7 version

1 Answer

0 votes
by (26.4k points)

Maybe it has to do with limitations on the floating-point numbers. Because of machine exactness (Precision), it is preposterous to expect to store each possible value perfectly as an floating point. For instance: 

>>> 8.4

8.4000000000000004

>>> 8.35

8.3499999999999996

In this way, 8.4 as a floating-point is somewhat more prominent than the actual estimation of 8.4, while 8.35 as a floating-point is a smidgen less.

Are you interested to learn the concepts of Python in structure? Join the python course to gain more knowledge. 

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked Oct 15, 2019 in Python by Sammy (47.6k points)

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...