Path: csiph.com!feeder.erje.net!2.eu.feeder.erje.net!border1.nntp.ams1.giganews.com!nntp.giganews.com!buffer1.nntp.ams1.giganews.com!local2.nntp.ams1.giganews.com!nntp.brightview.co.uk!news.brightview.co.uk.POSTED!not-for-mail NNTP-Posting-Date: Wed, 13 Apr 2016 03:04:24 -0500 Subject: Re: Serious error in int() function? To: martin.spichty@gmail.com References: <52f7516c-8601-4252-ab16-bc30c59c8306@googlegroups.com> Newsgroups: comp.lang.python From: blindanagram@nowhere.net Message-ID: <570DFD89.7010808@nowhere.net> Date: Wed, 13 Apr 2016 09:04:25 +0100 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:38.0) Gecko/20100101 Thunderbird/38.7.2 MIME-Version: 1.0 In-Reply-To: <52f7516c-8601-4252-ab16-bc30c59c8306@googlegroups.com> Content-Type: text/plain; charset=windows-1252 Content-Transfer-Encoding: 8bit Lines: 23 X-Usenet-Provider: http://www.giganews.com X-Trace: sv3-TH4fb2Tu5AviG4wAToPrBgDkkr2/CSuh5Cvdh1Kg5kH9Ui/ggdc24dVWYSGMv1MbwUCHUexhFfg+Y+m!gJuOscASRsBUw73UIsqNlaHolw1oPmxoDUqu/RZcmD911Yq885DgXjv2wrL3R62luRGgt3w1VA== X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly X-Postfilter: 1.3.40 X-Original-Bytes: 1689 Xref: csiph.com comp.lang.python:106929 On 13/04/2016 08:41, martin.spichty@gmail.com wrote: > Hi, > > there may be a serious error in python's int() function: > > print int(float(2.8/0.1)) > > yields > > 27 > > instead of 28!! > > I am using Python Python 2.7.6, GCC 4.8.2 on Linux Ubuntu. > > Is that known? This arises because finite floating point arithmetic is not exact so 2.8/0.1 results in 27.999999999999996. And, since the int() function truncates towards zero, this results in a value of 27. If you want the nearest integer you should use round(x) rather than int(x).