473,795 Members | 2,861 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Why the nonsense number appears?

Hi,
Pls take a look at this code:

----------
t1 = "1130748744 "
t2 = "461"
t3 = "1130748744 "
t4 = "500"
time1 = t1+"."+t2
time2 = t3+"."+t4
print time1, time2 1130748744.461 1130748744.500 float(time2) - float(time1) 0.0390000343322 75391


Why are there so many nonsense tails? thanks for your help.

Regards,
Johnny

Oct 31 '05 #1
9 1502
Johnny Lee enlightened us with:
Why are there so many nonsense tails? thanks for your help.


Because if the same reason you can't write 1/3 in decimal:

http://docs.python.org/tut/node16.html

Sybren
--
The problem with the world is stupidity. Not saying there should be a
capital punishment for stupidity, but why don't we just take the
safety labels off of everything and let the problem solve itself?
Frank Zappa
Oct 31 '05 #2
Johnny Lee wrote:
<snip>
Why are there so many nonsense tails? thanks for your help.


I guess you were expecting 0.039? You first need to understand floating
point numbers:

http://docs.python.org/tut/node16.html

What you see are the effects of representation errors.

The solution is presented here:
http://www.python.org/peps/pep-0327.html

But briefly, it means your code should read:

from decimal import *
t1 = "1130748744 "
t2 = "461"
t3 = "1130748744 "
t4 = "500"
time1 = t1+"."+t2
time2 = t3+"."+t4
print time1, time2
Decimal(time2) - Decimal(time1)
Oct 31 '05 #3
Johnny Lee wrote:
print time1, time2
1130748744.461 1130748744.500
float(time2 ) - float(time1)


0.0390000343322 75391
Why are there so many nonsense tails? thanks for your help.


http://en.wikipedia.org/wiki/Floatin...floating-point,
especially 'Rounding'. Or google for "gloating point precision" if you
need more details.

Daniel
Oct 31 '05 #4
On Mon, October 31, 2005 9:39, Sybren Stuvel said:
Johnny Lee enlightened us with:
Why are there so many nonsense tails? thanks for your help.


Because if the same reason you can't write 1/3 in decimal:

http://docs.python.org/tut/node16.html

Sybren
--
The problem with the world is stupidity. Not saying there should be a
capital punishment for stupidity, but why don't we just take the
safety labels off of everything and let the problem solve itself?
Frank Zappa
--
http://mail.python.org/mailman/listinfo/python-list

I think that the previous poster was asking something different. I think
he was asking something like this:

If
t1 = 0.500
t2 = 0.461
print t1-t2 0.039

Then why:
t1 += 12345678910
t2 += 12345678910
# Note, both t1 and t2 have been incremented by the same amount.
print t1-t2

0.0389995574951

It appears Yu-Xi Lim beat me to the punch. Using decimal as opposed to
float sorts out this error as floats are not built to handle the size of
number used here.

Ben

Oct 31 '05 #5
Ben O'Steen enlightened us with:
I think that the previous poster was asking something different.
It all boils down to floating point inprecision.
If
t1 = 0.500
t2 = 0.461
print t1-t2 0.039

Then why:
t1 += 12345678910
t2 += 12345678910
# Note, both t1 and t2 have been incremented by the same amount.
print t1-t2
0.0389995574951


It's easier to explain in decimals. Just assume you only have memory
to keep three decimals. 12345678910.500 is internally stored as
something like 1.23456789105e1 0. Strip that to three decimals, and you
have 1.234e10. In that case, t1 - t2 = 1.234e10 - 1.234e10 = 0.
Using decimal as opposed to float sorts out this error as floats are
not built to handle the size of number used here.


They can handle the size just fine. What they can't handle is 1/1000th
precision when using numbers in the order of 1e10.

Sybren
--
The problem with the world is stupidity. Not saying there should be a
capital punishment for stupidity, but why don't we just take the
safety labels off of everything and let the problem solve itself?
Frank Zappa
Oct 31 '05 #6
On Mon, October 31, 2005 10:23, Sybren Stuvel said:
Ben O'Steen enlightened us with:
Using decimal as opposed to float sorts out this error as floats are
not built to handle the size of number used here.
They can handle the size just fine. What they can't handle is 1/1000th
precision when using numbers in the order of 1e10.


I used the word 'size' here incorrectly, I intended to mean 'length'
rather than numerical value. Sorry for the confusion :)
Sybren
--
The problem with the world is stupidity. Not saying there should be a
capital punishment for stupidity, but why don't we just take the
safety labels off of everything and let the problem solve itself?
Frank Zappa
--
http://mail.python.org/mailman/listinfo/python-list

Oct 31 '05 #7
Ben O'Steen wrote:
On Mon, October 31, 2005 10:23, Sybren Stuvel said:
Ben O'Steen enlightened us with:
Using decimal as opposed to float sorts out this error as floats are
not built to handle the size of number used here.

They can handle the size just fine. What they can't handle is 1/1000th
precision when using numbers in the order of 1e10.


I used the word 'size' here incorrectly, I intended to mean 'length'
rather than numerical value. Sorry for the confusion :)


Sybren is right. The problem is not the length or the size, it's
the fact that 0.039 cannot be represented exactly in binary, in
just the same way that 1/3 cannot be represented exactly in
decimal. They both give recurring numbers. If you truncate those
recurring numbers to a finite number of digits, you lose
precision. And this shows up when you convert the inaccurate
number from binary into decimal representation where an exact
representation IS possible.

Steve
Oct 31 '05 #8
Steve Horsley wrote:
Ben O'Steen wrote:
On Mon, October 31, 2005 10:23, Sybren Stuvel said:
Ben O'Steen enlightened us with:
Using decimal as opposed to float sorts out this error as floats are
not built to handle the size of number used here.
They can handle the size just fine. What they can't handle is 1/1000th
precision when using numbers in the order of 1e10.


I used the word 'size' here incorrectly, I intended to mean 'length'
rather than numerical value. Sorry for the confusion :)


Sybren is right. The problem is not the length or the size, it's
the fact that 0.039 cannot be represented exactly in binary, in
just the same way that 1/3 cannot be represented exactly in
decimal. They both give recurring numbers. If you truncate those
recurring numbers to a finite number of digits, you lose
precision. And this shows up when you convert the inaccurate
number from binary into decimal representation where an exact
representation IS possible.


That's A source of error, but it's only part of the story. The
double-precision binary representation of 0.039 is 562049233495837 9 *
2**(-57), which is in error by 1/180143985094819 84000. By contrast,
Johnny Lee's answer is in error by 9/262144000, which is more than 618
billion times the error of simply representing 0.039 in floating point
-- a loss of 39 bits.

The problem here is catastrophic cancellation.

1130748744.500 ~= 474270398205132 8 * 2**(-22)
1130748744.461 ~= 474270398188775 0 * 2**(-22)

Subtracting gives 163578 * 2**(-22), which has only 18 significant bits.

Nov 1 '05 #9
Dan Bishop wrote:
That's A source of error, but it's only part of the story. The
double-precision binary representation of 0.039 is 562049233495837 9 *
2**(-57), which is in error by 1/180143985094819 84000. By contrast,
Johnny Lee's answer is in error by 9/262144000, which is more than 618
billion times the error of simply representing 0.039 in floating point
-- a loss of 39 bits.

The problem here is catastrophic cancellation.

1130748744.500 ~= 474270398205132 8 * 2**(-22)
1130748744.461 ~= 474270398188775 0 * 2**(-22)

Subtracting gives 163578 * 2**(-22), which has only 18 significant bits.


Hmm. Good point.

Nov 1 '05 #10

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

Similar topics

5
19233
by: joemono | last post by:
Hello everyone! First, I appologize if this posting isn't proper "netiquette" for this group. I've been working with perl for almost 2 years now. However, my regular expression knowledge is pretty limited. I wrote the following expression to take (hopefully) any _reasonable_ phone number input, and format it as (999) 999-9999 x 9999.
9
1776
by: martin | last post by:
Hi, a very newbie question. How do I split the adress and number to 2 variables? ex. "Kingsroad 347" = variabel1 = "Kingsroad" variabel2 = "347" Ill guess i have to search the string from left to right after first apperence of a number, but how do i do that? Regards Martin
29
9135
by: james | last post by:
I have a problem that at first glance seems not that hard to figure out. But, so far, the answer has escaped me. I have an old database file that has the date(s) stored in it as number of days. An example is: 36,525 represents 01/01/1900. The starting point date is considered to be : 00/00/0000. I have looked thru Help and used Google and have not really found an answer. I know that Leap Years need to be accounted for too. Any...
1
3679
by: sunilkeswani | last post by:
Hi I am still new to access. I want to know how i can build a query which can display results from 4 different columns/fields Like. Field1 Field2 Field3 Field4 1 2 1 0 1 1 0 0
4
5020
by: viuxrluxvbbc | last post by:
Hi im trying to write a program that will read in numbers and display them in ascending order along with a count of how many times it repeats. i got the numerical order portion done but cant figure out the other part. i keep on getting a wrong number please help #include <iostream> using namespace std; int main() {
16
10122
by: usenet | last post by:
I want to be able to use the record numbers of a sub-form, are they available anyhow in VB (Access 2003)? I want to use the sub-form record number as *part* of the primary key for the table whose data is displayed on the sub-form. The other part of the primary key is the column used to join to the master table. E.g. I have TableA (the master table) with a primary key TableA.Column1. TableB which is shown in the sub-form has a primary...
2
2196
by: Simon | last post by:
I had an access database that i use as an ordering system. I have a form for entering customer details. When i add a new customer on the form the customer number is an auto number that appears when i type in the details. I have just moved over to mysql server with access as the front end. I have setup the sql tables with the customer number as autonumber. When i go into the form and add a new customer it does not generate the
4
2630
by: Horacius ReX | last post by:
Hi, I have to read some data from a file, and on each block it always appears the followng string; xyz.vs.1-81_1 . It appears a lot of time with different numbers like; xyz.vs.1-81_1 xyz.vs.1-1234_1 xyz.vs.1-56431_1
23
9804
by: neha_chhatre | last post by:
which is the best format specifier(data type) if i have to work with decimal number. also please tell me the syntax for truncating a decimal number please reply as soon as possible
0
9673
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
10448
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
10217
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
10167
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
7544
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
6784
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5440
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4114
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
3
2922
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.