472,956 Members | 2,563 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,956 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 1107
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
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
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
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
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
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
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
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
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
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
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
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...

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.