ROUND

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • kkmigas@gmail.com

    ROUND

    Can some one explain if this can be fixed using php.ini settings ?

    echo "round 20.545 -".round(20.545, 2)."<br>";
    echo "round 20.555 -".round(20.555, 2)."<br>";
    echo "number_for mat 20.545 -".number_format (20.545, 2, ',',
    '.')."<br>";
    echo "number_for mat 20.555 -".number_format (20.555, 2, ',',
    '.')."<br>";

    PHP Version 4.3.0 / FreeBSD
    round 20.545 -20.55
    round 20.555 -20.56
    number_format 20.545 -20,55
    number_format 20.555 -20,55

    PHP Version 4.4.4 / CENTOS
    round 20.545 -20.55
    round 20.555 -20.55
    number_format 20.545 -20,55
    number_format 20.555 -20,55

    PHP Version 5.1.4 / Windows NT
    round 20.545 -20.55
    round 20.555 -20.56
    number_format 20.545 -20,55
    number_format 20.555 -20,56

    I know that the round always goes for the odd but this is not
    consistence with the SO running PHP


    Thanks

  • David T. Ashley

    #2
    Re: ROUND

    <kkmigas@gmail. comwrote in message
    news:1166719766 .801640.296050@ 42g2000cwt.goog legroups.com...
    Can some one explain if this can be fixed using php.ini settings ?
    >
    echo "round 20.545 -".round(20.545, 2)."<br>";
    echo "round 20.555 -".round(20.555, 2)."<br>";
    echo "number_for mat 20.545 -".number_format (20.545, 2, ',',
    '.')."<br>";
    echo "number_for mat 20.555 -".number_format (20.555, 2, ',',
    '.')."<br>";
    >
    PHP Version 4.3.0 / FreeBSD
    round 20.545 -20.55
    round 20.555 -20.56
    number_format 20.545 -20,55
    number_format 20.555 -20,55
    >
    PHP Version 4.4.4 / CENTOS
    round 20.545 -20.55
    round 20.555 -20.55
    number_format 20.545 -20,55
    number_format 20.555 -20,55
    >
    PHP Version 5.1.4 / Windows NT
    round 20.545 -20.55
    round 20.555 -20.56
    number_format 20.545 -20,55
    number_format 20.555 -20,56
    My only observation about the examples you chose is that the fractional
    parts are infinitely repeating "radiximals " in binary.

    0.545 = 545/1000 = 109/200 (irreducible)
    0.555 = 555/1000 = 111/200 (irreducible)

    Both of the numbers above can't be expressed as precise binary floating
    point numbers because the denominator after reduction has prime factors that
    are not "2".

    PHP.INI may be a factor, but it may also be that something at a lower level
    is occurring (i.e. machine arithmetic, how certain rounding settings on
    floating-point processors or libraries are set).

    In other words, you've chosen antagonistic examples.

    Why don't you try 7/8 (0.875), 15/16 (0.9275), or 31/32 (0.96875) as the
    fractional part of the numbers and see if you can get the same behavior.
    I'm guessing that you may not.

    Any fraction with a reasonable denomiator (< 2^16) that is a power of 2
    would be a reasonable test case.

    Note that these are all exactly representable by a typical machine.

    Just a guess.



    Comment

    • kkmigas@gmail.com

      #3
      Re: ROUND

      I've tested the same function in the same hardware configuration and
      the only difference is the OS, so it must be the libraries the big
      problem is that i don't know witch ones to replace in order to get the
      system to solve the way i want.




      On Dec 22, 4:42 pm, "David T. Ashley" <d...@e3ft.comw rote:
      <kkmi...@gmail. comwrote in messagenews:116 6719766.801640. 296050@42g2000c wt.googlegroups .com...
      >
      >
      >
      Can some one explain if this can be fixed using php.ini settings ?
      >
      echo "round 20.545 -".round(20.545, 2)."<br>";
      echo "round 20.555 -".round(20.555, 2)."<br>";
      echo "number_for mat 20.545 -".number_format (20.545, 2, ',',
      '.')."<br>";
      echo "number_for mat 20.555 -".number_format (20.555, 2, ',',
      '.')."<br>";
      >
      PHP Version 4.3.0 / FreeBSD
      round 20.545 -20.55
      round 20.555 -20.56
      number_format 20.545 -20,55
      number_format 20.555 -20,55
      >
      PHP Version 4.4.4 / CENTOS
      round 20.545 -20.55
      round 20.555 -20.55
      number_format 20.545 -20,55
      number_format 20.555 -20,55
      >
      PHP Version 5.1.4 / Windows NT
      round 20.545 -20.55
      round 20.555 -20.56
      number_format 20.545 -20,55
      number_format 20.555 -20,56My only observation about the examples you chose is that the fractional
      parts are infinitely repeating "radiximals " in binary.
      >
      0.545 = 545/1000 = 109/200 (irreducible)
      0.555 = 555/1000 = 111/200 (irreducible)
      >
      Both of the numbers above can't be expressed as precise binary floating
      point numbers because the denominator after reduction has prime factors that
      are not "2".
      >
      PHP.INI may be a factor, but it may also be that something at a lower level
      is occurring (i.e. machine arithmetic, how certain rounding settings on
      floating-point processors or libraries are set).
      >
      In other words, you've chosen antagonistic examples.
      >
      Why don't you try 7/8 (0.875), 15/16 (0.9275), or 31/32 (0.96875) as the
      fractional part of the numbers and see if you can get the same behavior.
      I'm guessing that you may not.
      >
      Any fraction with a reasonable denomiator (< 2^16) that is a power of 2
      would be a reasonable test case.
      >
      Note that these are all exactly representable by a typical machine.
      >
      Just a guess.

      Comment

      • kkmigas@gmail.com

        #4
        Re: ROUND

        PHP code
        echo "round(10.045,2 ) - ".round(10.045, 2)."<br />";
        echo "round(20.045,2 ) - ".round(20.045, 2)."<br />";
        echo "round(30.045,2 ) - ".round(30.045, 2)."<br />";
        echo "round(40.045,2 ) - ".round(40.045, 2)."<br />";

        This is very strange:

        round(10.045,2) - 10.04
        round(20.045,2) - 20.05
        round(30.045,2) - 30.05
        round(40.045,2) - 40.05

        On Dec 26, 11:57 am, "kkmi...@gmail. com" <kkmi...@gmail. comwrote:
        I've tested the same function in the same hardware configuration and
        the only difference is the OS, so it must be the libraries the big
        problem is that i don't know witch ones to replace in order to get the
        system to solve the way i want.
        >
        On Dec 22, 4:42 pm, "David T. Ashley" <d...@e3ft.comw rote:
        >
        <kkmi...@gmail. comwrote in messagenews:116 6719766.801640. 296050@42g2000c wt.googlegroups .com...
        >
        Can some one explain if this can be fixed using php.ini settings ?
        >
        echo "round 20.545 -".round(20.545, 2)."<br>";
        echo "round 20.555 -".round(20.555, 2)."<br>";
        echo "number_for mat 20.545 -".number_format (20.545, 2, ',',
        '.')."<br>";
        echo "number_for mat 20.555 -".number_format (20.555, 2, ',',
        '.')."<br>";
        >
        PHP Version 4.3.0 / FreeBSD
        round 20.545 -20.55
        round 20.555 -20.56
        number_format 20.545 -20,55
        number_format 20.555 -20,55
        >
        PHP Version 4.4.4 / CENTOS
        round 20.545 -20.55
        round 20.555 -20.55
        number_format 20.545 -20,55
        number_format 20.555 -20,55
        >
        PHP Version 5.1.4 / Windows NT
        round 20.545 -20.55
        round 20.555 -20.56
        number_format 20.545 -20,55
        number_format 20.555 -20,56My only observation about the examples you chose is that the fractional
        parts are infinitely repeating "radiximals " in binary.
        >
        0.545 = 545/1000 = 109/200 (irreducible)
        0.555 = 555/1000 = 111/200 (irreducible)
        >
        Both of the numbers above can't be expressed as precise binary floating
        point numbers because the denominator after reduction has prime factors that
        are not "2".
        >
        PHP.INI may be a factor, but it may also be that something at a lower level
        is occurring (i.e. machine arithmetic, how certain rounding settings on
        floating-point processors or libraries are set).
        >
        In other words, you've chosen antagonistic examples.
        >
        Why don't you try 7/8 (0.875), 15/16 (0.9275), or 31/32 (0.96875) as the
        fractional part of the numbers and see if you can get the same behavior.
        I'm guessing that you may not.
        >
        Any fraction with a reasonable denomiator (< 2^16) that is a power of 2
        would be a reasonable test case.
        >
        Note that these are all exactly representable by a typical machine.
        >
        Just a guess.

        Comment

        • Jerry Stuckle

          #5
          Re: ROUND

          kkmigas@gmail.c om wrote:
          PHP code
          echo "round(10.045,2 ) - ".round(10.045, 2)."<br />";
          echo "round(20.045,2 ) - ".round(20.045, 2)."<br />";
          echo "round(30.045,2 ) - ".round(30.045, 2)."<br />";
          echo "round(40.045,2 ) - ".round(40.045, 2)."<br />";
          >
          This is very strange:
          >
          round(10.045,2) - 10.04
          round(20.045,2) - 20.05
          round(30.045,2) - 30.05
          round(40.045,2) - 40.05
          >
          On Dec 26, 11:57 am, "kkmi...@gmail. com" <kkmi...@gmail. comwrote:
          >
          >>I've tested the same function in the same hardware configuration and
          >>the only difference is the OS, so it must be the libraries the big
          >>problem is that i don't know witch ones to replace in order to get the
          >>system to solve the way i want.
          >>
          >>On Dec 22, 4:42 pm, "David T. Ashley" <d...@e3ft.comw rote:
          >>
          >>
          >>><kkmi...@gma il.comwrote in messagenews:116 6719766.801640. 296050@42g2000c wt.googlegroups .com...
          >>
          >>>>Can some one explain if this can be fixed using php.ini settings ?
          >>
          >>>>echo "round 20.545 -".round(20.545, 2)."<br>";
          >>>>echo "round 20.555 -".round(20.555, 2)."<br>";
          >>>>echo "number_for mat 20.545 -".number_format (20.545, 2, ',',
          >>>>'.')."<br>" ;
          >>>>echo "number_for mat 20.555 -".number_format (20.555, 2, ',',
          >>>>'.')."<br>" ;
          >>
          >>>>PHP Version 4.3.0 / FreeBSD
          >>>>round 20.545 -20.55
          >>>>round 20.555 -20.56
          >>>>number_form at 20.545 -20,55
          >>>>number_form at 20.555 -20,55
          >>
          >>>>PHP Version 4.4.4 / CENTOS
          >>>>round 20.545 -20.55
          >>>>round 20.555 -20.55
          >>>>number_form at 20.545 -20,55
          >>>>number_form at 20.555 -20,55
          >>
          >>>>PHP Version 5.1.4 / Windows NT
          >>>>round 20.545 -20.55
          >>>>round 20.555 -20.56
          >>>>number_form at 20.545 -20,55
          >>>>number_form at 20.555 -20,56My only observation about the examples you chose is that the fractional
          >>>
          >>>parts are infinitely repeating "radiximals " in binary.
          >>
          >>>0.545 = 545/1000 = 109/200 (irreducible)
          >>>0.555 = 555/1000 = 111/200 (irreducible)
          >>
          >>>Both of the numbers above can't be expressed as precise binary floating
          >>>point numbers because the denominator after reduction has prime factors that
          >>>are not "2".
          >>
          >>>PHP.INI may be a factor, but it may also be that something at a lower level
          >>>is occurring (i.e. machine arithmetic, how certain rounding settings on
          >>>floating-point processors or libraries are set).
          >>
          >>>In other words, you've chosen antagonistic examples.
          >>
          >>>Why don't you try 7/8 (0.875), 15/16 (0.9275), or 31/32 (0.96875) as the
          >>>fractional part of the numbers and see if you can get the same behavior.
          >>>I'm guessing that you may not.
          >>
          >>>Any fraction with a reasonable denomiator (< 2^16) that is a power of 2
          >>>would be a reasonable test case.
          >>
          >>>Note that these are all exactly representable by a typical machine.
          >>
          >>>Just a guess.
          >
          >
          This is the exact same problem you reported in comp.databases. mysql, and
          the solution is exactly the same.

          --
          =============== ===
          Remove the "x" from my email address
          Jerry Stuckle
          JDS Computer Training Corp.
          jstucklex@attgl obal.net
          =============== ===

          Comment

          • kkmigas@gmail.com

            #6
            Re: ROUND

            Has i told you on the other newsgroup

            This 2 servers have the same hardware configuration

            On Windows 2003


            On Linux


            On Dec 27, 12:54 am, Jerry Stuckle <jstuck...@attg lobal.netwrote:
            kkmi...@gmail.c om wrote:
            PHP code
            echo "round(10.045,2 ) - ".round(10.045, 2)."<br />";
            echo "round(20.045,2 ) - ".round(20.045, 2)."<br />";
            echo "round(30.045,2 ) - ".round(30.045, 2)."<br />";
            echo "round(40.045,2 ) - ".round(40.045, 2)."<br />";
            >
            This is very strange:
            >
            round(10.045,2) - 10.04
            round(20.045,2) - 20.05
            round(30.045,2) - 30.05
            round(40.045,2) - 40.05
            >
            On Dec 26, 11:57 am, "kkmi...@gmail. com" <kkmi...@gmail. comwrote:
            >
            >I've tested the same function in the same hardware configuration and
            >the only difference is the OS, so it must be the libraries the big
            >problem is that i don't know witch ones to replace in order to get the
            >system to solve the way i want.
            >
            >On Dec 22, 4:42 pm, "David T. Ashley" <d...@e3ft.comw rote:
            >
            >><kkmi...@gmai l.comwrote in messagenews:116 6719766.801640. 296050@42g2000c wt.googlegroups .com...
            >
            >>>Can some one explain if this can be fixed using php.ini settings ?
            >
            >>>echo "round 20.545 -".round(20.545, 2)."<br>";
            >>>echo "round 20.555 -".round(20.555, 2)."<br>";
            >>>echo "number_for mat 20.545 -".number_format (20.545, 2, ',',
            >>>'.')."<br> ";
            >>>echo "number_for mat 20.555 -".number_format (20.555, 2, ',',
            >>>'.')."<br> ";
            >
            >>>PHP Version 4.3.0 / FreeBSD
            >>>round 20.545 -20.55
            >>>round 20.555 -20.56
            >>>number_forma t 20.545 -20,55
            >>>number_forma t 20.555 -20,55
            >
            >>>PHP Version 4.4.4 / CENTOS
            >>>round 20.545 -20.55
            >>>round 20.555 -20.55
            >>>number_forma t 20.545 -20,55
            >>>number_forma t 20.555 -20,55
            >
            >>>PHP Version 5.1.4 / Windows NT
            >>>round 20.545 -20.55
            >>>round 20.555 -20.56
            >>>number_forma t 20.545 -20,55
            >>>number_forma t 20.555 -20,56My only observation about the examples you chose is that the fractional
            >
            >>parts are infinitely repeating "radiximals " in binary.
            >
            >>0.545 = 545/1000 = 109/200 (irreducible)
            >>0.555 = 555/1000 = 111/200 (irreducible)
            >
            >>Both of the numbers above can't be expressed as precise binary floating
            >>point numbers because the denominator after reduction has prime factors that
            >>are not "2".
            >
            >>PHP.INI may be a factor, but it may also be that something at a lower level
            >>is occurring (i.e. machine arithmetic, how certain rounding settings on
            >>floating-point processors or libraries are set).
            >
            >>In other words, you've chosen antagonistic examples.
            >
            >>Why don't you try 7/8 (0.875), 15/16 (0.9275), or 31/32 (0.96875) as the
            >>fractional part of the numbers and see if you can get the same behavior.
            >>I'm guessing that you may not.
            >
            >>Any fraction with a reasonable denomiator (< 2^16) that is a power of 2
            >>would be a reasonable test case.
            >
            >>Note that these are all exactly representable by a typical machine.
            >
            >>Just a guess.This is the exact same problem you reported in comp.databases. mysql, and
            the solution is exactly the same.
            >
            --
            =============== ===
            Remove the "x" from my email address
            Jerry Stuckle
            JDS Computer Training Corp.
            jstuck...@attgl obal.net
            =============== ===

            Comment

            • David T. Ashley

              #7
              Re: ROUND

              <kkmigas@gmail. comwrote in message
              news:1167146220 .067133.35770@7 3g2000cwn.googl egroups.com...
              PHP code
              echo "round(10.045,2 ) - ".round(10.045, 2)."<br />";
              echo "round(20.045,2 ) - ".round(20.045, 2)."<br />";
              echo "round(30.045,2 ) - ".round(30.045, 2)."<br />";
              echo "round(40.045,2 ) - ".round(40.045, 2)."<br />";
              >
              This is very strange:
              >
              round(10.045,2) - 10.04
              round(20.045,2) - 20.05
              round(30.045,2) - 30.05
              round(40.045,2) - 40.05
              Why are you posting this code and quoting my post?

              ..045 = 45/1000 = 9/200 = denominator not a power of 2.

              Like your earlier examples, these numbers are not exactly expressable as
              base-2 decimals.

              It would be interesting to determine the breakpoint between
              round(10.045,2) - 10.04
              round(20.045,2) - 20.05
              where the rounding changes. I'm going to guess that 15.045 is one way and
              16.045 is the other.

              Dav.e



              Comment

              • Jerry Stuckle

                #8
                Re: ROUND

                kkmigas@gmail.c om wrote:
                On Dec 27, 12:54 am, Jerry Stuckle <jstuck...@attg lobal.netwrote:
                >
                >>kkmi...@gmail .com wrote:
                >>
                >>>PHP code
                >>>echo "round(10.045,2 ) - ".round(10.045, 2)."<br />";
                >>>echo "round(20.045,2 ) - ".round(20.045, 2)."<br />";
                >>>echo "round(30.045,2 ) - ".round(30.045, 2)."<br />";
                >>>echo "round(40.045,2 ) - ".round(40.045, 2)."<br />";
                >>
                >>>This is very strange:
                >>
                >>>round(10.045 ,2) - 10.04
                >>>round(20.045 ,2) - 20.05
                >>>round(30.045 ,2) - 30.05
                >>>round(40.045 ,2) - 40.05
                >>
                >>>On Dec 26, 11:57 am, "kkmi...@gmail. com" <kkmi...@gmail. comwrote:
                >>
                >>>>I've tested the same function in the same hardware configuration and
                >>>>the only difference is the OS, so it must be the libraries the big
                >>>>problem is that i don't know witch ones to replace in order to get the
                >>>>system to solve the way i want.
                >>
                >>>>On Dec 22, 4:42 pm, "David T. Ashley" <d...@e3ft.comw rote:
                >>
                >>>>><kkmi...@g mail.comwrote in messagenews:116 6719766.801640. 296050@42g2000c wt.googlegroups .com...
                >>
                >>>>>>Can some one explain if this can be fixed using php.ini settings ?
                >>
                >>>>>>echo "round 20.545 -".round(20.545, 2)."<br>";
                >>>>>>echo "round 20.555 -".round(20.555, 2)."<br>";
                >>>>>>echo "number_for mat 20.545 -".number_format (20.545, 2, ',',
                >>>>>>'.')."<br >";
                >>>>>>echo "number_for mat 20.555 -".number_format (20.555, 2, ',',
                >>>>>>'.')."<br >";
                >>
                >>>>>>PHP Version 4.3.0 / FreeBSD
                >>>>>>round 20.545 -20.55
                >>>>>>round 20.555 -20.56
                >>>>>>number_fo rmat 20.545 -20,55
                >>>>>>number_fo rmat 20.555 -20,55
                >>
                >>>>>>PHP Version 4.4.4 / CENTOS
                >>>>>>round 20.545 -20.55
                >>>>>>round 20.555 -20.55
                >>>>>>number_fo rmat 20.545 -20,55
                >>>>>>number_fo rmat 20.555 -20,55
                >>
                >>>>>>PHP Version 5.1.4 / Windows NT
                >>>>>>round 20.545 -20.55
                >>>>>>round 20.555 -20.56
                >>>>>>number_fo rmat 20.545 -20,55
                >>>>>>number_fo rmat 20.555 -20,56My only observation about the examples you chose is that the fractional
                >>
                >>>>>parts are infinitely repeating "radiximals " in binary.
                >>
                >>>>>0.545 = 545/1000 = 109/200 (irreducible)
                >>>>>0.555 = 555/1000 = 111/200 (irreducible)
                >>
                >>>>>Both of the numbers above can't be expressed as precise binary floating
                >>>>>point numbers because the denominator after reduction has prime factors that
                >>>>>are not "2".
                >>
                >>>>>PHP.INI may be a factor, but it may also be that something at a lower level
                >>>>>is occurring (i.e. machine arithmetic, how certain rounding settings on
                >>>>>floating-point processors or libraries are set).
                >>
                >>>>>In other words, you've chosen antagonistic examples.
                >>
                >>>>>Why don't you try 7/8 (0.875), 15/16 (0.9275), or 31/32 (0.96875) as the
                >>>>>fraction al part of the numbers and see if you can get the same behavior.
                >>>>>I'm guessing that you may not.
                >>
                >>>>>Any fraction with a reasonable denomiator (< 2^16) that is a power of 2
                >>>>>would be a reasonable test case.
                >>
                >>>>>Note that these are all exactly representable by a typical machine.
                >>
                >>>>>Just a guess.This is the exact same problem you reported in comp.databases. mysql, and
                >>
                >>the solution is exactly the same.
                >>
                >>--
                >>============= =====
                >>Remove the "x" from my email address
                >>Jerry Stuckle
                >>JDS Computer Training Corp.
                >>jstuck...@att global.net
                >>============= =====
                >
                >
                Has i told you on the other newsgroup
                >
                This 2 servers have the same hardware configuration
                >
                On Windows 2003

                >
                On Linux

                >
                (Top posting fixed)

                And as I and others have told you - when using floating point, results
                when using expressions like these are unpredictable.

                It's just like when using base 10:

                round((1.0/3.0) + (1.0/6.0))

                can equal zero, because neither 1.0/3.0 nor 1.0/1.6 can be expressed as
                an exact value in base 10. They are repeating decimals.

                Sure, they *should* come out to 0.5. But their actual value will be
                0.499999999 to however many decimal places could be handled.

                *Some* libraries will "fix" this by adding a small value (i.e.
                0.000000001) to the results to "fudge" the value. But no such fudge
                factor is required, nor is it always good.

                You are always best to use integers if you need exact values, or add
                your own fudge factor to the results.

                It's been that way for the almost 40 years I've been programming, and
                it's not going to change now. That's why many processors now have a
                "packed decimal" or a "binary coded decimal" type internally to give
                exact results.

                --
                =============== ===
                Remove the "x" from my email address
                Jerry Stuckle
                JDS Computer Training Corp.
                jstucklex@attgl obal.net
                =============== ===

                Comment

                Working...