Difference between revisions of "User:Piphi/Asymptote/piphi"
< User:Piphi | Asymptote
m (pigments) |
|||
Line 3: | Line 3: | ||
My own unofficial Asymptote module with some useful functions: | My own unofficial Asymptote module with some useful functions: | ||
+ | ==Documentation== | ||
<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. | ||
− | <syntaxhighlight lang="Asymptote">string [] array (string s) { | + | |
− | + | <math>\texttt{int length (int i);}</math> | |
− | + | :Returns the number of digits in the given integer. | |
− | + | ||
+ | <math>\texttt{int int (string s);}</math> | ||
+ | :Converts strings into integers. | ||
+ | |||
+ | ==Source Code== | ||
+ | <syntaxhighlight lang="Asymptote" line> | ||
+ | /** | ||
+ | * The piphi module | ||
+ | * | ||
+ | * Made by piphi | ||
+ | */ | ||
+ | |||
+ | string [] array (string s) { | ||
+ | string[]strarray; | ||
+ | for (int i=0;i<length(s);++i) { | ||
+ | strarray[i]=substr(s,i,1); | ||
} | } | ||
return strarray; | return strarray; | ||
− | } | + | } |
− | + | int length (int i) { | |
− | + | int j=0; | |
− | |||
− | |||
bool brk=false; | bool brk=false; | ||
do { | do { | ||
− | + | ++j; | |
if (i/(10**j) < 1) { | if (i/(10**j) < 1) { | ||
− | + | brk=true; | |
} | } | ||
} while (!brk); | } while (!brk); | ||
return j; | return j; | ||
− | } | + | } |
− | + | int int (string s) { | |
− | + | string[]a=array(s); | |
− | + | int b; | |
− | |||
− | |||
for (int i=0;i<a.length;++i) | for (int i=0;i<a.length;++i) | ||
− | + | b+=(ascii(a[i])-48)*10**(a.length-i-1); | |
− | + | return b; | |
}</syntaxhighlight> | }</syntaxhighlight> | ||
− | |||
− |
Latest revision as of 22:22, 30 October 2020
![[asy]label(scale(2)*"The \texttt{piphi} module");[/asy]](http://latex.artofproblemsolving.com/5/6/8/568323f6c37143ff41d868f8a5353b3e7f3cbaca.png)
My own unofficial Asymptote module with some useful functions:
Documentation
- 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.
- Returns the number of digits in the given integer.
- Converts strings into integers.
Source Code
1 /**
2 * The piphi module
3 *
4 * Made by piphi
5 */
6
7 string [] array (string s) {
8 string[]strarray;
9 for (int i=0;i<length(s);++i) {
10 strarray[i]=substr(s,i,1);
11 }
12 return strarray;
13 }
14
15 int length (int i) {
16 int j=0;
17 bool brk=false;
18 do {
19 ++j;
20 if (i/(10**j) < 1) {
21 brk=true;
22 }
23 } while (!brk);
24 return j;
25 }
26
27 int int (string s) {
28 string[]a=array(s);
29 int b;
30 for (int i=0;i<a.length;++i)
31 b+=(ascii(a[i])-48)*10**(a.length-i-1);
32 return b;
33 }