Difference between revisions of "User:Piphi/Asymptote/piphi"

(Created page with "<center><asy>label(scale(2)*"The \texttt{piphi} module");</asy></center> My own unofficial Asymptote module with some useful functions: <math>\texttt{string [] array (string...")
 
m (pigments)
Line 5: Line 5:
 
<math>\texttt{string [] array (string s);}</math>
 
<math>\texttt{string [] array (string s);}</math>
 
: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.
 
: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.
<pre>string [] array (string s) {
+
<syntaxhighlight lang="Asymptote">string [] array (string s) {
 
string[]strarray;
 
string[]strarray;
 
for (int i=0;i<length(s);++i) {
 
for (int i=0;i<length(s);++i) {
Line 11: Line 11:
 
     }
 
     }
 
     return strarray;
 
     return strarray;
}</pre>
+
}</syntaxhighlight>
  
 
<math>\texttt{int length (int i);}</math>
 
<math>\texttt{int length (int i);}</math>
 
:returns the number of digits in the given integer.
 
:returns the number of digits in the given integer.
<pre>int length (int i) {
+
<syntaxhighlight lang="Asymptote">int length (int i) {
 
int j=0;
 
int j=0;
 
     bool brk=false;
 
     bool brk=false;
Line 25: Line 25:
 
     } while (!brk);
 
     } while (!brk);
 
     return j;
 
     return j;
}</pre>
+
}</syntaxhighlight>
  
 
<math>\texttt{int int (string s);}</math>
 
<math>\texttt{int int (string s);}</math>
 
:converts strings into integers.
 
:converts strings into integers.
<pre>int int (string s) {
+
<syntaxhighlight lang="Asymptote">int int (string s) {
 
string[]a=array(s);
 
string[]a=array(s);
 
int b;
 
int b;
Line 35: Line 35:
 
     b+=(ascii(a[i])-48)*10**(a.length-i-1);
 
     b+=(ascii(a[i])-48)*10**(a.length-i-1);
 
return b;
 
return b;
}</pre>
+
}</syntaxhighlight>
  
 
More to come...
 
More to come...

Revision as of 22:12, 30 October 2020

[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...