Menu

#4 Fix for Undefined Property warning

v1.0 (example)
open
nobody
None
5
2017-11-02
2017-11-02
Rob
No

https://sourceforge.net/p/bcmath-js/code/HEAD/tree/src/libbcmath.js#l154

Line 154 will throw an "undefined property" warning to the browser console, since the last repetition of the while loop will check for an out-of-range ptr index in str.

while ((str[ptr]) % 1 === 0)

Easy fix, just use a range-limited for loop instead, works a treat.

for(; ptr<str.length; ++ptr)

Cheers

Discussion

  • Rob

    Rob - 2017-11-02

    Of course, the ptr then doesn't have to be incremented inside the loop anymore.

    for(;ptr < str.length;++ptr)
    { //bcmath.isdigit(str[ptr])) {
        //ptr++;
        strscale++;    /* digits */
    }
    
     
  • Rob

    Rob - 2017-11-02

    I just noticed that there will be more "undefined property" warnings if no decimal point is used, i.e. when multiplying 10 and 10 instead of 10.0 and 10.0 ... the following snippet takes care of everything, including the original issue mentioned above, replace lines 144 - 157 with this:

    //while (bcmath.isdigit(str[ptr]))
    while((ptr<str.length) && ((str[ptr]) % 1 === 0))
    { //bcmath.isdigit(str[ptr])) {
        ptr++;
        digits++;    /* digits */
    }
    
    if((ptr<str.length) && (str[ptr] === '.'))
    {
        ptr++;            /* decimal point */
    }
    
    //while (bcmath.isdigit(str[ptr])) {
    //while((str[ptr]) % 1 === 0)
    for(;ptr < str.length;++ptr)
    { //bcmath.isdigit(str[ptr])) {
        //ptr++;
        strscale++;    /* digits */
    }
    
     

Log in to post a comment.