String functions

About advanced scenes, and the Thyme scripting language used in Algodoo.

String functions

Postby Kilinich » Sun Oct 16, 2011 8:47 pm

First what I've try with strings is to make display few digits function:

Code: Select all
scene.my.digits := (x,n) => {l := string.str2list(math.toString(x)); dp := 0; for(8, (i) => {l(i) == "." ? {dp = i+1} : {}}); dp == 0 ? {math.toString(x)}:{r := "";for(dp+n, (i) => {r = r+l(i)}); r}}


Result is:
> scene.my.digits (math.pi,2)
3.14
Dream of Algodoo as game development engine...
User avatar
Kilinich
[Best bug reporter 2010]
 
Posts: 2098
Joined: Mon Aug 31, 2009 8:27 pm
Location: South Russia

Re: String functions

Postby KarateBrot » Fri Nov 18, 2011 11:51 pm

Is it to cut down float numbers? if yes try this:

Code: Select all
scene.my.digits := (x, n) => {(x * 10^n - x * 10^n % 1) / 10^n}
Image
User avatar
KarateBrot
 
Posts: 825
Joined: Mon Aug 31, 2009 7:32 pm
Location: Germany

Re: String functions

Postby tatt61880 » Tue Nov 22, 2011 3:23 pm

Firstly, thanks Kilinich for telling us the existence of string.* :thumbup:
Secondary, I like KarateBrot's approach. :thumbup:
Thirdly, both script have some issues. ;)

----
Issues with Kilinich's code:
> Scene.my.digits(2.011, 2)
2131071 ms: - WARNING - Failed to evaluate: l(i) == "." ? {dp = i + 1} : {}, List index out of bounds
2.01

> Scene.my.digits(math.pi, 9)
2047689 ms: - WARNING - Failed to evaluate: r = r + l(i), List index out of bounds
3.1415927

I tried to solve these issue by next code.
Code: Select all
scene.my.digits := (x,n) => {strlen_x := string.length(math.toString(x)); l := string.str2list(math.toString(x)); dp := 0; for(strlen_x, (i) => {l(i) == "." ? {dp = i+1} : {}}); dp == 0 ? {math.toString(x)}:{r := "";for(dp+n, (i) => {r = r+((i >= strlen_x) ? {"0"} : {l(i)})}); r}}


----
Issues with KarateBrot's code:
Scene.my.digits(2.011, 2) returns 2.01 <-OK
Scene.my.digits(2.011, 3) also returns 2.01
Scene.my.digits(2.011, 4) returns 2.011
Scene.my.digits(2.011, 5) returns 2.01099
Scene.my.digits(2.011, 50) returns NaN
Scene.my.digits(math.pi, 9) returns 3.1415927

I don't know the way to solve this.
NOTE: I'm not an Algoryx member.
Hi, Algodoo lovers. Have you read next topic? Featured scenes suggestions
To translators: English.cfg changelog will be useful (even for me).
User avatar
tatt61880
[Most Helpful Person 2010]
 
Posts: 1150
Joined: Mon Aug 31, 2009 5:45 pm
Location: Tokyo, Japan

Re: String functions

Postby KarateBrot » Tue Nov 22, 2011 3:50 pm

it's caused by rounding errors inside of algodoo i think.

especially with:
Scene.my.digits(2.011, 3) also returns 2.01
Scene.my.digits(2.011, 5) returns 2.01099

I guess it's 1. maybe because of rounding errors (digits just get cut away directly inside of the calculation) or/and 2. algodoo has some weird value issues for example if you enter 0.7 in the scripting menu you will get 0.69999999.

- - - - -
Scene.my.digits(2.011, 50) returns NaN

this is an issue with digits that get cut away too. algodoo calculates 10^50. it's too much to handle.
Image
User avatar
KarateBrot
 
Posts: 825
Joined: Mon Aug 31, 2009 7:32 pm
Location: Germany

Re: String functions

Postby tatt61880 » Wed Nov 23, 2011 5:18 am

Never mind about Scene.my.digits(2.011, 50) returns NaN.

Even if you input 1.1111111111111111111, thyme returns 1.1111112
50 digits are unnecessarily.

Edit:
Btw, I tried to implement printf function. ;)
Scene.my.printf = (format, arguments) =>{
ret = "";
args_i = 0;
for(string.length(format), (n)=>{
temp = string.str2list(format);
(temp(n) == "%") ? {ret=ret+arguments(args_i); args_i=args_i+1} : {ret=ret+temp(n)}
});
print(ret);
};
Scene.my.printf("% plus % equals %", [1, 2, 1+2]);
NOTE: I'm not an Algoryx member.
Hi, Algodoo lovers. Have you read next topic? Featured scenes suggestions
To translators: English.cfg changelog will be useful (even for me).
User avatar
tatt61880
[Most Helpful Person 2010]
 
Posts: 1150
Joined: Mon Aug 31, 2009 5:45 pm
Location: Tokyo, Japan

Re: String functions

Postby Kilinich » Wed Nov 23, 2011 7:49 am

tatt61880 wrote:Issues with Kilinich's code:
> Scene.my.digits(2.011, 2)
2131071 ms: - WARNING - Failed to evaluate: l(i) == "." ? {dp = i + 1} : {}, List index out of bounds
2.01


Then I've make this, we don't have string.length function, now we do have it so just need to update the code.
Dream of Algodoo as game development engine...
User avatar
Kilinich
[Best bug reporter 2010]
 
Posts: 2098
Joined: Mon Aug 31, 2009 8:27 pm
Location: South Russia

Re: String functions

Postby KarateBrot » Wed Nov 23, 2011 1:49 pm

we had a string length function before it just wasn't implemented in algodoo. grady once made an algorithm for it.
Image
User avatar
KarateBrot
 
Posts: 825
Joined: Mon Aug 31, 2009 7:32 pm
Location: Germany

Re: String functions

Postby tatt61880 » Wed Nov 23, 2011 2:03 pm

KarateBrot wrote:we had a string length function before it just wasn't implemented in algodoo. grady once made an algorithm for it.

It's not Grady but REMqb, is it?
Strings functions in algodoo
NOTE: I'm not an Algoryx member.
Hi, Algodoo lovers. Have you read next topic? Featured scenes suggestions
To translators: English.cfg changelog will be useful (even for me).
User avatar
tatt61880
[Most Helpful Person 2010]
 
Posts: 1150
Joined: Mon Aug 31, 2009 5:45 pm
Location: Tokyo, Japan

Re: String functions

Postby KarateBrot » Wed Nov 23, 2011 2:42 pm

Strings functions in algodoo doesn't detect string length automatically as far as i can see. you always need to enter it manually. but anyway...

on the 9th of november 2009 grady sent me a PM with his function that detects the length. i improved it a bit because it couldn't handle matrices and strings in lists as far as i can remember.
i used it in this forum post and sometimes it appeared in other posts.

if anyone is interested in how it works:
Code: Select all
Scene.my.ArraySize := (array)=>{
    a := array ++ ["end"];
    n := 0;
    SizeUnknown := true;
    for(40, (i)=>{
        SizeUnknown ? {
            ("" + math.toString(a(i))) == "end" ? {
                n = i;
                SizeUnknown = false
            } : {}
        } : {}
    });
    n
}

don't get me wrong. i don't want to sound like i want to take credit for it so again: it's grady's work not mine. i just used it several times and refined it.
but i like string.length() better. because it's an standard function now alone is reason enough that it's way cooler :thumbup:
Image
User avatar
KarateBrot
 
Posts: 825
Joined: Mon Aug 31, 2009 7:32 pm
Location: Germany

Re: String functions

Postby tatt61880 » Wed Nov 23, 2011 3:16 pm

KarateBrot wrote:but i like string.length() better. because it's an standard function now alone is reason enough that it's way cooler :thumbup:

Yes, we have to say thank you to emilk :thumbup:
NOTE: I'm not an Algoryx member.
Hi, Algodoo lovers. Have you read next topic? Featured scenes suggestions
To translators: English.cfg changelog will be useful (even for me).
User avatar
tatt61880
[Most Helpful Person 2010]
 
Posts: 1150
Joined: Mon Aug 31, 2009 5:45 pm
Location: Tokyo, Japan


Return to Thyme scripting

Who is online

Users browsing this forum: No registered users and 1 guest