Strange math result... 1-1= -1 ???

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

    #1

    Strange math result... 1-1= -1 ???

    See something weird on this result?

    632.35 - 632.35 = -1.13686837722E-013

    If anyone has seen something similar, please let me know!

    this a part of the code:

    $total_due = abs( $order_total ) - abs( $total_paid );
    echo( $order_total . " - " . $total_paid . " = " . $total_due );

    I've added the abs() in order to be sure that I'm dealing with
    numbers, I've also used is_numeric() and confirmed it.
    The echo() is showing the result I pasted up there.


    any ideas??
  • William Holroyd

    #2
    Re: Strange math result... 1-1= -1 ???

    I have never seen that before and no clue as how to fix it.

    But for a work around, use...

    $output = round($input, 2); // outputs 12.36 from 12.3556

    Hope this helps.

    -William


    "Mark" <marcosc@gmail. com> wrote in message
    news:5a57cb8b.0 406301231.2cefc 08e@posting.goo gle.com...[color=blue]
    > See something weird on this result?
    >
    > 632.35 - 632.35 = -1.13686837722E-013
    >
    > If anyone has seen something similar, please let me know!
    >
    > this a part of the code:
    >
    > $total_due = abs( $order_total ) - abs( $total_paid );
    > echo( $order_total . " - " . $total_paid . " = " . $total_due );
    >
    > I've added the abs() in order to be sure that I'm dealing with
    > numbers, I've also used is_numeric() and confirmed it.
    > The echo() is showing the result I pasted up there.
    >
    >
    > any ideas??[/color]


    Comment

    • Pedro Graca

      #3
      Re: Strange math result... 1-1= -1 ???

      Mark wrote:[color=blue]
      > See something weird on this result?
      >
      > 632.35 - 632.35 = -1.13686837722E-013[/color]

      The first 632.35 is in fact something like 632.34999999993 7038
      and the second is ....... 632.35000000020 3192
      [color=blue]
      > If anyone has seen something similar, please let me know!
      >
      > this a part of the code:
      >
      > $total_due = abs( $order_total ) - abs( $total_paid );
      > echo( $order_total . " - " . $total_paid . " = " . $total_due );[/color]

      echo number_format($ order_total, 14);
      echo number_format($ total_paid, 14);
      [color=blue]
      > I've added the abs() in order to be sure that I'm dealing with
      > numbers, I've also used is_numeric() and confirmed it.
      > The echo() is showing the result I pasted up there.
      >
      >
      > any ideas??[/color]

      As another poster said use round().

      --
      USENET would be a better place if everybody read: | to email me: use |
      http://www.catb.org/~esr/faqs/smart-questions.html | my name in "To:" |
      http://www.netmeister.org/news/learn2quote2.html | header, textonly |
      http://www.expita.com/nomime.html | no attachments. |

      Comment

      • Andy Hassall

        #4
        Re: Strange math result... 1-1= -1 ???

        On 30 Jun 2004 13:31:26 -0700, marcosc@gmail.c om (Mark) wrote:
        [color=blue]
        >See something weird on this result?
        >
        >632.35 - 632.35 = -1.13686837722E-013
        >
        >If anyone has seen something similar, please let me know!
        >
        >this a part of the code:
        >
        >$total_due = abs( $order_total ) - abs( $total_paid );
        >echo( $order_total . " - " . $total_paid . " = " . $total_due );
        >
        >I've added the abs() in order to be sure that I'm dealing with
        >numbers, I've also used is_numeric() and confirmed it.
        >The echo() is showing the result I pasted up there.
        >
        >any ideas??[/color]

        This is normal behaviour from computers which use the IEEE floating-point
        format (i.e. pretty much all of them). Many decimal numbers have no exact
        representation in this format, however they will differ only by a very small
        value. So when you start doing calculations, small errors build up.

        It looks like you are working with monetary values. You should avoid using
        floating point numbers for money, and convert everything to integer values of
        the smallest currency unit or fraction thereof you want to deal with, for
        instance cents or pence. Then only convert to a floating point value for
        display. This way all calculations remain exact.

        --
        Andy Hassall <andy@andyh.co. uk> / Space: disk usage analysis tool
        http://www.andyh.co.uk / http://www.andyhsoftware.co.uk/space

        Comment

        • Dana Cartwright

          #5
          Re: Strange math result... 1-1= -1 ???

          You subject line isn't correct (just for the record).

          -1.13686837722E-013 isn't -1. It's a number that is very close to being
          zero. It happens to be negative, meaning it's slightly less than zero. It
          *certainly* isn't anywhere close to -1.

          So within the normal limits of precision of floating point arithmetic, you
          have "x - x = 0" which is correct. As others have pointed out, the computer
          is doing its job exactly right.

          --
          --------------------
          My e-mail address doesn't have a 2 in it.

          "Mark" <marcosc@gmail. com> wrote in message
          news:5a57cb8b.0 406301231.2cefc 08e@posting.goo gle.com...[color=blue]
          > See something weird on this result?
          >
          > 632.35 - 632.35 = -1.13686837722E-013
          >
          > If anyone has seen something similar, please let me know!
          >
          > this a part of the code:
          >
          > $total_due = abs( $order_total ) - abs( $total_paid );
          > echo( $order_total . " - " . $total_paid . " = " . $total_due );
          >
          > I've added the abs() in order to be sure that I'm dealing with
          > numbers, I've also used is_numeric() and confirmed it.
          > The echo() is showing the result I pasted up there.
          >
          >
          > any ideas??[/color]


          Comment

          • Nikolai Chuvakhin

            #6
            Re: Strange math result... 1-1= -1 ???

            marcosc@gmail.c om (Mark) wrote in message
            news:<5a57cb8b. 0406301231.2cef c08e@posting.go ogle.com>...[color=blue]
            >
            > See something weird on this result?
            >
            > 632.35 - 632.35 = -1.13686837722E-013[/color]

            There's ABSOLUTELY NOTHING strange about it. This is how floating-
            point numbers work.
            [color=blue]
            > this a part of the code:
            >
            > $total_due = abs( $order_total ) - abs( $total_paid );
            > echo( $order_total . " - " . $total_paid . " = " . $total_due );[/color]
            [color=blue]
            > any ideas??[/color]

            Try this:

            $total_due = $order_total - $total_paid;
            echo $order_total , " - " , $total_paid , " = " ,
            number_format($ total_due, 2);

            Cheers,
            NC

            Comment

            Working...