473,505 Members | 14,618 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1125
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
2459
by: Tom | last post by:
Has anyone ever seen a IComparer for floats the returns magnitude. i.e. instead of returning -1, it would return -5. To let you know HOW different the two numbers are. obviously for int it is a -...
8
4861
by: Madhusudan Singh | last post by:
Is it possible to convert a very long list of strings to a list of floats in a single statement ? I have tried float(x) and float(x) but neither work. I guess I would have to write a loop if...
3
1622
by: freelanceinaz | last post by:
My problem page is at http://girlschorus.org/test.html I have a container with a relatively positioned graphic at the top, then two floats which are relatively positioned (for a a two-column...
11
18521
by: Steve | last post by:
I'm trying to create a list range of floats and running into problems. I've been trying something like: a = 0.0 b = 10.0 flts = range(a, b) fltlst.append(flts)
13
2319
by: yb | last post by:
Hi, Is there a CSS method to clear a float such that it aligns with the left content edge. For example: X X X X X X X X
9
1740
by: Thomas Nelson | last post by:
I want to generate all the fractions between 1 and limit (with limit>1) in an orderly fashion, without duplicates. def all_ratios(limit): s = set() hi = 1.0 lo = 1.0 while True: if hi/lo not...
16
4113
by: luca bertini | last post by:
Hi, i have strings which look like money values (ie 34.45) is there a way to convert them into float variables? everytime i try I get this error: "numb = float(my_line) ValueError: empty string...
1
2029
by: donpro | last post by:
https://testbed.odysseyshipping.com/index.php This is driving me nuts. I've spent much time trying to style this page footer but because I cannot set widths using "display: inline". I've tried...
1
1827
by: efittery | last post by:
I need to modify the following code to handle a vector of pairs of floats. I was just thinking of casting my vector of floats into being a vector of pairs of floats. Alternately, I have...
0
1473
by: Matthieu Brucher | last post by:
2008/11/5 L V <somelauw@yahoo.com>: Hi, I don't think the Python developers list is th best list to post this kind of question. What version of Python did you use for this test? Matthieu
0
7216
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7303
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7018
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5613
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5028
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4699
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3187
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1528
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
754
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.