Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Unexpected result
#1
I'm teaching myself python and used the following tutorial:

https://www.pythoncentral.io/pythons-ran...explained/

>>> def frange(start, stop, step):
...     i = start
...     while i < stop:
...         yield i
...         i += step
...
>>> for f in frange(0.5,3.0,0.1):
...     print(f)
Instead of increments in 0.1 steps I get:

Output:
0.5 0.6 0.7 0.7999999999999999 0.8999999999999999 0.9999999999999999 1.0999999999999999 1.2 1.3 1.4000000000000001 1.5000000000000002 1.6000000000000003 1.7000000000000004 1.8000000000000005 1.9000000000000006 2.0000000000000004 2.1000000000000005 2.2000000000000006 2.3000000000000007 2.400000000000001 2.500000000000001 2.600000000000001 2.700000000000001 2.800000000000001 2.9000000000000012 >>>
I can't work out why some of the increments have values that aren't exactly .1 from the previous.
Is there some secret floa behaviour?

Thanks.
Quote

Top Page

Messages In This Thread
Unexpected result - by eftimios - Dec-02-2018, 01:54 AM
RE: Unexpected result - by Gribouillis - Dec-02-2018, 07:39 AM

Possibly Related Threads...
Thread Author Replies Views Last Post
  Unexpected expected type error result MartinMaker 1 294 Feb-16-2019, 05:02 PM
Last Post: micseydel
  unexpected sub result after overloading operator jolinchewjb 1 320 Jan-24-2019, 08:23 AM
Last Post: buran
  Unexpected result in simple prime number example jackhj 2 697 Apr-20-2018, 01:48 AM
Last Post: jackhj
  Reversing word in strings yields unexpected result Dec 4 1,257 May-17-2017, 05:32 PM
Last Post: wavic
  datetime unexpected result PickyBiker 10 3,420 Dec-27-2016, 10:47 PM
Last Post: PickyBiker

Forum Jump:


Users browsing this thread: 1 Guest(s)