473,396 Members | 1,996 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Problem with floating point precision

I checked the docs, but found nothing about floating point issues.

I am working on an app that deals with currency values, and only does
addition and subtraction (no division). But I am starting to see
values that should be the same failing a compare! What is the
workaround or correct way to compare 2 floats? It worked fine for
months, then suddenly starting acting odd. I might have upgraded
Python somewhere in between, I can't recall.

jonathon
Jul 18 '05 #1
5 1773
"j_mckitrick" <j_*********@bigfoot.com> wrote in message
news:ec**************************@posting.google.c om...
I checked the docs, but found nothing about floating point issues.

I am working on an app that deals with currency values, and only does
addition and subtraction (no division). But I am starting to see
values that should be the same failing a compare! What is the
workaround or correct way to compare 2 floats? It worked fine for
months, then suddenly starting acting odd. I might have upgraded
Python somewhere in between, I can't recall.
The basic issue here is that you shouldn't be using floats
for currency. Granted, lots of people do, but it will eventually
run into exactly the problem you're describing.

The reason is that currency is best handled by some form of
arithmetic that works with discrete quantities, while floats are
intended to be used with continuous quantities (like scientific
and engineering measurements).

There's a floating decimal package in 2.4 that is intended
to eventually (in 2.5 maybe?) be the base of a currency
package. However, that doesn't help today.

What I'd do is create a currency class that uses plain
old, boring ints as the base, with the necessary decimal
point handling present behind the scene.

John Roth


jonathon

Jul 18 '05 #2
j_*********@bigfoot.com (j_mckitrick) writes:
I checked the docs, but found nothing about floating point issues.


See the appendix "Floating Point Arithmetic: Issues and Limitations"
in the Python Tutorial for a discussion of this topic.

--
Brian Gough

Network Theory Ltd,
Publishing the Python Manuals --- http://www.network-theory.co.uk/
Jul 18 '05 #3
[John Roth]
...
There's a floating decimal package in 2.4 that is intended
to eventually (in 2.5 maybe?) be the base of a currency
package. However, that doesn't help today.


It will help in 2.4, though. Fixed-point decimal is a subset of what
2.4's decimal arithmetic can do, and it's dead easy to do exact
"dollars and cents" calculations using it. I'm not sure what a
currency package would add on top of it -- maybe add currency markers
to string output, perhaps offer a simplified rounding discipline for
multiplication, not really sure. The "hard parts" will already be in
2.4.
Jul 18 '05 #4
On Tue, 17 Aug 2004 12:19:50 -0400, "John Roth"
<ne********@jhrothjr.com> wrote:
"j_mckitrick" <j_*********@bigfoot.com> wrote in message
news:ec**************************@posting.google. com...
I checked the docs, but found nothing about floating point issues.

I am working on an app that deals with currency values, and only does
addition and subtraction (no division). But I am starting to see
values that should be the same failing a compare! What is the
workaround or correct way to compare 2 floats? It worked fine for
months, then suddenly starting acting odd. I might have upgraded
Python somewhere in between, I can't recall.


The basic issue here is that you shouldn't be using floats
for currency. Granted, lots of people do, but it will eventually
run into exactly the problem you're describing.

The reason is that currency is best handled by some form of
arithmetic that works with discrete quantities, while floats are
intended to be used with continuous quantities (like scientific
and engineering measurements).

There's a floating decimal package in 2.4 that is intended
to eventually (in 2.5 maybe?) be the base of a currency
package. However, that doesn't help today.


@$#,###.##
import decimal

I believe wil be the syntax for getting at it ;)

Art
Jul 18 '05 #5
j_*********@bigfoot.com (j_mckitrick) wrote:
I checked the docs, but found nothing about floating point issues.

I am working on an app that deals with currency values, and only does
addition and subtraction (no division). But I am starting to see
values that should be the same failing a compare! What is the
workaround or correct way to compare 2 floats?


A full analysis of floating point math would fill a book. The details
depend greatly on the underlying FP hardware in your system, although
these days it's pretty hard to find a machine which doesn't use IEEE 754
standard math, so you might start by googling for "ieee 754".

Python 2.4 will have a new Decimal numeric type, which was invented to
resolve many of the problems that happen when trying to use floats for
currency calculations. Take a look at
http://www.python.org/peps/pep-0327.html for more information.

There is an alpha version of 2.4 that was released recently; you might
want to download that and start playing around with the Decimal type to
see if that solves your problem. I'm sure other people here could give
a better estimate of when there will be a production release of 2.4, but
my guess is that it's a few months away.
Jul 18 '05 #6

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

Similar topics

31
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....
687
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...
21
by: syntax | last post by:
hi, i need to get high presion float numbers. say, i need pi = 22/7.0 = 3.142857....(upto 80 digits) is it possible ? does gcc/g++ compiler can give such type of high precision?? plz...
1
by: Shreyas Kulkarni | last post by:
hi there, recently i have got a problem regarding calculation of sum of digits in a floating point or precision number. the weird behaviour of compiler/language is preventing me from calculating...
38
by: baong | last post by:
dear all i have use this line to time in many my web base appl. but now i found a weird problem the javascript line is the same but i use the calculation 2 time @ the first time, it is ok, 3 *...
10
by: Bryan Parkoff | last post by:
The guideline says to use %f in printf() function using the keyword float and double. For example float a = 1.2345; double b = 5.166666667; printf("%.2f\n %f\n", a, b);
6
by: graham.macpherson | last post by:
I have 2 Suse Linux PCs which I compile my code on. Until recently they both had gcc 4.0.X on them, but I upgraded one of them to gcc 4.1.0. I have come across a very strange problem in the...
5
by: rocknbil | last post by:
Hello everyone! I'm new here but have been programming for the web in various languages for 15 years or so. I'm certainly no "expert" but can keep myself out of trouble (or in it?) most of the time....
137
by: mathieu.dutour | last post by:
Dear all, I want to do multiprecision floating point, i.e. I want to go beyond single precision, double precision and have quadruple precision, octuple precision and the like, and possibly with...
39
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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
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,...

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.