473,543 Members | 1,908 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

floating point glitch


Is this only on solaris ?

Python 2.3.3 (#1, Mar 19 2004, 16:18:33)
[GCC 2.95.2 19991024 (release)] on sunos5
Type "help", "copyright" , "credits" or "license" for more information.
a=[66.6, 333, 333, 1, 1234.5]
print a.count(333), a.count(66.6), a.count('x') 2 1 0 a.append(333)
print a

[66.599999999999 994, 333, 333, 1, 1234.5, 333]

Jul 18 '05 #1
15 1566
David O'Farrell <Da************ @ericsson.com> writes:
Is this only on solaris ?


No, it's inherent in how floating point conversions work. It happens
on all IEEE 754 machines. You don't need that fancy list-append stuff
to see it happen. Just type
66.6

66.599999999999 994

Jul 18 '05 #2
In article <7x************ @ruckus.brouhah a.com>,
Paul Rubin <http://ph****@NOSPAM.i nvalid> wrote:
David O'Farrell <Da************ @ericsson.com> writes:
Is this only on solaris ?


No, it's inherent in how floating point conversions work. It happens
on all IEEE 754 machines. You don't need that fancy list-append stuff
to see it happen. Just type
66.6

66.59999999999 9994


Next time someone tries to Wikify the definitive response to these
FAQs (I don't feel up to it myself, this week), let me recommend
mention of "Computing over the Reals: Where Turing Meets Newton"
<URL: http://www.ams.org/notices/200409/fea-blum.pdf >.
Jul 18 '05 #3
David O'Farrell <Da************ @ericsson.com> wrote:

Is this only on solaris ?
Every IEEE754 processor, every language.
Python 2.3.3 (#1, Mar 19 2004, 16:18:33)
[GCC 2.95.2 19991024 (release)] on sunos5
Type "help", "copyright" , "credits" or "license" for more information.
a=[66.6, 333, 333, 1, 1234.5]
print a.count(333), a.count(66.6), a.count('x')2 1 0 a.append(333)
print a[66.599999999999 994, 333, 333, 1, 1234.5, 333]


This is a FAQ. The short answer is that 66.6 cannot be represented exactly
in binary. It is an infinitely repeating fraction. (1234.5 is not, which
is why the same thing didn't happen to it.)

When you use print, it calls repr() to get the string representation. repr
tells you the exact value, as close as possible. That value is as close as
you can get to 66.6 using a 64-bit IEEE754 float.

str() lies to you to make you happy:
str(a[0])

66.6
--
- Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
Jul 18 '05 #4
Tim Roberts wrote:
>a=[66.6, 333, 333, 1, 1234.5]
>print a.count(333), a.count(66.6), a.count('x')


2 1 0
>a.append(3 33)
>print a


[66.599999999999 994, 333, 333, 1, 1234.5, 333]


This is a FAQ. The short answer is that 66.6 cannot be represented exactly
in binary. It is an infinitely repeating fraction. (1234.5 is not, which
is why the same thing didn't happen to it.)

When you use print, it calls repr() to get the string representation.


Actually, print essentialy uses str() to get the string representation.
But repr(list) or str(list) still gets the repr() of each item of the
list rather than the str():
class TestObject(obje ct): .... def __str__(self):
.... return "<str() called>"
.... def __repr__(self):
.... return "<repr() called>"
.... t = TestObject()
str(t) '<str() called>' repr(t) '<repr() called>' print t <str() called> t <repr() called> l = [TestObject()]
str(l) '[<repr() called>]' repr(l) '[<repr() called>]' print l [<repr() called>] l [<repr() called>]

One way around this is to call str for each item yourself:
"[%s]" % ", ".join(map( str, l))

'[<str() called>]'

You could also define a list subclass that does this for you.
--
Michael Hoffman
Jul 18 '05 #5
Michael Hoffman <m.************ *************** ******@example. com> writes:
Actually, print essentialy uses str() to get the string
representation. But repr(list) or str(list) still gets the repr() of
each item of the list rather than the str():

print .66 0.66 print [.66] [0.6600000000000 0003]

Yucch! Also, str is not invertible:
a=.66
b=a+1e-16
a==b False str(a) '0.66' str(b)

'0.66'
Jul 18 '05 #6
Paul Rubin <http://ph****@NOSPAM.i nvalid> wrote:
Michael Hoffman <m.************ *************** ******@example. com> writes:
Actually, print essentialy uses str() to get the string
representation. But repr(list) or str(list) still gets the repr() of
each item of the list rather than the str():

>>> print .66 0.66 >>> print [.66]

[0.6600000000000 0003]

Yucch! Also, str is not invertible:


Right! That's the point. str() is the perfect solution in those cases
where you want the language to lie to you. In many cases, that IS what you
want. repr() is the perfect solution when you need an invertible function.

And that's really the lesson that needs to be taught when this FAQ is A-ed.
--
- Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
Jul 18 '05 #7
On Mon, 27 Sep 2004 23:21:12 -0700, Tim Roberts wrote:
Paul Rubin <http://ph****@NOSPAM.i nvalid> wrote:
Michael Hoffman <m.************ *************** ******@example. com> writes:
Actually, print essentialy uses str() to get the string
representation. But repr(list) or str(list) still gets the repr() of
each item of the list rather than the str():
>>> print .66

0.66
>>> print [.66]

[0.6600000000000 0003]

Yucch! Also, str is not invertible:

Right! That's the point. str() is the perfect solution in those cases
where you want the language to lie to you. In many cases, that IS what you
want. repr() is the perfect solution when you need an invertible function.


The "perfect solution" is either: (a) to print the minimal number of
digits that can maintain print/read consistency -- in this case, that
means printing "0.66", or (b) to print the actual, exact, decimal
representation of the value -- in this case, that means printing
"0.660000000000 000031086244689 504383131861686 70654296875"

And I think (a) is more perfect than (b) :-)
--
Malum est consilium quod mutari non potest -- Publilius Syrus

(setq reply-to
(concatenate 'string "Paul Foley " "<mycroft" '(#\@) "actrix.gen.nz> "))
Jul 18 '05 #8
Paul Foley <se*@below.inva lid> writes:
The "perfect solution" is either: (a) to print the minimal number of
digits that can maintain print/read consistency -- in this case, that
means printing "0.66",


Nah, you can do that by printing everything as zero.
Jul 18 '05 #9
>>>>> Paul Rubin <http://ph****@NOSPAM.i nvalid> (PR) wrote:

PR> Paul Foley <se*@below.inva lid> writes:
The "perfect solution" is either: (a) to print the minimal number of
digits that can maintain print/read consistency -- in this case, that
means printing "0.66",


PR> Nah, you can do that by printing everything as zero.

That would not maintain print/read consistency, i.e. if you read back the
printed value you don't get the original value.
--
Piet van Oostrum <pi**@cs.uu.n l>
URL: http://www.cs.uu.nl/~piet [PGP]
Private email: P.***********@h ccnet.nl
Jul 18 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

31
3621
by: JS | last post by:
We have the same floating point intensive C++ program that runs on Windows on Intel chip and on Sun Solaris on SPARC chips. The program reads the exactly the same input files on the two platforms. However, they generate slightly different results for floating point numbers. Are they really supposed to generate exactly the same results? I...
5
3728
by: Anton Noll | last post by:
We are using Visual Studio 2003.NET (C++) for the development of our software in the fields digital signal processing and numerical acoustics. One of our programs was working correctly if we are using the Debug-Version of the program, but it fails (or leads to false results) if we are using the Release-Version. After a long debugging...
12
2799
by: meltedown | last post by:
I would like the floating divs to float and then the header to come after them , on the left. That's what I thought clearing the floats was for, but in this example, the header is to the right of the floating divs, nothing is cleared. What am I doing wrong ? Here is the url:http://www.reenie.org/test/test5.php Here is the code: <html>...
687
22853
by: cody | last post by:
no this is no trollposting and please don't get it wrong but iam very curious why people still use C instead of other languages especially C++. i heard people say C++ is slower than C but i can't believe that. in pieces of the application where speed really matters you can still use "normal" functions or even static methods which is...
7
3378
by: Vinoth | last post by:
I'm working in an ARM (ARM9) system which does not have Floating point co-processor or Floating point libraries. But it does support long long int (64 bits). Can you provide some link that would discuss about ways to emulate floating point calculations with just long int or long long int. For eg., if i've a formula X=(1-b)*Y + b*Z in floating...
15
3901
by: michael.mcgarry | last post by:
Hi, I have a question about floating point precision in C. What is the minimum distinguishable difference between 2 floating point numbers? Does this differ for various computers? Is this the EPSILON? I know in float.h a FLT_EPSILON is defined to be 10^-5. Does this mean that the computer cannot distinguish between 2 numbers that...
4
2824
by: jacob navia | last post by:
Hi people I continue to work in the tutorial for lcc-win32, and started to try to explain the floating point flags. Here is the relevant part of the tutorial. Since it is a difficult part, I would like your expert advise before I publish any serious nonsense. Any comments are welcome, style, organization, hard errors, etc.
32
4050
by: ma740988 | last post by:
template <class T> inline bool isEqual( const T& a, const T& b, const T epsilon = std::numeric_limits<T>::epsilon() ) { const T diff = a - b; return ( diff <= epsilon ) && ( diff >= -epsilon ); } int main() { std::deque<double> pt ;
39
3524
by: rembremading | last post by:
Hi all! The following piece of code has (for me) completely unexpected behaviour. (I compile it with gcc-Version 4.0.3) Something goes wrong with the integer to float conversion. Maybe somebody out there understands what happens. Essentially, when I subtract the (double) function value GRID_POINT(2) from a variable which has been assigned...
0
7411
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7354
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7354
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
5888
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5282
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3394
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3394
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1824
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 we have to send another system
0
643
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.