User:Piphi/Asymptote/piphi

< User:Piphi‎ | Asymptote
Revision as of 22:12, 30 October 2020 by Piphi (talk | contribs) (pigments)
[asy]label(scale(2)*"The \texttt{piphi} module");[/asy]

My own unofficial Asymptote module with some useful functions:

$\texttt{string [] array (string s);}$

This function is actually part of newer versions of Asymptote, but is not supported in 2.32 so I decided to make it. What it does is it takes a string and splits it into individual characters.
string [] array (string s) {
	string[]strarray;
	for (int i=0;i<length(s);++i) {
    	strarray[i]=substr(s,i,1);
    }
    return strarray;
}

$\texttt{int length (int i);}$

returns the number of digits in the given integer.
int length (int i) {
	int j=0;
    bool brk=false;
    do {
    	++j;
        if (i/(10**j) < 1) {
        	brk=true;
        }
    } while (!brk);
    return j;
}

$\texttt{int int (string s);}$

converts strings into integers.
int int (string s) {
	string[]a=array(s);
	int b;
    for (int i=0;i<a.length;++i)
    	b+=(ascii(a[i])-48)*10**(a.length-i-1);
	return b;
}

More to come...