简体   繁体   中英

How to count number of places right of decimal for a list of values in Igor Pro

I'm using Igor Pro (I don't use this often). I'm trying to count the number of places to the right of a decimal, if there is one, for one wave. And then add a decimal place however many places to the left, that were in the first wave, to a second wave. For reference I attached a pic of what I currently have. The wave "test_correct" is what it SHOULD look like, and "fix_err" is what I'm currently outputting. I've been at this for awhile but can't seem to figure it out, any help would be appreciated. Thanks example

Function testErrFix()

    wave test_energy, test_err
    variable i, j, s
    variable len = numpnts(test_energy)
    make/O/D/N=(len) fix_err
    string current_Energy
    string current_Err
    string Error
    variable slen
    For(i=0;i<len;i+=1)
        current_Energy = num2str(test_energy[i])
        current_Err = num2str(test_err[i])
        slen = strlen(current_energy)
        Error = ""
        For(j=0;j<slen;j+=1)
          If(Stringmatch(current_Energy[j], ".")==1)
           For(s=j;s<slen;s+=1)
                 Error += "." + current_Err[s-4] + current_Err[s-3] + current_Err[s-2]
                EndFor
           ElseIf(Stringmatch(current_Energy[j], "")==1)
             Error += current_Err[s+1]
                For(s=j;s<slen;s+=1)
                 Error = current_Err[s]
                EndFor
                EndIf
            EndFor
            fix_err[i] = str2num(Error)
            EndFor  
    
End

Your example uses num2str incorrectly. By default this only returns 5 decimal places, see its documentation. You can also tweak the number of digits shown in the table.

Could you ellaborate what you want to achieve? I never had the need to know the number of digits right of the decimal point so far.

EDIT: The number of decimal places depends on the way you convert the number to a string. So in general this will not work.

A naive implementation

Function/S num2strHighPrec(variable num)

    string str
    sprintf str, "%.15f", num

    return str
End

Function/S RemoveEndingExhaustive(string str, string ending)

    string result

    for(;;)
        result = RemoveEnding(str, ending)

        if(!cmpstr(result, str) || strlen(result) == 0)
            return result
        endif

        str = result
    endfor
End

Function CountDecimalPlaces(variable num)

    string numAsStr
    variable pos

    numAsStr = num2strHighPrec(num)
    numAsStr = RemoveEndingExhaustive(numAsStr, "0")
    pos = strsearch(numAsStr, ".", 0)
    
    if(pos < 0)
        return 0
    endif
    
    return strlen(numAsStr) - 1 - pos
End

might be a good starting point.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM