Why 3.0/5.0 = 0.59999...

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

    #1

    Why 3.0/5.0 = 0.59999...

    Hi all,

    I used to be a Matlab user. And want to use Python to replace some
    Matlab work.

    When I type 3.0/5.0, the result is 0.59999...

    Is there some precision loss? And how to overcome it?

    Any suggestions will be appreciated!
    Best regards,
    Davy

  • Dale Strickland-Clark

    #2
    Re: Why 3.0/5.0 = 0.59999...

    Have you looked at the decimal module?

    python
    Python 2.4 (#1, Mar 22 2005, 21:42:42)
    [GCC 3.3.5 20050117 (prerelease) (SUSE Linux)] on linux2
    Type "help", "copyright" , "credits" or "license" for more information.[color=blue][color=green][color=darkred]
    >>> from decimal import *
    >>> Decimal("3")/Decimal("5")[/color][/color][/color]
    Decimal("0.6")[color=blue][color=green][color=darkred]
    >>>[/color][/color][/color]

    I don't suppose it is quick but it might do what you want.

    Davy wrote:
    [color=blue]
    > Hi all,
    >
    > I used to be a Matlab user. And want to use Python to replace some
    > Matlab work.
    >
    > When I type 3.0/5.0, the result is 0.59999...
    >
    > Is there some precision loss? And how to overcome it?
    >
    > Any suggestions will be appreciated!
    > Best regards,
    > Davy[/color]

    --
    Dale Strickland-Clark
    Riverhall Systems - www.riverhall.co.uk

    Comment

    • Mirco Wahab

      #3
      Re: Why 3.0/5.0 = 0.59999...

      Hi Davy
      [color=blue]
      > When I type 3.0/5.0, the result is 0.59999...[/color]

      try:
      [color=blue][color=green][color=darkred]
      >>> print (3.0/5.0)[/color][/color][/color]
      0.6
      [color=blue]
      > Is there some precision loss? And how to overcome it?[/color]

      Make sure the result gets piped
      through some floating point con-
      version (like print, %s etc.)

      BTW, the 0.5999999999999 9998 value
      is the "exact" calculated value
      corresponding to your floating point
      accuracy (34/2 digits).


      Regards

      Mirco

      Comment

      • Jeremy Sanders

        #4
        Re: Why 3.0/5.0 = 0.59999...

        Davy wrote:
        [color=blue]
        > Is there some precision loss? And how to overcome it?[/color]


        See

        for some useful information.

        --
        Jeremy Sanders

        Comment

        • Grant Edwards

          #5
          Re: Why 3.0/5.0 = 0.59999...

          On 2006-05-09, Davy <zhushenli@gmai l.com> wrote:
          [color=blue]
          > When I type 3.0/5.0, the result is 0.59999...
          >
          > Is there some precision loss? And how to overcome it?[/color]

          Doesn't anybody read the FAQ? Or even browse through current
          threads before posting?

          Sheesh. This has been in the FAQ forever, and was answered
          again in the group just yesterday.
          [color=blue]
          > Any suggestions will be appreciated![/color]

          I doubt it.

          --
          Grant Edwards grante Yow! I'LL get it!! It's
          at probably a FEW of my
          visi.com ITALIAN GIRL-FRIENDS!!

          Comment

          Working...