473,326 Members | 2,192 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,326 software developers and data experts.

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.039000034332275391


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

Regards,
Johnny

Oct 31 '05 #1
9 1489
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.039000034332275391
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.23456789105e10. 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 5620492334958379 *
2**(-57), which is in error by 1/18014398509481984000. 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 ~= 4742703982051328 * 2**(-22)
1130748744.461 ~= 4742703981887750 * 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 5620492334958379 *
2**(-57), which is in error by 1/18014398509481984000. 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 ~= 4742703982051328 * 2**(-22)
1130748744.461 ~= 4742703981887750 * 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
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...
9
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...
29
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...
1
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 ...
4
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...
16
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...
2
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...
4
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...
23
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
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.