473,796 Members | 2,425 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Simple addition

Hi,

I tried a simple addition with python and I don't understand what is
going on:

$python
464.73+279. 78

744.50999999999 999

weird isn't it ? I use python2.3.

comments welcome
Mathieu
Jul 18 '05 #1
11 1676
Mathieu Malaterre wrote:
$python
>>464.73+279. 78 744.50999999999 999

weird isn't it ? I use python2.3.


Mathieu, you can find a full explanation of what you're seeing at the
following documentation link:
http://www.python.org/doc/current/tut/node15.html

From that page:

"Note that this is in the very nature of binary floating-point: this is
not a bug in Python, it is not a bug in your code either, and you'll see
the same kind of thing in all languages that support your hardware's
floating-point arithmetic (although some languages may not display the
difference by default, or in all output modes).

Python's builtin str() function produces only 12 significant digits, and
you may wish to use that instead. It's unusual for eval(str(x)) to
reproduce x, but the output may be more pleasant to look at:

print str(0.1)

0.1

It's important to realize that this is, in a real sense, an illusion:
the value in the machine is not exactly 1/10, you're simply rounding the
display of the true machine value."
Jul 18 '05 #2
Brian wrote:
Mathieu Malaterre wrote:
$python
>>464.73+279. 78

744.50999999999 999

weird isn't it ? I use python2.3.

Mathieu, you can find a full explanation of what you're seeing at the
following documentation link:
http://www.python.org/doc/current/tut/node15.html


Thanks a bunch Brian !
Jul 18 '05 #3
>>>>> Brian <py************ *************** **@invalid.net> (B) wrote:
print str(0.1)

B> 0.1

B> It's important to realize that this is, in a real sense, an illusion: the
B> value in the machine is not exactly 1/10, you're simply rounding the
B> display of the true machine value."

On the other hand, python could have done better. There are algorithms to
print floating point numbers properly with a more pleasant output[1]:
in this particular case python could have given "0.1" also with "print 0.1".
Unfortunately most C libraries only use the stupid algorithm which often
gives some useless digits.

This is because ideally it should print the representation with the least
number of digits that when read back gives the same internal value as the
number printed. In this case that is obviously "0.1".

[1] Guy L. Steele, Jr. Jon L. White, How to print floating-point numbers
accurately, Proceedings of the ACM SIGPLAN 1990 conference on Programming
language design and implementation, Pages: 112 - 126.
--
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 #4

"Piet van Oostrum" <pi**@cs.uu.n l> wrote in message
news:wz******** ****@cs.uu.nl.. .
>> Brian <py************ *************** **@invalid.net> (B) wrote: print str(0.1) B> 0.1

B> It's important to realize that this is, in a real sense, an illusion: the B> value in the machine is not exactly 1/10, you're simply rounding the
B> display of the true machine value."

On the other hand, python could have done better.
Python gives you a choice between most exact and 'pleasant'. This *is*
better, in my opinion, than no choice.
There are algorithms to
print floating point numbers properly with a more pleasant output[1]:
in this particular case python could have given "0.1" also with "print 0.1".

What? In 2.2:
print 0.1

0.1

did this change in 2.3?
Unfortunately most C libraries only use the stupid algorithm which often
gives some useless digits.
They are not useless if you want more accuracy about what you have and what
you will get with further computation. Tracking error expansion is an
important part of designing floating point calculations.
This is because ideally it should print the representation with the least
number of digits that when read back gives the same internal value as the
number printed. In this case that is obviously "0.1".


This is opinion, not fact. Opinions are divided.

Terry J. Reedy
Jul 18 '05 #5
Mathieu Malaterre <mm******@nycap .rr.com> wrote in message news:<LW******* ************@tw ister.nyroc.rr. com>...
Hi,

I tried a simple addition with python and I don't understand what is
going on:

$python
>>464.73+279. 78

744.50999999999 999


You computer does arithmetic in binary. None of these numbers can be
exactly represented as a binary fraction.

464.73 = bin 111010000.10 111010111000010 10001 111010111000010 10001...
279.78 = bin 100010111.1 100011110101110 00010 100011110101110 00010...

They get rounded to the nearest 53-bit float:

464.73 ~= 0x1.D0BAE147AE1 47p+8
279.78 ~= 0x1.17C7AE147AE 14p+8
--------------------
0x2.E8828F5C28F 5Bp+8
~= 0x1.744147AE147 AEp+9 after normalization

The exact decimal equivalent of this sum is
744.50999999999 999090505298227 071762084960937 5. Python's repr()
rounds this to 17 decimal digits, or "744.5099999999 9999".
Jul 18 '05 #6
>>>>> "Terry Reedy" <tj*****@udel.e du> (TR) wrote:

TR> "Piet van Oostrum" <pi**@cs.uu.n l> wrote in message
TR> news:wz******** ****@cs.uu.nl.. .
>>>>> Brian <py************ *************** **@invalid.net> (B) wrote:

>>>> print str(0.1) B> 0.1
B> It's important to realize that this is, in a real sense, an illusion:
TR> the
B> value in the machine is not exactly 1/10, you're simply rounding the
B> display of the true machine value."
On the other hand, python could have done better.
TR> Python gives you a choice between most exact and 'pleasant'. This *is*
TR> better, in my opinion, than no choice.

0.1000000000000 0001 is not more exact then 0.1. It is a false illusion of
exactness.
There are algorithms to
print floating point numbers properly with a more pleasant output[1]:
in this particular case python could have given "0.1" also with "print TR> 0.1".

TR> What? In 2.2: print 0.1

TR> 0.1

TR> did this change in 2.3?

Ok, mistake, I should have left out the print. But you should know what I
mean.
Unfortunately most C libraries only use the stupid algorithm which often
gives some useless digits.
TR> They are not useless if you want more accuracy about what you have and what
TR> you will get with further computation. Tracking error expansion is an
TR> important part of designing floating point calculations.
This is because ideally it should print the representation with the least
number of digits that when read back gives the same internal value as the
number printed. In this case that is obviously "0.1".


TR> This is opinion, not fact. Opinions are divided.

It would cause no errors and it would prevent a lot of the questions that
appear about every few days here about this subject. So what is the
advantage of printing 0.1000000000000 0001 or xx.xxx999999999 998?
--
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 #7
"Terry Reedy" <tj*****@udel.e du> wrote in message news:<3O******* *************@c omcast.com>...
"Piet van Oostrum" <pi**@cs.uu.n l> wrote in message
news:wz******** ****@cs.uu.nl.. .
>>>> Brian <py************ *************** **@invalid.net> (B) wrote: [repr(0.1) is ugly!]

Unfortunately most C libraries only use the stupid algorithm which often
gives some useless digits.


They are not useless if you want more accuracy about what you have


Why not display the *exact* decimal representation,
"0.100000000000 000005551115123 125782702118158 3404541015625"?
and what you will get with further computation.
Tracking error expansion is an
important part of designing floating point calculations.


We're talking about human-readable representation, not calculations.
Jul 18 '05 #8
Dan Bishop wrote:
Why not display the *exact* decimal representation,
"0.100000000000 000005551115123 125782702118158 3404541015625"?


This has my vote. Unfortunately Python seems incapable of figuring out all
of those digits.

Python 2.3.2 (#49, Oct 2 2003, 20:02:00) [MSC v.1200 32 bit (Intel)] on
win32
Type "help", "copyright" , "credits" or "license" for more information.
'%.64f' % 0.1

'0.100000000000 000010000000000 000000000000000 000000000000000 0000000'
--
Rainer Deyke - ra*****@eldwood .com - http://eldwood.com
Jul 18 '05 #9
> > > Unfortunately most C libraries only use the stupid algorithm which
often
gives some useless digits.


They are not useless if you want more accuracy about what you have


Why not display the *exact* decimal representation,
"0.100000000000 000005551115123 125782702118158 3404541015625"?


One can do better than that--or at least something I think is better.

Many moons ago, Guy Steele proposed an elegant pair of rules for converting
between decimal and internal floating-point, be it binary, decimal,
hexadecimal, or something else entirely:

1) Input (i.e. conversion from decimal to internal form) always yields
the closest (rounded) internal value to the given input.

2) Output (i.e. conversion from internal form to decimal) yields the
smallest number of significant digits that, when converted back to internal
form, yields exactly the same value.

This scheme is useful because, among other things, it ensures that all
numbers with only a few significant digits will convert to internal form and
back to decimal without change. For example, consider 0.1. Converting 0.1
to internal form yields the closest internal number to 0.1. Call that
number X. Then when we write X back out again, we *must* get 0.1, because
0.1 is surely the decimal number with the fewest significant digits that
yields X when converted.

I have suggested in the past that Python use these conversion rules. It
turns out that there are three strong arguments against it:

1) It would preclude using the native C library for conversions, and
would probably yield different results from C under some circumstances.

2) It is difficult to implement portably, and if it is not implemented
portably, it must be reimplemented for every platform.

3) It potentially requires unbounded-precision arithmetic to do the
conversions, although a clever implementation can avoid it most of the time.

I still think it would be a good idea, but I can see that it would be more
work than is feasible. I don't want to do the work myself, anyway :-)
Jul 18 '05 #10

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

Similar topics

3
1302
by: Gavin Bauer | last post by:
My DOS window (running in windows ME) closes the second it finishes running my programs. As you can imagine, this makes it hard to see the results. I've gotten in the habit of putting raw_input("Press enter to exit") at the end of every program, and in addition to being pain in the butt, it often fails to work. Being new to programming in general, I make more mistakes than most people. My programs often have errors before they get to my...
19
2989
by: Dales | last post by:
I have a custom control that builds what we refer to as "Formlets" around some content in a page. These are basically content "wrapper" sections that are tables that have a colored header and provide an open TD with a DIV in it for the content of this formlet. (The DIV is for DHTML to hide and show the content) I've created a web page showing step by step the two problems I'm encountering. This problem is much easier to see than it...
8
4253
by: rdavis7408 | last post by:
I am attempting what I would think would be a simple calculation of the cost of traveling a single mile. But I can not figure this out. The following is my script. Any help would be appreciated. I want the user to enter the price per gallon in one textbox, the miles per gallon in the next textbox and then press the button and get the cost per gallon by dividing the price per gallon by the miles per gallon.
4
2663
by: Ranginald | last post by:
Sorry for the simple question but thanks in advance: My goal is to create reusale code for a web app in C#. I have written the code already as a windows app but here is where I am confused: To keep it really easy let's say this is the code: function addition(int, int); int X;
13
2045
by: aum | last post by:
Hi, I'm a Python programmer, just starting to get into javascript. On reading some of the js guides, and not liking any of the OO usage patterns I saw, I've cooked up something which python folks might find to taste. Here's the code - first the 'engine', then some code demonstrating the usage patterns. For me (and maybe for some of you), it promotes readability and some
5
4596
by: Mike | last post by:
Hello All, Please, if anyone can point me to the problem, I'd sure appreciate it! I am very new to VB programming and not a programmer to begin with. This is part of a Visual Basic 2005 Express Edition program to control a remote basketball scoreboard display unit. All I'm trying to do is add 5 byte variables and store the result in an integer variable. I added a Try/Catch block to take look at things. This exception occurs only when...
1
2942
by: Synapse | last post by:
Hello... We were asked to create a simple calculator program in our C++ subject by using loops only. i have a problem in creating a loop in the multiplication and division operation so please can anyone help me on this please. and also during the operation selection, if ill enter a character it wont go back to the main program. by the way, my compiler is Dev-C++. I need help badly..here's my code below... #include <iostream.h> #include...
5
1524
by: wazzup | last post by:
My task is Create a program to read a simple integer expression and display the result. "Simple" means no parentheses are allowed and only the +, -, *, and / operators are allowed. I think expression would be Multiplications, then Division, then Addition and Subtraction. My program gave me a wrong result after addition, take a look to my code you will understand. Please help me to develop this. Here is my Code
2
7638
by: aagase29 | last post by:
The code below accepts two numbers from textboxes and should show the addition in the third one.I can t figure out how to display addition in the third text box... <html> <body> <form action="addition_n.asp" method="post"> First number:&nbsp;&nbsp;&nbsp;&nbsp; <input type="text" name="no1" size="20" /> &nbsp; <p> Second number: <input type="text" name="no2" size="20" value="5" /> &nbsp;</p>
0
9680
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10455
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10228
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10173
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 most users, this new feature is actually very convenient. If you want to control the update process,...
1
7547
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5573
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4116
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
2
3731
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2925
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.