Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Python

Decimal() instead of float?

 

 

First page Previous page 1 2 Next page Last page  View All Python python RSS feed   Index | Next | Previous | View Threaded


fd0man at gmail

Nov 11, 2006, 5:23 PM

Post #1 of 34 (410 views)
Permalink
Decimal() instead of float?

Is there a way to use Decimal() by default in Python instead of float?
I've no use for the float type, and I have some stuff that would require
Decimal(), but it is kind of a pain to try and cast things all over the
place all the time. Float is just way too inexact for me.

I am searching around, and I don't see anything helpful, but I could (as
always) be missing something... I tried (rather naĂŻvely) to just do
something like:

>>> import decimal
>>> float=Decimal
>>> x=1.1
>>> x
1.1000000000000001
>>>

But, that didn't work (obviously). It was a shot, anyway. Are there
any ideas, or does anyone have a way around this? I would prefer to not
have to convert incoming floating point numbers to strings and then
convert them to Decimal() types every single time that I want to use
them (which would be in *every* case). For example, I have a ZIP code
database that can do some processing on its numbers, and the numbers are
stored as floating point values (exactly) but Python doesn't get them
right; so the Decimal() thing would be needed. *shrugs*

Thanks a bunch,
Mike

--
Michael B. Trausch
fd0man [at] gmail
Phone: (404) 592-5746
Jabber IM: fd0man [at] livejournal

Demand Freedom! Use open and free protocols, standards, and software!


fredrik at pythonware

Nov 11, 2006, 5:31 PM

Post #2 of 34 (405 views)
Permalink
Re: Decimal() instead of float? [In reply to]

Michael B. Trausch wrote:

> Is there a way to use Decimal() by default in Python instead of float?

nope.

> For example, I have a ZIP code
> database that can do some processing on its numbers, and the numbers are
> stored as floating point values (exactly) but Python doesn't get them
> right

sounds odd. are you sure you don't mean "stored as strings containing
decimal numbers" ?

(who uses fractional ZIP codes, btw?)

</F>

--
http://mail.python.org/mailman/listinfo/python-list


steve at REMOVE

Nov 11, 2006, 6:08 PM

Post #3 of 34 (405 views)
Permalink
Re: Decimal() instead of float? [In reply to]

On Sun, 12 Nov 2006 02:31:04 +0100, Fredrik Lundh wrote:

>> For example, I have a ZIP code
>> database that can do some processing on its numbers, and the numbers are
>> stored as floating point values (exactly) but Python doesn't get them
>> right
>
> sounds odd. are you sure you don't mean "stored as strings containing
> decimal numbers" ?
>
> (who uses fractional ZIP codes, btw?)

Well, I can't speak for Americans, but here in Australia we typically give
our post codes to six decimal places:

Melbourne 3000.000000
Brunswick 3056.000000
Clifton Hill 3068.000000
Sydney 2000.000000
St Johns Park 2176.000000

and so forth. You can't have too much precision with those floating point
post/ZIP codes!


--
Steven.

--
http://mail.python.org/mailman/listinfo/python-list


sjmachin at lexicon

Nov 12, 2006, 1:14 AM

Post #4 of 34 (398 views)
Permalink
Re: Decimal() instead of float? [In reply to]

Steven D'Aprano wrote:
> On Sun, 12 Nov 2006 02:31:04 +0100, Fredrik Lundh wrote:
>
> >> For example, I have a ZIP code
> >> database that can do some processing on its numbers, and the numbers are
> >> stored as floating point values (exactly) but Python doesn't get them
> >> right
> >
> > sounds odd. are you sure you don't mean "stored as strings containing
> > decimal numbers" ?
> >
> > (who uses fractional ZIP codes, btw?)
>
> Well, I can't speak for Americans, but here in Australia we typically give
> our post codes to six decimal places:
>
> Melbourne 3000.000000
> Brunswick 3056.000000
> Clifton Hill 3068.000000
> Sydney 2000.000000
> St Johns Park 2176.000000
>
> and so forth. You can't have too much precision with those floating point
> post/ZIP codes!

Here in Austraila, (I expect this is common to most countries), there
are people who are utterly clueless about elementary data model rules,
like identification "numbers" should be kept as strings.

E.g. (1) National grief started over twenty years ago when the Post
Office started using postcodes with leading zeroes, and continues to
the present. The postcode for Darwin can be stored as 800, "800", or
"0800".

E.g. (2) Many Australians have a Tax File Number (TFN) which is a
9-digit number with an "officially kept secret" check digit algorithm
(you need to sign an NDA with the Tax Office). Storing this as an
integer allows the TFN be negative -- if the data entry for say
123456789 is actually 123456789-, you don't check for anything (length,
allowable characters, check digit) and an old-fashioned
trailing-minus-allowed conversion-to-integer routine is used.

Cheers,
John

--
http://mail.python.org/mailman/listinfo/python-list


bignose+hates-spam at benfinney

Nov 12, 2006, 1:38 AM

Post #5 of 34 (404 views)
Permalink
Re: Decimal() instead of float? [In reply to]

"Steven D'Aprano" <steve [at] REMOVE> writes:

> On Sun, 12 Nov 2006 02:31:04 +0100, Fredrik Lundh wrote:
> > (who uses fractional ZIP codes, btw?)
>
> Well, I can't speak for Americans, but here in Australia we
> typically give our post codes to six decimal places:
>
> Melbourne 3000.000000
> Brunswick 3056.000000
> Clifton Hill 3068.000000
> Sydney 2000.000000
> St Johns Park 2176.000000

Yeah, I know. As soon as that legislation came in, I started giving
the *precision* they asked for, with my own choice of *accuracy*, just
to mess with their damned totalitarian databases.

Melbourne 3000.000000
Brunswick 3000.000000
Clifton Hill 3000.000000

But you try to tell people overseas about this legislation, and they
just don't believe you.

--
\ "It is well to remember that the entire universe, with one |
`\ trifling exception, is composed of others." -- John Andrew |
_o__) Holmes |
Ben Finney

--
http://mail.python.org/mailman/listinfo/python-list


steve at REMOVE

Nov 12, 2006, 2:43 AM

Post #6 of 34 (396 views)
Permalink
Re: Decimal() instead of float? [In reply to]

On Sun, 12 Nov 2006 20:38:31 +1100, Ben Finney wrote:

> But you try to tell people overseas about this legislation, and they
> just don't believe you.

Ha! You were lucky. When I was a lad, we had to write our postcodes on
envelopes in balanced ternary.


--
Steven.

--
http://mail.python.org/mailman/listinfo/python-list


steve at holdenweb

Nov 13, 2006, 1:45 AM

Post #7 of 34 (395 views)
Permalink
Re: Decimal() instead of float? [In reply to]

Michael B. Trausch wrote:
> Is there a way to use Decimal() by default in Python instead of float?

No. You'll just have to be explicit.

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://holdenweb.blogspot.com
Recent Ramblings http://del.icio.us/steve.holden

--
http://mail.python.org/mailman/listinfo/python-list


steve at holdenweb

Nov 14, 2006, 9:08 AM

Post #8 of 34 (397 views)
Permalink
Re: Decimal() instead of float? [In reply to]

Michael B. Trausch wrote:
> On Mon, 2006-11-13 at 03:45 -0600, Steve Holden wrote:
>> Michael B. Trausch wrote:
>> > Is there a way to use Decimal() by default in Python instead of float?
>>
>> No. You'll just have to be explicit.
>>
>
> That's kinda what I figured. :-(
>
> It /would/ be nice to see Decimal() become the default. I cannot
> imagine why in an otherwise "human enough" language, math wouldn't be
> included in that without going out of one's way to do it. :-)
>
Speed has a lot to do with it. Have you timed some decimal operations
against their floating-point counterparts? It might be possible to build
a version of Python that used decimal instead of floating-point but it
certainly wouldn't be trivial: consider the use of C language libraries
that know nothing of Python's decimal representation.

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://holdenweb.blogspot.com
Recent Ramblings http://del.icio.us/steve.holden
--
http://mail.python.org/mailman/listinfo/python-list


fredrik at pythonware

Nov 15, 2006, 3:48 AM

Post #9 of 34 (393 views)
Permalink
Re: Decimal() instead of float? [In reply to]

Steve Holden wrote:

>> It /would/ be nice to see Decimal() become the default. I cannot
>> imagine why in an otherwise "human enough" language, math wouldn't be
>> included in that without going out of one's way to do it. :-)
>>
> Speed has a lot to do with it. Have you timed some decimal operations
> against their floating-point counterparts? It might be possible to build
> a version of Python that used decimal instead of floating-point but it
> certainly wouldn't be trivial: consider the use of C language libraries
> that know nothing of Python's decimal representation.

judging from the various decimal FAQ:s, I think it's a bit naive to
think that using Decimal instead of float would somehow make everything
"just work":

http://www2.hursley.ibm.com/decimal/decifaq.html
http://effbot.org/pylib/decimal.htm#decimal-faq

(btw, the OP mentioned in private mail that he wanted to store
geographical coordinates in decimal because floats kept messing things
up, but given that a 64-bit float can hold enough decimal digits to
represent a geographical coordinate with sub-millimeter precision on a
global scale, I'm not sure I buy that argument. I suspect he was just
tricked by the usual repr(1.15) != "1.15" issue. and seriously, under-
standing the various aspects of floats and decimals is utterly trivial
compared to all the nearly-magical things you need to understand to be
able to do geographical calculations at a sub-millimeter scale. heck,
even sub-kilometer stuff is pretty hard to get right ;-)

</F>

--
http://mail.python.org/mailman/listinfo/python-list


johnjsal at NOSPAMgmail

Nov 15, 2006, 1:12 PM

Post #10 of 34 (393 views)
Permalink
Re: Decimal() instead of float? [In reply to]

John Machin wrote:

> Here in Austraila, (I expect this is common to most countries), there
> are people who are utterly clueless about elementary data model rules,
> like identification "numbers" should be kept as strings.

Do you mean that ID numbers that serve as a primary key in a database
should also be strings?
--
http://mail.python.org/mailman/listinfo/python-list


tjreedy at udel

Nov 15, 2006, 1:57 PM

Post #11 of 34 (395 views)
Permalink
Re: Decimal() instead of float? [In reply to]

"John Salerno" <johnjsal [at] NOSPAMgmail> wrote in message
news:455b82a0$0$7054$c3e8da3 [at] news
> John Machin wrote:
>
>> Here in Austraila, (I expect this is common to most countries), there
>> are people who are utterly clueless about elementary data model rules,
>> like identification "numbers" should be kept as strings.
>
> Do you mean that ID numbers that serve as a primary key in a database
> should also be strings?

If you mean user-entered data like social security, phone, account, part,
or postal code 'numbers' -- as opposed to internal db-generated numbers
that the user never sees -- this I would presume 'yes'.

tjr



--
http://mail.python.org/mailman/listinfo/python-list


aahz at pythoncraft

Nov 15, 2006, 2:20 PM

Post #12 of 34 (396 views)
Permalink
Re: Decimal() instead of float? [In reply to]

In article <455b82a0$0$7054$c3e8da3 [at] news>,
John Salerno <johnjsal [at] NOSPAMgmail> wrote:
>John Machin wrote:
>>
>> Here in Austraila, (I expect this is common to most countries), there
>> are people who are utterly clueless about elementary data model rules,
>> like identification "numbers" should be kept as strings.
>
>Do you mean that ID numbers that serve as a primary key in a database
>should also be strings?

Depends. If they are strictly internal, ints are fine. But if they
interact with the outside world, they should be strings.
--
Aahz (aahz [at] pythoncraft) <*> http://www.pythoncraft.com/

"In many ways, it's a dull language, borrowing solid old concepts from
many other languages & styles: boring syntax, unsurprising semantics,
few automatic coercions, etc etc. But that's one of the things I like
about it." --Tim Peters on Python, 16 Sep 1993
--
http://mail.python.org/mailman/listinfo/python-list


sjmachin at lexicon

Nov 15, 2006, 6:30 PM

Post #13 of 34 (393 views)
Permalink
Re: Decimal() instead of float? [In reply to]

On 16/11/2006 8:57 AM, Terry Reedy wrote:
> "John Salerno" <johnjsal [at] NOSPAMgmail> wrote in message
> news:455b82a0$0$7054$c3e8da3 [at] news
>> John Machin wrote:
>>
>>> Here in Austraila, (I expect this is common to most countries), there
>>> are people who are utterly clueless about elementary data model rules,
>>> like identification "numbers" should be kept as strings.
>> Do you mean that ID numbers that serve as a primary key in a database
>> should also be strings?
>
> If you mean user-entered data like social security, phone, account, part,
> or postal code 'numbers' -- as opposed to internal db-generated numbers
> that the user never sees -- this I would presume 'yes'.
>
> tjr

Clarification:

A db-generated number like ROWID etc should be whatever the db makes it.

An identification "number" like social security number etc should be a
string, irrespective of (a) whether the user entered it or a script
entered it and (b) whether it's a primary key, foreign key or no key at all.

Some "numbers" contain characters outside [0-9]: e.g. ISBN (base 11, "X"
means 10); Hong Kong ID card "number" (base 36 in some positions).

Even if all bytes are in [0-9], a simple rule should be applied: Does it
make sense to add or subtract such "numbers"? The answer with (SSN,
phone number, account number, postal code) is no, so don't store it as a
numeric type.

Cheers,
John
--
http://mail.python.org/mailman/listinfo/python-list


mail at microcorp

Nov 15, 2006, 9:40 PM

Post #14 of 34 (393 views)
Permalink
Re: Decimal() instead of float? [In reply to]

"Fredrik Lundh" <fredrik [at] pythonware> wrote:

> ...... and seriously, under-
> standing the various aspects of floats and decimals is utterly trivial
> compared to all the nearly-magical things you need to understand to be
> able to do geographical calculations at a sub-millimeter scale. heck,
> even sub-kilometer stuff is pretty hard to get right ;-)

This is true - have you looked at that thing they call a geode? - horrible...

I can never understand why people grab for floats at the first opportunity.
To my simple mind, it seems better to work with a sub unit, and to stick to
integer arithmetic - if, as Steve said, there is a speed penalty for changing
from floats to decimal. then you can make even a cripple processor look
good by sticking to integers - unless you run out of precision...

- Hendrik

--
http://mail.python.org/mailman/listinfo/python-list


mail at microcorp

Nov 15, 2006, 10:11 PM

Post #15 of 34 (391 views)
Permalink
Re: Decimal() instead of float? [In reply to]

"John Salerno" <johnjsal [at] NOSPAMgmail> wrote:

> John Machin wrote:
>
> > Here in Austraila, (I expect this is common to most countries), there
> > are people who are utterly clueless about elementary data model rules,
> > like identification "numbers" should be kept as strings.
>
> Do you mean that ID numbers that serve as a primary key in a database
> should also be strings?

Nothing wrong with doing that - its not as if you are going to arithmetic with
them -
adding my id to yours is generally not very useful...

- Hendrik

--
http://mail.python.org/mailman/listinfo/python-list


fredrik at pythonware

Nov 15, 2006, 11:40 PM

Post #16 of 34 (392 views)
Permalink
Re: Decimal() instead of float? [In reply to]

Hendrik van Rooyen wrote:

> Nothing wrong with doing that - its not as if you are going to arithmetic with
> them - adding my id to yours is generally not very useful...

internal rowid's are best treated as pointers, though. they're more
like memory addresses than labels.

(from a design perspective, it's not entirely obvious that it's a good
idea to use rowid's to point *into* the database from the outside, but
that's another story).

</F>

--
http://mail.python.org/mailman/listinfo/python-list


steve at holdenweb

Nov 16, 2006, 2:37 AM

Post #17 of 34 (396 views)
Permalink
Re: Decimal() instead of float? [In reply to]

Terry Reedy wrote:
> "John Salerno" <johnjsal [at] NOSPAMgmail> wrote in message
> news:455b82a0$0$7054$c3e8da3 [at] news
>> John Machin wrote:
>>
>>> Here in Austraila, (I expect this is common to most countries), there
>>> are people who are utterly clueless about elementary data model rules,
>>> like identification "numbers" should be kept as strings.
>> Do you mean that ID numbers that serve as a primary key in a database
>> should also be strings?
>
> If you mean user-entered data like social security, phone, account, part,
> or postal code 'numbers' -- as opposed to internal db-generated numbers
> that the user never sees -- this I would presume 'yes'.
>
The modern trend is to use such values as alternate keys, and to have
all tables use an automatically-allocated integer (autoincrement,
identity, sequence) field as the primary key.

Unfortunately some applications are getting such large tables that a
32-bit field is insufficient to enumerate all existing and deleted rows.
Then you have to start keeping tables of unused primary keys.

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://holdenweb.blogspot.com
Recent Ramblings http://del.icio.us/steve.holden

--
http://mail.python.org/mailman/listinfo/python-list


"http://phr.cx" at NOSPAM

Nov 16, 2006, 2:44 AM

Post #18 of 34 (394 views)
Permalink
Re: Decimal() instead of float? [In reply to]

Steve Holden <steve [at] holdenweb> writes:
> Unfortunately some applications are getting such large tables that a
> 32-bit field is insufficient to enumerate all existing and deleted
> rows. Then you have to start keeping tables of unused primary keys.

Please tell me that's not from some Kafka nightmare. They don't use
64-bit ints?
--
http://mail.python.org/mailman/listinfo/python-list


steve at holdenweb

Nov 16, 2006, 6:07 AM

Post #19 of 34 (390 views)
Permalink
Re: Decimal() instead of float? [In reply to]

Paul Rubin wrote:
> Steve Holden <steve [at] holdenweb> writes:
>> Unfortunately some applications are getting such large tables that a
>> 32-bit field is insufficient to enumerate all existing and deleted
>> rows. Then you have to start keeping tables of unused primary keys.
>
> Please tell me that's not from some Kafka nightmare. They don't use
> 64-bit ints?

I don't believe SQL Server , for example, yet supports 64-bit identity
values. If it does, then the version that at least one Python user has
in use certainly doesn't.

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://holdenweb.blogspot.com
Recent Ramblings http://del.icio.us/steve.holden

--
http://mail.python.org/mailman/listinfo/python-list


gagsl-py at yahoo

Nov 16, 2006, 1:29 PM

Post #20 of 34 (395 views)
Permalink
Re: Decimal() instead of float? [In reply to]

At Thursday 16/11/2006 04:40, Fredrik Lundh wrote:

>internal rowid's are best treated as pointers, though. they're more
>like memory addresses than labels.
>
>(from a design perspective, it's not entirely obvious that it's a good
>idea to use rowid's to point *into* the database from the outside, but
>that's another story).

You could consider them like id() in python - an identifier that,
although it's in fact a pointer, should not be dereferenced.


--
Gabriel Genellina
Softlab SRL

__________________________________________________
Correo Yahoo!
Espacio para todos tus mensajes, antivirus y antispam ˇgratis!
ˇAbrí tu cuenta ya! - http://correo.yahoo.com.ar


fd0man at gmail

Nov 17, 2006, 12:10 PM

Post #21 of 34 (395 views)
Permalink
Re: Decimal() instead of float? [In reply to]

On Wed, 2006-11-15 at 12:48 +0100, Fredrik Lundh wrote:

> Steve Holden wrote:
>
> >> It /would/ be nice to see Decimal() become the default. I cannot
> >> imagine why in an otherwise "human enough" language, math wouldn't be
> >> included in that without going out of one's way to do it. :-)
> >>
> > Speed has a lot to do with it. Have you timed some decimal operations
> > against their floating-point counterparts? It might be possible to build
> > a version of Python that used decimal instead of floating-point but it
> > certainly wouldn't be trivial: consider the use of C language libraries
> > that know nothing of Python's decimal representation.
>
> judging from the various decimal FAQ:s, I think it's a bit naive to
> think that using Decimal instead of float would somehow make everything
> "just work":
>
> http://www2.hursley.ibm.com/decimal/decifaq.html
> http://effbot.org/pylib/decimal.htm#decimal-faq
>
> (btw, the OP mentioned in private mail that he wanted to store
> geographical coordinates in decimal because floats kept messing things
> up, but given that a 64-bit float can hold enough decimal digits to
> represent a geographical coordinate with sub-millimeter precision on a
> global scale, I'm not sure I buy that argument. I suspect he was just
> tricked by the usual repr(1.15) != "1.15" issue. and seriously, under-
> standing the various aspects of floats and decimals is utterly trivial
> compared to all the nearly-magical things you need to understand to be
> able to do geographical calculations at a sub-millimeter scale. heck,
> even sub-kilometer stuff is pretty hard to get right ;-)


I don't have (so far as I know) a 64-bit float available to me.

Some of the lat/long pairs that I have used seem to come out fine, but
some do not. Because the mathmatics used with them involve complex
equations when determining distance and the like, any error gets
massively compounded before a final result is evident. Thus, the
numbers must be exact. That's why I originally asked if Decimal() can
be used instead. Since it cannot, that's fine, I will just use
Decimal(), assuming that it supports everything that I will need to do
with the numbers. If not, then I guess I will have to look at something
that uses GMP.

-- Mike

--
Michael B. Trausch
fd0man [at] gmail
Phone: (404) 592-5746
Jabber IM: fd0man [at] livejournal

Demand Freedom! Use open and free protocols, standards, and software!


robert.kern at gmail

Nov 17, 2006, 12:21 PM

Post #22 of 34 (392 views)
Permalink
Re: Decimal() instead of float? [In reply to]

Michael B. Trausch wrote:
> I don't have (so far as I know) a 64-bit float available to me.

Yes, you do: the regular Python float type.

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco

--
http://mail.python.org/mailman/listinfo/python-list


fredrik at pythonware

Nov 17, 2006, 12:25 PM

Post #23 of 34 (389 views)
Permalink
Re: Decimal() instead of float? [In reply to]

Michael B. Trausch wrote:

> I don't have (so far as I know) a 64-bit float available to me.

as mentioned in the documentation, Python's "float" datatype is
implemented C doubles, which is 64-bit IEEE on all major platforms.

> Some of the lat/long pairs that I have used seem to come out fine, but
> some do not. Because the mathmatics used with them involve complex
> equations when determining distance and the like, any error gets
> massively compounded before a final result is evident.

sorry, but I don't think you have the slightest idea what you're doing,
really.

</F>

--
http://mail.python.org/mailman/listinfo/python-list


fd0man at gmail

Nov 17, 2006, 1:51 PM

Post #24 of 34 (391 views)
Permalink
Re: Decimal() instead of float? [In reply to]

On Fri, 2006-11-17 at 21:25 +0100, Fredrik Lundh wrote:

> > Some of the lat/long pairs that I have used seem to come out fine, but
> > some do not. Because the mathmatics used with them involve complex
> > equations when determining distance and the like, any error gets
> > massively compounded before a final result is evident.
>
> sorry, but I don't think you have the slightest idea what you're doing,
> really.
>


Sure, I do. Let's say that I want to work with the latitude 33.6907570.
In Python, that number can not be stored exactly without the aid of
decimal.Decimal().

>>> 33.6907570
33.690756999999998
>>>

As you can see, it loses accuracy after the 6th decimal place. That's
not good enough: I have 8 numbers that need to be exact, and I am only
getting six. That error will propagate throughout any multiplication or
division problem. The error compounds itself:

>>> x = 33.6907570
>>> x
33.690756999999998
>>> x*2
67.381513999999996
>>>

Now, observe what happens when you compute it exactly:

>>> from decimal import *
>>> xx = Decimal('33.6907570')
>>> xx
Decimal("33.6907570")
>>> xx*2
Decimal("67.3815140")

Now, observe what happens when you compare the differences
pre-multiplication and post-multiplication:

>>> x = Decimal('33.6907570')
>>> y = Decimal('33.690756999999998')
>>> x - y
Decimal("2E-15")
>>> x = Decimal('67.3815140')
>>> y = Decimal('67.381513999999996')
>>> x - y
Decimal("4E-15")
>>>

The discrepancy has carried over and been compounded—and this was a
simple multiplication, not something more advanced.

Now, while 4e-15 is a relatively small difference, it is a difference
that will continue to grow due to the inexact nature of the numbers.
This is proof enough that when exact precision is needed, the regular
floating point numbers should not be used. *I* need exact numbers—plain
and simple. Those are the requirements for the project I am working on,
and thus I must adhere to them—and they are non-negotiable, which is why
I had opened this thread to begin with. I wanted to know if there was a
way to simply switch the default mechanism, that's all. Quite simply,
we're talking about a project where the requirements are absolutely zero
tolerance for error—and it is clearly evident that Python cannot use a
float to store a simple latitude number without causing some error in
the precision. And it doesn't take a genius to realize that when you
introduce an error—even a minuscule one—into a long sequence of
equations, and those equations involve multiplication and division, the
error becomes compounded and unpredictable.

An example:

>>> 1.1
1.1000000000000001
>>> 1.1+0.1
1.2000000000000002
>>> 1.2
1.2
>>> 1.1*100000
110000.00000000001
>>>

This does not make any sense at all. Why is 1.1 == 1.1 and some change,
and 1.1 + 0.1 is off, but 1.2 is not off on its own? Or, for that
matter, why doesn't the default float type recognize that 1.1*100000 =
110000 without any more floating point? It is a whole number.

Perhaps you should not make assumptions; I am sure that you have heard
what they do at some point before. While *some* of the error doesn't
propagate as expected (which is actually a problem in itself—equations
no longer make sense if they are not mathematically balanced!) some
does. It is unpredictable and can't be tolerated when the numbers must
come out exactly.

— Mike

--
Michael B. Trausch
fd0man [at] gmail
Phone: (404) 592-5746
Jabber IM: fd0man [at] livejournal

Demand Freedom! Use open and free protocols, standards, and software!


carsten at uniqsys

Nov 17, 2006, 2:00 PM

Post #25 of 34 (392 views)
Permalink
Re: Decimal() instead of float? [In reply to]

On Fri, 2006-11-17 at 16:51 -0500, Michael B. Trausch wrote:
> [...]
> Let's say that I want to work with the latitude 33.6907570. In
> Python, that number can not be stored exactly without the aid of
> decimal.Decimal().
>
> >>> 33.6907570
> 33.690756999999998

You say that like it's Python's fault. Can this number be stored
*exactly* in C?

-Carsten


--
http://mail.python.org/mailman/listinfo/python-list

First page Previous page 1 2 Next page Last page  View All Python python RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.