Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #106935
| From | Gregory Ewing <greg.ewing@canterbury.ac.nz> |
|---|---|
| Newsgroups | comp.lang.python |
| Subject | Re: Serious error in int() function? |
| Date | 2016-04-13 23:51 +1200 |
| Message-ID | <dn6q60FmpvfU1@mid.individual.net> (permalink) |
| References | <52f7516c-8601-4252-ab16-bc30c59c8306@googlegroups.com> |
martin.spichty@gmail.com wrote: > print int(float(2.8/0.1)) > > yields > > 27 > > instead of 28!! This is a consequence of the fact that the machine does floating point arithmetic in binary, not decimal. 0.1 is not exactly representable as a binary floating point number, and the result of the division comes out very slightly less that 28: >>> 2.8/0.1 27.999999999999996 This is not unique to Python; you'll get the same result from anything that uses the machine's native floating point numbers. When using floating point, you always need to be prepared for slight inaccuracies. Usually you don't notice them, because the results are rounded to some reasonable number of decimal places before display. But sometimes they show up, such as when using int(), which truncates instead of rounding. The best way to handle things like this depends on the circumstances. If you tell us what you're trying to achieve, we may be able to offer advice. -- Greg
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Serious error in int() function? martin.spichty@gmail.com - 2016-04-13 00:41 -0700
Re: Serious error in int() function? Marko Rauhamaa <marko@pacujo.net> - 2016-04-13 10:55 +0300
Re: Serious error in int() function? Alain Ketterlin <alain@universite-de-strasbourg.fr.invalid> - 2016-04-13 10:00 +0200
Re: Serious error in int() function? Peter Otten <__peter__@web.de> - 2016-04-13 10:03 +0200
Re: Serious error in int() function? blindanagram@nowhere.net - 2016-04-13 09:04 +0100
Re: Serious error in int() function? Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2016-04-13 23:51 +1200
Re: Serious error in int() function? "ast" <nomail@com.invalid> - 2016-04-14 08:52 +0200
Re: Serious error in int() function? Christian Gollwitzer <auriocus@gmx.de> - 2016-04-14 09:25 +0200
Accuracy of math.sqrt(), was Re: Serious error in int() function? Peter Otten <__peter__@web.de> - 2016-04-14 09:46 +0200
Re: Serious error in int() function? blindanagram@nowhere.net - 2016-04-14 08:59 +0100
Re: Serious error in int() function? blindanagram@nowhere.net - 2016-04-14 09:13 +0100
Re: Serious error in int() function? blindanagram@nowhere.net - 2016-04-14 13:07 +0100
Re: Serious error in int() function? Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2016-04-14 20:36 -0400
Re: Serious error in int() function? Christian Gollwitzer <auriocus@gmx.de> - 2016-04-15 04:38 +0200
Re: Serious error in int() function? John Pote <johnhpote@o2.co.uk> - 2016-04-15 12:05 +0100
Re: Serious error in int() function? alister <alister.ware@ntlworld.com> - 2016-04-15 11:50 +0000
Re: Serious error in int() function? blindanagram@nowhere.net - 2016-04-15 09:41 +0100
Re: Serious error in int() function? blindanagram@nowhere.net - 2016-04-15 09:41 +0100
csiph-web