Decimal vs Float comparasion

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Yuan HOng

    Decimal vs Float comparasion

    Hi,

    It seems decimal object will always be larger than float in
    comparasion, which goes against common sense:
    >>from decimal import Decimal
    >>a = Decimal('0.5')
    >>a 99999
    False
    >>a 99999.0
    True

    It seems to me that rather than allowing this to happen, comparasion
    between the two should either be made correct (by convertion decimal
    to float e.g.) or forbidden, like arithmatic operations between the
    two types.

    --
    Hong Yuan

    ´ó¹Ü¼ÒÍøÉϽ¨²Ä³ ¬ÊÐ
    ×°ÐÞ×°ä꽨²ÄÒ»Õ ¾Ê½¹ºÎï

  • Gasto

    #2
    Re: Decimal vs Float comparasion

    I still don't see why such a module exists.

    On 5 mayo, 21:52, "Yuan HOng" <hongyuan1...@g mail.comwrote:
    Hi,
    >
    It seems decimal object will always be larger than float in
    comparasion, which goes against common sense:
    >
    >from decimal import Decimal
    >a = Decimal('0.5')
    >a 99999
    False
    >a 99999.0
    >
    True
    >
    It seems to me that rather than allowing this to happen, comparasion
    between the two should either be made correct (by convertion decimal
    to float e.g.) or forbidden, like arithmatic operations between the
    two types.
    >
    --
    Hong Yuan
    >
    ´ó¹Ü¼ÒÍøÉϽ¨²Ä³ ¬ÊÐ
    ×°ÐÞ×°ä꽨²ÄÒ»Õ ¾Ê½¹ºÎïhttp://www.homemaster.cn

    Comment

    • Marco Mariani

      #3
      Re: Decimal vs Float comparasion

      Gasto wrote:
      I still don't see why such a module exists.
      There are 2.0 types of programmers: those who always use floating point,
      and those who know how to use them.

      Comment

      • Erik Max Francis

        #4
        Re: Decimal vs Float comparasion

        Dennis Lee Bieber wrote:
        On Tue, 6 May 2008 11:52:10 +0800, "Yuan HOng" <hongyuan1306@g mail.com>
        declaimed the following in comp.lang.pytho n:
        >
        >It seems to me that rather than allowing this to happen, comparasion
        >between the two should either be made correct (by convertion decimal
        >to float e.g.) or forbidden, like arithmatic operations between the
        >two types.
        >
        Why should decimal be coerced to float? Maybe float should be
        coerced to decimal?
        >
        Or... the programmer should explicitly specify what comparison is
        wanted -- if any...
        >
        Or... Isn't Python 3.x supposed to forbid mixed type comparisons
        unless the types implement suitable handling?
        Bottom line is that it shouldn't silently return something insane.
        99999.0 is surely exactly representable in any modern floating point
        system, being a floating point representing of an integer, so silently
        returning a completely invalid comparison is a tremendously bad idea.

        It's a bug.

        --
        Erik Max Francis && max@alcyone.com && http://www.alcyone.com/max/
        San Jose, CA, USA && 37 18 N 121 57 W && AIM, Y!M erikmaxfrancis
        Can I walk with you / 'Till the day that the world stops turning
        -- India Arie

        Comment

        • Rhamphoryncus

          #5
          Re: Decimal vs Float comparasion

          On May 6, 1:31 am, Dennis Lee Bieber <wlfr...@ix.net com.comwrote:
          On Tue, 6 May 2008 11:52:10 +0800, "Yuan HOng" <hongyuan1...@g mail.com>
          declaimed the following in comp.lang.pytho n:
          >
          >
          >
          It seems to me that rather than allowing this to happen, comparasion
          between the two should either be made correct (by convertion decimal
          to float e.g.) or forbidden, like arithmatic operations between the
          two types.
          >
          Why should decimal be coerced to float? Maybe float should be
          coerced to decimal?
          >
          Or... the programmer should explicitly specify what comparison is
          wanted -- if any...
          >
          Or... Isn't Python 3.x supposed to forbid mixed type comparisons
          unless the types implement suitable handling?
          Yes, it is fixed in 3.0. Unfortunately it's well established
          behaviour in 2.x, so it won't be changing there. Don't bother
          reporting a bug about this unless it's about 3.0.

          Comment

          Working...