math problem

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

    #1

    math problem

    Hey,

    Expression:
    Math.floor(x * 100) / 100

    x= 4.1 gives 4.09, why in gods name?
    While other values for x don't give a problem.

    Thx in advance

    Chiwa


  • DJ WIce

    #2
    Re: math problem

    : Expression:
    : Math.floor(x * 100) / 100
    :
    : x= 4.1 gives 4.09, why in gods name?

    Because browsers are not calculators (here = sign is "can be").

    4.1 = 4.100000000000
    = 4.099999999999
    = 4.100000000001

    So:
    4.1*100 = 410.0000000000
    = 409.9999999999

    So:
    Math.floor(x * 100) = 410
    = 409

    And so your answer can be 409.
    How more devisions, multiplications and roundings the bigger the margin to
    the exact calculation may grow.

    : While other values for x don't give a problem.
    No, you just did not find them ;-)

    Wouter


    Comment

    • DJ WIce

      #3
      Re: math problem

      : Expression:
      : Math.floor(x * 100) / 100

      Try this:

      x.setPlaces(2);

      Wouter


      Comment

      • DJ WIce

        #4
        Re: math problem

        : Try this:
        :
        : x.setPlaces(2);
        See : http://www.mredkj.com/javascript/num...xample150.html

        Wouter


        Comment

        • Randy Webb

          #5
          Re: math problem

          DJ WIce wrote:
          [color=blue]
          > : Try this:
          > :
          > : x.setPlaces(2);
          > See : http://www.mredkj.com/javascript/num...xample150.html
          >[/color]



          Is a better reference for the OP

          --
          Randy
          Chance Favors The Prepared Mind
          comp.lang.javas cript FAQ - http://jibbering.com/faq/

          Comment

          • Brian Genisio

            #6
            Re: math problem

            Chiwa wrote:
            [color=blue]
            > Hey,
            >
            > Expression:
            > Math.floor(x * 100) / 100
            >
            > x= 4.1 gives 4.09, why in gods name?
            > While other values for x don't give a problem.
            >
            > Thx in advance
            >
            > Chiwa
            >
            >[/color]


            Just so you know, this is not just a limitation of Javascript. Many
            other languages (C/C++ for instance) have this as an issue. That is
            because Real numbers (floating point numbers) are _ALWAYS_
            approximations. When you do a measurement of your finger (for
            instance), and you measure exactly 1.25 inches... is that really
            correct? Or could it be 1.2500000234358 9 inches instead? Or
            1.2500000034322 349872342229? (You get the idea)

            For this reason (and others dealing with the complexity of floating
            point numbers), when a float (indescrete) is represented by binary data
            (descrete), you will have some goofiness... it is guaranteed.

            Because of this, most languages have methods for you to set the
            precision (the number of significant figures), where rounding will occur
            to the precision value you wish for.

            If you desire an exact floating point value, such as in something like
            money calculations, simply set your precisions.

            You can also work in values multiplied by (10*order)... For instance,
            $10.47 can be worked with as integers... immagine 1,047 pennies :) You
            can convert it back by dividing or modding by (10*order) in integer form.

            I have had to use that method once when developing assembly code on an
            8-bit processor. There was no float support, so I needed to work in
            values multiplied by (10*order), and then do some simple calculations to
            provide useful user information.

            Blah.... I wrote too much. Oh well, I hope it helps :)
            Brian

            Comment

            • Lasse Reichstein Nielsen

              #7
              Re: math problem

              "Chiwa" <Chiwakee@hotma il.com> writes:
              [color=blue]
              > Expression:
              > Math.floor(x * 100) / 100
              >
              > x= 4.1 gives 4.09, why in gods name?
              > While other values for x don't give a problem.[/color]

              <URL:http://jibbering.com/faq/#FAQ4_7>

              /L
              --
              Lasse Reichstein Nielsen - lrn@hotpop.com
              DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
              'Faith without judgement merely degrades the spirit divine.'

              Comment

              • Cardman

                #8
                Re: math problem

                On Sun, 01 Feb 2004 21:25:58 GMT, "Chiwa" <Chiwakee@hotma il.com>
                wrote:
                [color=blue]
                >Hey,
                >
                >Expression:
                >Math.floor(x * 100) / 100
                >
                >x= 4.1 gives 4.09, why in gods name?
                >While other values for x don't give a problem.
                >
                >Thx in advance[/color]

                After working on my order form code it appears that I noticed and
                solved this same problem long ago.

                What I do is to add 0.000001 to the value x before putting it through
                this same function, when this makes sure that the value is not below
                the true value before you run the calculation.

                Since this fraction is so small, then it would be rounded off
                anywhere, where you only have to watch out that your x value does not
                use this many decimal places.

                That of course is not a problem for money values, like what I use it
                for, with only two (or three with VAT) decimal places.

                There you go, perfect rounding results every time.

                Cardman


                Comment

                • Brian Genisio

                  #9
                  Re: math problem

                  Cardman wrote:
                  [color=blue]
                  > After working on my order form code it appears that I noticed and
                  > solved this same problem long ago.
                  >
                  > What I do is to add 0.000001 to the value x before putting it through
                  > this same function, when this makes sure that the value is not below
                  > the true value before you run the calculation.
                  >
                  > Since this fraction is so small, then it would be rounded off
                  > anywhere, where you only have to watch out that your x value does not
                  > use this many decimal places.
                  >
                  > That of course is not a problem for money values, like what I use it
                  > for, with only two (or three with VAT) decimal places.
                  >
                  > There you go, perfect rounding results every time.
                  >
                  > Cardman
                  > http://www.cardman.com
                  > http://www.cardman.co.uk[/color]

                  You have got to be careful with a solution like this, since it is just a
                  hack, and can be confusing to maintainers in the future. You should
                  really use precision mechanisms built into the language to solve the
                  problem more elegantly.

                  Brian



                  Comment

                  • Jim Ley

                    #10
                    Re: math problem

                    On Mon, 02 Feb 2004 19:48:00 +0000, Cardman <do-not@spam-me.com>
                    wrote:
                    [color=blue]
                    >That of course is not a problem for money values, like what I use it
                    >for, with only two (or three with VAT) decimal places.
                    >
                    >There you go, perfect rounding results every time.[/color]

                    however it is not a legal solution for calculating VAT involving Euros
                    say. Do it properly.

                    Jim.
                    --
                    comp.lang.javas cript FAQ - http://jibbering.com/faq/

                    Comment

                    • Douglas Crockford

                      #11
                      Re: math problem

                      > Expression:[color=blue]
                      > Math.floor(x * 100) / 100
                      >
                      > x= 4.1 gives 4.09, why in gods name?
                      > While other values for x don't give a problem.[/color]

                      JavaScript Numbers are 64-bit floating point as specified in IEEE 754.
                      It is the same representation that Java calls Double.

                      The scheme is intended to preserve as much precision as possible, but it
                      has some significant weakness. For example, it is not able to exactly
                      represent common fractions such as 1/10 or 1/100. It can at best
                      approximate them. As you can see, with a single operation you begin
                      accumulating noticeable error. Also, be aware that the associative law
                      and distributive law do not apply.

                      I think floating point is the wrong representation for numbers in most
                      applications. However, it is extremely popular, and popularity is much
                      more important these days than suitability or reliability. I recommend
                      that in applications that require exactness (for example, when working
                      with money) scale your values by a suitable factor (such as 100) so that
                      all of your arithmetic will be on whole numbers, which are exact.


                      Comment

                      • DJ WIce

                        #12
                        Re: math problem

                        : You have got to be careful with a solution like this, since it is just a
                        : hack, and can be confusing to maintainers in the future. You should
                        : really use precision mechanisms built into the language to solve the
                        : problem more elegantly.

                        I think this:
                        adding 0.0000001 and then use setPlaces(2);
                        will only be the solution when one want to have the code work in Netscape 4.
                        Exept that browser, a value of 4.1 will become 4.10 when directly used
                        setPlaces(2) (according to precious referred page).

                        Wouter






                        :
                        : Brian
                        :
                        :
                        :


                        Comment

                        • DJ WIce

                          #13
                          Re: math problem


                          : I think this:
                          : adding 0.0000001 and then use setPlaces(2);
                          : will only be the solution ..
                          <snip>

                          Sorry correction:
                          The input is x.
                          Make it a string.
                          Then get the lastIndexOf("." ) that string.

                          If it's -1 -> add ".000"
                          If it's string length-2 -> add "00"
                          If it's string length-1 -> add "000"
                          If it's string length -> add "000"

                          Now parceFloat and setPlaces(2).

                          Wouter


                          Comment

                          • DJ WIce

                            #14
                            Re: math problem

                            : Now parceFloat and setPlaces(2).
                            parseFloat..

                            sorry


                            Comment

                            • Cardman

                              #15
                              Re: math problem

                              On Mon, 02 Feb 2004 20:12:20 GMT, jim@jibbering.c om (Jim Ley) wrote:
                              [color=blue]
                              >On Mon, 02 Feb 2004 19:48:00 +0000, Cardman <do-not@spam-me.com>
                              >wrote:
                              >[color=green]
                              >>That of course is not a problem for money values, like what I use it
                              >>for, with only two (or three with VAT) decimal places.
                              >>
                              >>There you go, perfect rounding results every time.[/color]
                              >
                              >however it is not a legal solution for calculating VAT involving Euros
                              >say. Do it properly.[/color]

                              Except that the value is in Sterling when that calculation is done,
                              where it is only converted to Euros following this.

                              Even had I used a different method, then the Sterling value before
                              Euro conversion would still be exactly the same. As I said the value
                              is too small to ever effect the total.

                              Cardman


                              Comment

                              Working...