Avatar for Robin

Robin

Member since Jan 2017 • Last active Aug 2019
  • 54 conversations
  • 726 comments

Most recent activity

  • in JavaScript
    Avatar for Robin

    Sun 2019.08.25

    And just when one thinks every stone is over turned, more web searching digs up that others have found that toFixed(2); is problematic between different browsers and have concluded the safest method is to use your own rounding function, 'just to be sure!'

    https://stackoverflow.com/questions/1001­5027/javascript-tofixed-not-rounding

    A link there over turns this relatively recent 'fix' - "Revision 1383484 of Math.round()"

    https://developer.mozilla.org/en-US/docs­/Web/JavaScript/Reference/Global_Objects­/Math/round$revision/1383484

    "Rounding 1.005 should have an expected result of 1.01, but it returns 1."



    So, what do we know so far for function toFixed()?

    by definition:

    'The number is rounded if necessary, and the fractional part is padded with zeros if necessary so that it has the specified length'

    https://developer.mozilla.org/en-US/docs­/Web/JavaScript/Reference/Global_Objects­/Number/toFixed

    EXCEPT when it doesn't, then one must apply a patch that is only discovered when locating the recommended fix as suggested in revision 1383484.

    Pardon me while I go scream!!

  • in ESP8266
    Avatar for Robin

    Right-Click on image works, see if the filename AND extension are present in the link

  • in JavaScript
    Avatar for Robin

    Sun 2019.08.25

    Links to areas of source that appear to be the entry points that will need a looksy:

    Definition

    https://www.espruino.com/Reference#l_Num­ber_toFixed

    GitHub source file for toFixed()

    https://github.com/espruino/Espruino/blo­b/master/src/jswrap_number.c#L120

    JsVar *jswrap_number_toFixed(JsVar *parent, int decimals) {
      if (decimals<0) decimals=0;
      if (decimals>20) decimals=20;
      char buf[JS_NUMBER_BUFFER_SIZE];
      ftoa_bounded_extra(jsvGetFloat(parent), buf, sizeof(buf), 10, decimals);
      return jsvNewFromString(buf);
    }
    



    JS_NUMBER_BUFFER_SIZE    is suspect, but I'm not able to locate where that constant is defined



    ftoa_bounded_extra

    https://github.com/espruino/Espruino/blo­b/master
    https://github.com/espruino/Espruino/blo­b/master/src/jsutils.c
    https://github.com/espruino/Espruino/blo­b/master/src/jsutils.c#L614

    void ftoa_bounded_extra(JsVarFloat val,char *str, size_t len, int radix, int fractionalDigits) {
    
    // possible areas that end of array may be encountered
    

    jsvGetFloat

    https://github.com/espruino/Espruino/blo­b/master
    https://github.com/espruino/Espruino/blo­b/master/src/jsvar.c#L1867

    JsVarFloat jsvGetFloat(const JsVar *v) {
    
    // too many lines and entry points to list here
    
  • in JavaScript
    Avatar for Robin

    Sun 2019.08.25

    Thank you @AkosLukacs for the additional tidy summary.

    'There gdb, but that itself has a steep learning curve afaik.'

    Found this ten minute overview of GDB, which supplies the basics,

    https://www.youtube.com/watch?v=sCtY--xR­UyI

    and there is VSCode, Git and Bash to learn on top of it all.



    With only a basic introduction with RedHat over a decade ago, this process will feel like starting from scratch in an entirely new environment. Planning on chunking, maybe an hour a week, until I'm up to speed.

    With three Espruino projects in the works, will even have to delay that thought. A winter starter perhaps.

  • in JavaScript
    Avatar for Robin

    '64 bit Windows'

    Yes, have that.

    Thank you @Wilberforce, that helps in clearing that up. I'm sure others reading along will appreciate that info also. By "In your ide" does this imply a browser of my choice, or is this an IDE that will be available once Ubuntu shell is installed? Is a debugger available, or do we use the Chrome debugger?

    The biggest obstacle I see is attempting to traverse the 'C' logic in performing the rounding. I've uncovered these links:

    TO BE ADDED SHORTLY - EDIT see post #10 for functions and entry points

    BTW: This will be more of a winter project for me, as I really don't want to start meddling with the underlying source. Will also need to re-build my Linux skills along with telnet commands, quite an undertaking. However, as implied by post #4, this has been a tremendous learning experience in the understanding of how complex, just a simple addition instruction truly is under the covers. Most probably don't give a rat's expletive but it has clued me in to the complexity of what you guy's actually do. Thank's for that effort!!

  • in JavaScript
    Avatar for Robin

    Sat 2019.08.24

    Although I have spent a great deal of time in an attempt to resolve this rounding issue, I am currently behind on the skills and don't have the tools needed to perform a correction. I'm also waaaaaaay short on time to face the learning curve involved.

    In case I enter one of my sadistic moments to venture down a path of creating compilations,

    I've scanned

    https://github.com/espruino/Espruino
    https://github.com/espruino/Espruino/blo­b/master/README_BuildProcess.md

    and it appears one could perform this task on a Windows10 PC. If I'm understanding this, I need a virtual PC to run a Linux OS image. Ubuntu is used to compile the files that are written in 'C'. Ubuntu runs Python scripts that perform the compilation.

    So far, am I on the right track?

    Then what is not clear, what tools are needed to perform debugging? My background from a decade ago was VStudio pre community edition, building web services in C/C++/C#.

    Any other intuitive assistance to get a good overview would be appreciated.

  • in JavaScript
    Avatar for Robin

    Sat 2019.08.24

    Took a bit longer than I anticipated

    Attempting to uncover the source of this rounding error reminds of a book I read thirty years ago that had an impact on my career, if not more awareness to the world of computing. I was buried in electronics at the time, barely post tube era. (I hear the laughter now) oops, sorry proper web etiquette ROTF LOL

    Even remember the author, Clifford Stoll. Anyone with me? The Cuckoos Egg A true story that Cliff unravels tracking down $0.75 accumulated rounding error that leads him down the path tracking the crime in progress. And across a 1200 baud modem! Remember those days? Mesmerised over the step-by-step process to find the computer anomaly. Can't immediately put my hand on the hard copy I picked up later in the nineties, but did find a link. Isn't the web a marvelous place!!

    Grab a copy - worth the read

    http://bayrampasamakina.com/tr/pdf_stoll­_4_1.pdf



    Back to our Cuckoos Egg that is plaguing this discovered rounding issue.

    While cranking out routines to assist in building some data to analyze, I uncovered a slick online tool to create Floating Point values.

    https://www.exploringbinary.com/floating­-point-converter/

    I used this to cross check my manual register byte creation. Still need some looping functions to solve this anomaly though.





    From the following we can visually verify that the @valderman Feb 2012 snippet does in fact return consistent accurate results.

    Output from Chrome browser

    significand= 0011010010100000010100011110101110000101­00
    significand= 01234567890123456789012345678901      
    //    array element index reference only
    
    
    // we have a sign bit, eight exponent bits, followed by the significand, 
    // the string of bits we are after starting at element 10
    
    
    2^0 = 1 add leading implicit 1
    // we would use the above for our decimal representation
    
    str = [ 10000001010001111010111 ] 
    
    [ 2^ -1 ] 0.5
    [ 2^ -8 ] 0.00390625
    [ 2^-10 ] 0.0009765625
    [ 2^-14 ] 0.00006103515625
    [ 2^-15 ] 0.000030517578125
    [ 2^-16 ] 0.0000152587890625
    [ 2^-17 ] 0.00000762939453125
    [ 2^-19 ] 0.0000019073486328125
    [ 2^-21 ] 4.76837158203125e-7
    [ 2^-22 ] 2.384185791015625e-7
    [ 2^-23 ] 1.1920928955078125e-7
    
    sum = 0.5049999952316284
    



    Note the number of digits the browser presents to the right of the D.P.

    To determine if there might be any issues while adding, (remember we use the PC to perform the addition, which is using the same questionable Floating Point to perform that calculation) I added the summation intermediate step.

    I've added the element location beneath the addend to ease locating elements ref  D:0123456

    // Chrome browser
    2^0 = 1 add leading implicit 1
    str = [ 10000001010001111010111 ] 
    
    [ 2^-1 ]
    0.5
    - - - - - - - - - - - - - - - - - - - -
    0.5
    
    [ 2^-8 ]
    0.00390625
    D:12345678901234567890
    - - - - - - - - - - - - - - - - - - - -
    0.50390625
    
    [ 2^-10 ]
    0.0009765625
    D:12345678901234567890
    - - - - - - - - - - - - - - - - - - - -
    0.5048828125
    
    
    [ 2^-14 ]
    0.00006103515625
    D:12345678901234567890
    - - - - - - - - - - - - - - - - - - - -
    0.50494384765625
    
    [ 2^-15 ]
    0.000030517578125
    D:12345678901234567890
    - - - - - - - - - - - - - - - - - - - -
    0.504974365234375
    
    
    [ 2^-16 ]
    0.0000152587890625
    D:12345678901234567890
    - - - - - - - - - - - - - - - - - - - -
    0.5049896240234375
    
    
    [ 2^-17 ]
    0.00000762939453125
    D:12345678901234567890
    - - - - - - - - - - - - - - - - - - - -
    0.5049972534179688
    
    
    [ 2^-19 ]
    0.0000019073486328125
    D:12345678901234567890
    - - - - - - - - - - - - - - - - - - - -
    0.5049991607666016
    
    
    [ 2^-21 ]
    4.76837158203125e-7
    D:12345678901234567890
    - - - - - - - - - - - - - - - - - - - -
    0.5049996376037598
    
    [ 2^-22 ]
    2.384185791015625e-7
    D:12345678901234567890
    - - - - - - - - - - - - - - - - - - - -
    0.5049998760223389
    
    [ 2^-23 ]
    1.1920928955078125e-7
    D:12345678901234567890
    - - - - - - - - - - - - - - - - - - - -
    
    - - - - - - - - - - - - - - - - - - - -
    - - - - - - - - - - - - - - - - - - - -
    D:12345678901234567890
    0.5049999952316284
    
    toFixed(2)
    0.50
    
    // Chrome browser
    



    Note that the browser output renders up to 19 base ten numerals to the right of the D.P. and will default to 'E' notation when significantly small a value.

        document.write( sum.toString(10) );
    
      // it appears that the toString() function has no effect on the formatting in this case
        document.write( sum );
    
    Findings:
    
    Chrome output no format specifier, but 16 numerals past the D.P. and up to 19 used during rendering to get to this final value
    0.5049999952316284
    
    
    Espruino output no format specifier, but truncates (up to) 11 numerals
    
    0.50499999523
    





    Now let's compare to the Espruino output

    // Espruino output
    2^0 = 1   add leading implicit 1
     
     
    [ 2^- 1 ]
    0.5
     
    D:12345678901234567890
    -------------------------
    0.5
     
     
    [ 2^- 8 ]
    0.00390625
     
    D:12345678901234567890
    -------------------------
    0.50390625
     
     
    [ 2^-10 ]
    0.0009765625
     
    D:12345678901234567890
    -------------------------
    0.5048828125
     
     
    [ 2^-14 ]
    0.00006103515
     
    D:12345678901234567890
    -------------------------
    0.50494384765
     
     
    [ 2^-15 ]
    0.00003051757
     
    D:12345678901234567890
    -------------------------
    0.50497436523
     
     
    [ 2^-16 ]
    0.00001525878
     
    D:12345678901234567890
    -------------------------
    0.50498962402
     
     
    [ 2^-17 ]
    0.00000762939
     
    D:12345678901234567890
    -------------------------
    0.50499725341
     
     
    [ 2^-19 ]
    0.00000190734
     
    D:12345678901234567890
    -------------------------
    0.50499916076
     
     
    [ 2^-21 ]
    0.00000047683
     
    D:12345678901234567890
    -------------------------
    0.50499963760
     
     
    [ 2^-22 ]
    0.00000023841
     
    D:12345678901234567890
    -------------------------
    0.50499987602
     
     
    [ 2^-23 ]
    0.00000011920
     
    D:12345678901234567890
    -------------------------
    0.50499999523
    
    toFixed(2)
    0.50
    
    // Espruino output
    



    Using our online floating point generator, we can see that our input of 1234.505 using either precision presents a number that should round to the hundredths place. The value 1234.5050048828125 contains enough bits to the right of our last thousandths numeral '5' to allow for a round up. During the conversion back to decimal, several bits beyond 2^-23 are lost, leaving us with a value that is just shy of our original '5' thousandths place. 1234.50499999



    As the conversion to and from F.P. introduces a slight bit of error, It would be nice if we had a right of the D.P. adder that works using integers only. This could assist in resolving if there is additional slop during the summation of the equivalent right of D.P. bits. Anyone up for the task to create that little wonder, while I battle cleaning up these snippets to allow posting here?



    The Html code file presents the conversion to a floating point value and uses color hinting to show the concatenation of bits to form the register value used in math calculations. It was a quick hack to compare summation output against Espruino output. Needs a bit of refactoring and renders more of a what's going on under the hood. Can be used to check conversions back to decimal along with toFixed() checks.

  • in General
    Avatar for Robin

    Fri 2019.08.23

    Hello @user102674 and welcome to the world of Espruino!

    'Is it possible to view the already running code?

    From the main page:
    Menu >> Documentation >> Quick Start
    Menu >> Documentation >> Videos

    https://www.espruino.com/Quick+Start
    WebIDE video interaction around ~01:58
    https://www.espruino.com/Quick+Start+USB­
    https://www.youtube.com/user/espruino

    which links to

    https://www.espruino.com/Quick+Start+Cod­e

    I believe you will find the environment meets your criteria. Watch the video and lets see if there are more get started questions you may have.

  • in ESP8266
    Avatar for Robin

    Went to check out your charity work @nhlives and on which continent, but found link is bad. Missing a 't' in protocol ref #7 link

Actions