-
Notifications
You must be signed in to change notification settings - Fork 2
/
comparing-floating-point-and-decimal-in-python.html
260 lines (222 loc) · 15.5 KB
/
comparing-floating-point-and-decimal-in-python.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head profile="http://gmpg.org/xfn/11">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Comparing floating-point and decimal in Python — Martin Kleppmann’s blog</title>
<link rel="stylesheet" type="text/css" media="screen, print, handheld" href="/css/typography.css" />
<link rel="stylesheet" type="text/css" media="screen" href="/css/style.css" />
<link rel="stylesheet" type="text/css" media="all" href="/css/pygments-default.css" />
<link rel="stylesheet" type="text/css" media="all" href="/css/ansi2html.css" />
<link rel="stylesheet" type="text/css" media="all" href="/css/customizations.css?2" />
<!--[if lt IE 8]>
<link rel="stylesheet" href="/css/ie.css" type="text/css" media="screen" charset="utf-8" />
<![endif]-->
<link rel="alternate" type="application/rss+xml" title="RSS" href="http://feeds.feedburner.com/martinkl?format=xml" title="Martin Kleppmann's blog" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js" type="text/javascript"></script>
<script src="http://downloads.mailchimp.com/js/jquery.form-n-validate.js" type="text/javascript"></script>
<script src="/form.js" type="text/javascript"></script>
</head>
<body class="wordpress">
<div id="page">
<p id="top">
<a id="to-content" href="#content" title="Skip to content">Skip to content</a>
</p>
<div id="header">
<div class="wrapper">
<strong id="blog-title">
<a href="/" title="Home" rel="home">Martin Kleppmann</a>
</strong>
<p id="blog-description">Entrepreneurship, web technology and the user experience</p>
</div>
</div>
<div id="sub-header">
<div class="wrapper">
<div id="navigation">
<ul>
<li class="page_item"><a href="/contact.html" title="About/Contact">About/Contact</a></li>
</ul>
</div>
</div>
</div>
<hr class="divider">
<div class="wrapper">
<div id="content">
<h1>
<div style="float: right; padding: 0 0 10px 20px;">
<script type="text/javascript">
tweetmeme_source = 'martinkl';
</script>
<script type="text/javascript" src="http://tweetmeme.com/i/scripts/button.js"></script>
</div>
Comparing floating-point and decimal in Python
</h1>
<p>Bizarre happenings in the world of Python. It seems that you are really not supposed to compare floating-point and decimal numbers, as this example shows:</p>
<div class='highlight'><pre><code class='pycon'><span class='go'>Python 2.5.1 (r251:54863, Jan 17 2008, 19:35:17)</span>
<span class='go'>[GCC 4.0.1 (Apple Inc. build 5465)] on darwin</span>
<span class='go'>Type "help", "copyright", "credits" or "license" for more information.</span>
<span class='gp'>>>> </span><span class='kn'>from</span> <span class='nn'>decimal</span> <span class='kn'>import</span> <span class='n'>Decimal</span>
<span class='gp'>>>> </span><span class='n'>Decimal</span><span class='p'>(</span><span class='s'>'1.0'</span><span class='p'>)</span> <span class='o'>></span> <span class='mf'>2.0</span>
<span class='go'>False</span>
<span class='gp'>>>> </span><span class='n'>Decimal</span><span class='p'>(</span><span class='s'>'3.0'</span><span class='p'>)</span> <span class='o'>></span> <span class='mf'>2.0</span>
<span class='go'>False</span>
<span class='gp'>>>> </span><span class='n'>Decimal</span><span class='p'>(</span><span class='s'>'1.0'</span><span class='p'>)</span> <span class='o'><</span> <span class='mf'>2.0</span>
<span class='go'>True</span>
<span class='gp'>>>> </span><span class='n'>Decimal</span><span class='p'>(</span><span class='s'>'3.0'</span><span class='p'>)</span> <span class='o'><</span> <span class='mf'>2.0</span>
<span class='go'>True</span>
<span class='gp'>>>> </span><span class='mf'>2.0</span> <span class='o'><</span> <span class='n'>Decimal</span><span class='p'>(</span><span class='s'>'1.0'</span><span class='p'>)</span>
<span class='go'>False</span>
<span class='gp'>>>> </span><span class='mf'>2.0</span> <span class='o'><</span> <span class='n'>Decimal</span><span class='p'>(</span><span class='s'>'3.0'</span><span class='p'>)</span>
<span class='go'>False</span>
<span class='gp'>>>> </span><span class='mf'>2.0</span> <span class='o'>></span> <span class='n'>Decimal</span><span class='p'>(</span><span class='s'>'1.0'</span><span class='p'>)</span>
<span class='go'>True</span>
<span class='gp'>>>> </span><span class='mf'>2.0</span> <span class='o'>></span> <span class='n'>Decimal</span><span class='p'>(</span><span class='s'>'3.0'</span><span class='p'>)</span>
<span class='go'>True</span>
</code></pre>
</div>
<p>But that’s not the end yet. I have <a href='http://nltk.sourceforge.net/'>NLTK</a> 0.9.4 installed, and watch how truth is reversed if I simply import the NLTK package:</p>
<div class='highlight'><pre><code class='pycon'><span class='gp'>>>> </span><span class='kn'>import</span> <span class='nn'>nltk</span>
<span class='gp'>>>> </span><span class='n'>Decimal</span><span class='p'>(</span><span class='s'>'1.0'</span><span class='p'>)</span> <span class='o'>></span> <span class='mf'>2.0</span>
<span class='go'>True</span>
<span class='gp'>>>> </span><span class='n'>Decimal</span><span class='p'>(</span><span class='s'>'3.0'</span><span class='p'>)</span> <span class='o'>></span> <span class='mf'>2.0</span>
<span class='go'>True</span>
<span class='gp'>>>> </span><span class='n'>Decimal</span><span class='p'>(</span><span class='s'>'1.0'</span><span class='p'>)</span> <span class='o'><</span> <span class='mf'>2.0</span>
<span class='go'>False</span>
<span class='gp'>>>> </span><span class='n'>Decimal</span><span class='p'>(</span><span class='s'>'3.0'</span><span class='p'>)</span> <span class='o'><</span> <span class='mf'>2.0</span>
<span class='go'>False</span>
<span class='gp'>>>> </span><span class='mf'>2.0</span> <span class='o'><</span> <span class='n'>Decimal</span><span class='p'>(</span><span class='s'>'1.0'</span><span class='p'>)</span>
<span class='go'>True</span>
<span class='gp'>>>> </span><span class='mf'>2.0</span> <span class='o'><</span> <span class='n'>Decimal</span><span class='p'>(</span><span class='s'>'3.0'</span><span class='p'>)</span>
<span class='go'>True</span>
<span class='gp'>>>> </span><span class='mf'>2.0</span> <span class='o'>></span> <span class='n'>Decimal</span><span class='p'>(</span><span class='s'>'1.0'</span><span class='p'>)</span>
<span class='go'>False</span>
<span class='gp'>>>> </span><span class='mf'>2.0</span> <span class='o'>></span> <span class='n'>Decimal</span><span class='p'>(</span><span class='s'>'3.0'</span><span class='p'>)</span>
<span class='go'>False</span>
</code></pre>
</div>
<p>I’m filing bugs about this, but I do find this quite entertaining in a ‘oh-my-God-I-always-believed-Python-was-a-well-behaved-language’ sort of way.</p>
<p><strong>Edit:</strong> Edward Loper from NLTK gave <a href='https://sourceforge.net/tracker/?func=detail&atid=400896&aid=2089412&group_id=30982'>an explanation</a> why this is the case – it’s not because of NLTK, but because of Python’s internal handling of the comparison operator on floating-point numbers:</p>
<blockquote>Apparently, you're only allowed to use comparison operators to compare
<code>Decimal</code> objects to (i) other
<code>Decimal</code> objects; (ii) integers; (iii) longs. Here, you're comparing it to a
<code>float</code>, which isn't allowed, as evidenced by the
following:</blockquote><div class='highlight'><pre><code class='pycon'><span class='gp'>>>> </span><span class='n'>Decimal</span><span class='p'>(</span><span class='s'>'.1'</span><span class='p'>)</span><span class='o'>.</span><span class='n'>__cmp__</span><span class='p'>(</span><span class='mi'>1</span><span class='p'>)</span>
<span class='go'>-1</span>
<span class='gp'>>>> </span><span class='n'>Decimal</span><span class='p'>(</span><span class='s'>'.1'</span><span class='p'>)</span><span class='o'>.</span><span class='n'>__cmp__</span><span class='p'>(</span><span class='o'>.</span><span class='mi'>3</span><span class='p'>)</span>
<span class='go'>NotImplemented</span>
</code></pre>
</div><blockquote><p>Since Decimal's
<code>__cmp__</code> method returns
<code>NotImplemented</code>, python falls back on using
<code>.3</code>'s
<code>__cmp__</code> method. Unfortunately, when you compare a
<code>float</code> to some random object, the results are pretty much arbitrary, and are not
guaranteed to be consistent. [...]</p>
<p>nltk's not really playing much of a role here (other than
tweaking the system to change that arbitrary result -- my guess would be that the result depends on
the pointer address of the
<code>Decimal</code> class, or of some other object like that).</p></blockquote>
<p>Crazy stuff.</p>
<div id="disqus_thread"></div>
</div>
<div id="sidebar">
<div id="carrington-subscribe" class="widget">
<h2 class="widget-title">Subscribe</h2>
<a class="feed" title="RSS 2.0 feed for posts" rel="alternate" href="http://feeds.feedburner.com/martinkl">
Site <acronym title="Really Simple Syndication">RSS</acronym> feed
</a>
<div id="mc_embed_signup">
<p>
Enjoyed this? To get notified when I write something new,
<a href="http://twitter.com/martinkl">follow me</a> on Twitter,
<a href="http://feeds.feedburner.com/martinkl">subscribe</a> to the RSS feed,
or type in your email address:
</p>
<form action="http://rapportive.us2.list-manage.com/subscribe/post?u=9a1adaf549282981a96e171d1&id=4543b695f6"
method="post" id="mc-embedded-subscribe-form" name="mc-embedded-subscribe-form" class="validate" target="_blank">
<fieldset>
<div class="mc-field-group">
<label for="mce-EMAIL">Email:</label>
<input type="text" value="" name="EMAIL" class="required email" id="mce-EMAIL">
<input type="submit" value="Subscribe" name="subscribe" id="mc-embedded-subscribe" class="btn">
</div>
<div id="mce-responses">
<div class="response" id="mce-error-response" style="display:none"></div>
<div class="response" id="mce-success-response" style="display:none"></div>
</div>
</fieldset>
</form>
<p class="disclaimer">
I won't give your address to anyone else, won't send you any spam, and you can unsubscribe at any time.
</p>
</div>
</div>
<div id="carrington-about" class="widget">
<div class="about">
<h2 class="title">About</h2>
<p>Hello! I'm Martin Kleppmann, entrepreneur and software craftsman.
I co-founded <a href="http://rapportive.com/">Rapportive</a>
(<a href="http://blog.rapportive.com/rapportive-acquired-by-linkedin">acquired</a>
by <a href="http://www.linkedin.com/">LinkedIn</a> in 2012) and Go Test It (acquired by
<a href="http://www.red-gate.com/">Red Gate Software</a> in 2009).</p>
<p>I care about making stuff that people want, great people and culture, the web and
its future, marvellous user experiences, maintainable code and scalable architectures.</p>
<p>I'd love to hear from you, so please leave comments, or feel free to
<a rel="author" href="/contact.html">contact me directly</a>.</p>
</div>
</div>
<div id="primary-sidebar">
</div>
<div id="secondary-sidebar">
<div id="carrington-archives" class="widget">
<h2 class="title">Recent posts</h2>
<ul>
<li>08 Oct 2012: <a href="/2012/10/08/complexity-of-user-experience.html">The complexity of user experience</a></li>
<li>01 Oct 2012: <a href="/2012/10/01/rethinking-caching-in-web-apps.html">Rethinking caching in web apps</a></li>
<li>18 Jun 2012: <a href="/2012/06/18/java-hashcode-unsafe-for-distributed-systems.html">Java's hashCode is not safe for distributed systems</a></li>
<li>16 Aug 2011: <a href="/2011/08/16/founderly-interview.html">My FounderLY interview</a></li>
<li>24 May 2011: <a href="/2011/05/24/evolution-of-rapportive-new-design.html">Evolution of Rapportive's new design</a></li>
<li><a href="/archive.html">Full archive</a></li>
</ul>
</div>
</div>
</div>
</div> <!-- div.wrapper, started in 'before.html' -->
<hr class="divider" />
<div id="footer">
<div class="wrapper">
<p id="generator-link">
<a rel="license" href="http://creativecommons.org/licenses/by/3.0/"
style="float: left; padding: 0.3em 1em 0 0;"><img alt="Creative Commons License"
src="http://i.creativecommons.org/l/by/3.0/88x31.png" /></a>
Unless otherwise specified, all content on this site is licensed under a
<a rel="license" href="http://creativecommons.org/licenses/by/3.0/">Creative Commons
Attribution 3.0 Unported License</a>.
Theme borrowed from
<span id="theme-link"><a href="http://carringtontheme.com" title="Carrington theme for WordPress">Carrington</a></span>,
ported to <a href="https://github.com/mojombo/jekyll">Jekyll</a> by Martin Kleppmann.
</p>
</div>
</div>
</div>
<script type="text/javascript">
var disqus_shortname = 'martinkl';
var disqus_url = 'http://martin.kleppmann.com/2008/09/02/comparing-floating-point-and-decimal-in-python.html';
var disqus_identifier = disqus_url;
(function() {
var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
dsq.src = 'http://' + disqus_shortname + '.disqus.com/embed.js';
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
})();
</script>
<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try {
var pageTracker = _gat._getTracker("UA-7958895-1");
pageTracker._trackPageview();
} catch (err) {}
</script>
</body>
</html>