[Python-Dev] Re: Decimal type question [Prothon] (original) (raw)
Michael Chermside mcherm at mcherm.com
Wed Aug 11 16🔞47 CEST 2004
- Previous message: [Python-Dev] Multi-line import implementation (was: 2.4a2, and @decorators)
- Next message: [Python-Dev] Re: Re: Decimal type question [Prothon]
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Mark Hahn writes:
I am more interested now in hearing about people's feelings on the idea of having the Decimal type "in-between" Int and Float. Assume a decent Decimal implementation is used.
All right. I think that "in-between" Int and Float is a bad idea -- or at least that it's a bad way to think about a decent design.
"in-between" suggests that Float has greater range than Decimal which has greater range than Int... but in your design that simply isn't true. Your Int, for instance, can represent values that neither Decimal nor Float can handle.
So the "right" way (IMHO) to think about it is as three different classes of numbers. There are Ints (can only handle integers, but able to manage any of those), Decimals (can only handle certain rational numbers but it happens to be those that humans often find useful; also has an upper bound and a precision limit (which may vary with size depending on what implementation you use)), and Floats (can only handle certain rational numbers most of which are NOT the same as those handled by Decimal; also has upper bounds and precision limits; runs real fast).
I would recomend that you AVOID "automatic" conversion (just like Python does) but that you make sure that when a (pure) number can be represented in two ways that they compare equal. So the following would be true: Int(1) == Decimal(1.0) == Float(1.e0) Decimal(2.5) == Float(2.5e0)
This is pretty much what you are already doing... for instance, Int(1) / Int(3) ---> Decimal(0.3333333333333333) because there is a "/" operator which accepts two Ints or Decimals and returns a Decimal. I'd propose that there's also another "/" operator which accepts two Floats and returns a Float. And that passing one Float and one (Int or Decimal) to "/" results in a type error. The cos() function would accept one number (of any type) and return a Float.
There's also the question (and it's an important one!) of what literals to use. I agree (and everyone else would too) that literals like "1234" should create Ints. I also agree (as would everyone else) that literals like "123.45e6" should create Floats. The contraversial bit is whether literals like "123.45" should create Decimals... but I think that they should. My reasons are (1) for newbies, matching what people expect is more important than speed... and that's exactly why Decimal is better than Float, and (2) if not, then what form WOULD you use for Decimal literals? (I suppose there's always the trailing "d".)
- Previous message: [Python-Dev] Multi-line import implementation (was: 2.4a2, and @decorators)
- Next message: [Python-Dev] Re: Re: Decimal type question [Prothon]
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]