471,337 Members | 1,095 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,337 software developers and data experts.

puzzled about floats

from math import modf

class nco (object):
def __init__ (self, delta):
self.delta = delta
self.phase = 0.0
def __call__ (self):
self.phase += self.delta
f,i = modf (self.phase)
print modf (self.phase)
if (self.phase 1.0):
self.phase -= 1.0
return self.phase

n = nco (0.1)
for x in xrange (100):
print '%.12f' % n()

prints out
[...]
(0.99999999999999978, 0.0) <<< from modf
1.000000000000 << from n()

I'm baffled as to why 'print modf (self.phase)' prints out the first value,
but the result printed in the 2nd case is different. Without any precision
spec on the first print, an approximate float value was printed, but even
with %.12f, the second gives exactly 1.000....

Sep 24 '07 #1
1 1058
On Mon, 24 Sep 2007 14:13:18 -0400, Neal Becker wrote:
from math import modf

class nco (object):
def __init__ (self, delta):
self.delta = delta
self.phase = 0.0
def __call__ (self):
self.phase += self.delta
f,i = modf (self.phase)
print modf (self.phase)
if (self.phase 1.0):
self.phase -= 1.0
return self.phase

n = nco (0.1)
for x in xrange (100):
print '%.12f' % n()

prints out
[...]
(0.99999999999999978, 0.0) <<< from modf
1.000000000000 << from n()

I'm baffled as to why 'print modf (self.phase)' prints out the first value,
but the result printed in the 2nd case is different. Without any precision
spec on the first print, an approximate float value was printed, but even
with %.12f, the second gives exactly 1.000....
Tuples are printed with calling `repr()` on the objects:

In [144]: str(0.1)
Out[144]: '0.1'

In [145]: repr(0.1)
Out[145]: '0.10000000000000001'

In [146]: '%.12f' % 0.1
Out[146]: '0.100000000000'

In [147]: '%.50f' % 0.1
Out[147]: '0.10000000000000000555111512312578270211815834045 410'

Ciao,
Marc 'BlackJack' Rintsch
Sep 24 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by Tom | last post: by
8 posts views Thread by Madhusudan Singh | last post: by
11 posts views Thread by Steve | last post: by
13 posts views Thread by yb | last post: by
9 posts views Thread by Thomas Nelson | last post: by
16 posts views Thread by luca bertini | last post: by
1 post views Thread by donpro | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.