Announcement

Collapse
No announcement yet.

possible efs number adding problem

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • possible efs number adding problem

    tired and overworked so this may be me but could someone please verify the following efs code on a JPY A0-FX 1 minute chart:

    PHP Code:
    var vSum 0;

    function 
    main() {

        
    vSum += 0.05;
        
        
    debugPrintln(vSum);    

        return 
    null;

    ... numbers don't add up properly
    Paul Williams
    Strategy & Applications
    www.futurenets.co.uk

  • #2
    Paul
    It seems to be working fine. When I first load it on a chart with 300 bars vSum returns 15. Then it just keeps adding 0.05 at every tick.
    Alex

    Comment


    • #3
      that's wierd, I getting

      0.05
      0.1
      0.15000000000000002
      0.2
      0.25
      0.3
      0.35
      0.39999999999999997 etc.

      my version is 7.91 Bld 738 09/12/2005
      Paul Williams
      Strategy & Applications
      www.futurenets.co.uk

      Comment


      • #4
        Paul
        In my understanding that is due to how JavaScript represents certain numbers (see this article in the EFS KnowledgeBase). The solution is to use the .toFixed() method and then multiply (or divide) by 1 to convert the resulting string back to a number
        Alex

        Comment


        • #5
          thanks Alexis. that does create a bit of a headache though.

          when adding say 0.05 to 0.35 and you may get something like 0.39999999999999997 unless you fix the decimal places?

          when immersed in logical <= >= conditions, while/for loops etc. it certainly gives you something to think about.
          Paul Williams
          Strategy & Applications
          www.futurenets.co.uk

          Comment


          • #6
            This appears to be a problem of all languages which don't have infinite-precision real numbers.

            Floating-point numbers are represented in the hardware in base 2 and stored in, say, 4 bytes. Some luckier numbers can be represented exactly, while infinitely many others can only be approximated by the closest number that can be stored exactly.

            e.g. 0.05 (in base 2) will actually be stored as something like 0.050000000000000003

            so to be safe, numbers need some pre-processing before use.

            Jason advises me to use:

            PHP Code:
            function myRound(nd) {
                return (
            n.toFixed(d) * 1);

            using d = 6 should resolve the issue for any security type.

            PS. could someone rename this "adding numbers in efs"
            Paul Williams
            Strategy & Applications
            www.futurenets.co.uk

            Comment


            • #7
              ... this and a few other nuances are in

              My EFS Template of Nuances

              Futurenets File Share
              Paul Williams
              Strategy & Applications
              www.futurenets.co.uk

              Comment


              • #8
                Hi Paul,

                Your final conclusions with regards to the binary system being able to accurately represent a number are pretty much spot on based on what I know. I appreciate the posts as it is a good reminder of the phenomena. I have several instances in my code where I should have taken this under consideration, but did not, in spite of having posted something on this previously. Sorry for not saying anything earlier, however, you had figured it out prior to me being able to post anything.

                I have a couple of items to consider when rounding for comparison purposes.
                • While rounding is extremely quick, it does take a finite amount of time to perform (.004 milliseconds), so if used excessively, it can be a potential drain on your resources. I had written an efs where I compared different methods for rounding and compared how long they took relative to each other. I posted this to my fileshare here if you are interested.
                • If testing for values being above or below a value, consider adding or subtracting .001 from the comparison value. For example, if comparing relative to 0.5, consider constructing the comparisons if (x>=.499) , or if (x<.501) in lieu of rounding.
                Last edited by ; 01-26-2006, 09:22 AM.

                Comment


                • #9
                  thanks Steve, will take a look at your fileshare, would you mind if I decide to pinch a bit of it for mine (full credits to you will be given). Likewise please take whatever you need from mine.

                  Your note on performance is definately worth some thought. My only concern is that most of the time I won't be able to predict what numbers the code will be dealing with.

                  Maybe this method could be generalized?
                  Paul Williams
                  Strategy & Applications
                  www.futurenets.co.uk

                  Comment

                  Working...
                  X