Register to post in forums, or Log in to your existing account
 

Play RetroMUD
Post new topic  Reply to topic     Home » Forums » Finished MUD Scripts
Driver256
Newbie


Joined: 21 Oct 2005
Posts: 8

PostPosted: Thu Oct 27, 2005 12:12 pm   

Math functions for large and floating point numbers
 
After much searching of these wonderful forums, and several hours of finding and reading VBScript docs, I believe that I have found a simple way to overcome the limitations of ZMud when it comes to doing math with large and floating point numbers.

I saw a script or two touching on the subject in the forums already, but the aliases were long and cumbersome. Using VBScript, it seems almost too easy:

Code:

#SCRIPT {function add(x,y):add=x+y:end function}
#SCRIPT {function subtract(x,y):subtract=x-y:end function}
#SCRIPT {function multiply(x,y):multiply=x*y:end function}
#SCRIPT {function divide(x,y):divide=x/y:end function}
#SCRIPT {function power(x,y):power=x^y:end function}


Now to use the functions, the ZMud syntax is %mss(multiply(x,y)), etc.

This works with zmud string variables which represent big numbers! Proper floating point math seems to be performed with long-double precision and the answer is returned as a Variant data-type by VBScript, which Zmud puts into a string.

For example,

Code:
temp=123456789012
temp2=1000
#show @temp x @temp2 = %mss(multiply(@temp,@temp2))


displays

123456789012 x 1000 = 123456789012000


Code:
temp=123456;temp2=232
#show @temp / @temp2 = %mss(divide(@temp,@temp2))


shows

123456 / 232 = 532.137931034483

But, in Zmud:

Code:
#show %eval(123456/232)
532

#show %eval(123456/%float(232))
532.137939453125


Note that ZMud does not agree with Window's Calculator.


Code:
temp2=23
#show @temp ^ @temp2 = %mss(power(@temp,@temp2))


returns

123456 ^ 23 = 1.27285999580857E+117


Code:
temp=12345678;temp2=12345678
#show @temp * @temp2 = %mss(multiply(%string(@temp),%string(@temp2)))


properly parses the strings, and returns

12345678 * 12345678 = 152415765279684

(which, naturally, agrees with Window's Calculator)

By contrast, try it in ZMud and you get this:

Code:
#show @temp x @temp2 != %eval(@temp*@temp2)
12345678 x 12345678 != 260846532

#show @temp x @temp2 != %eval(@temp*%float(@temp2))
12345678 x 12345678 != 152415772868608



I hope this is as useful to someone as it has been to me.

-Driver256 (Berndt on Realms of Despair)


Last edited by Driver256 on Fri Oct 28, 2005 6:31 am; edited 1 time in total
Reply with quote
Tech
GURU


Joined: 18 Oct 2000
Posts: 2733
Location: Atlanta, USA

PostPosted: Thu Oct 27, 2005 2:36 pm   
 
I just tried this and it worked like a charm. One question though, how do I see a list of the script I have define. It wasn't immediately obvious to me.
_________________
Asati di tempari!
Reply with quote
Driver256
Newbie


Joined: 21 Oct 2005
Posts: 8

PostPosted: Thu Oct 27, 2005 9:26 pm   
 
From the help file for the #script command:

Quote:
SCRIPT

Syntax: #SCR [script] [language]

If script is omitted the global functions, procedures, subs, etc. of the currently active language are displayed.


So, to list your scripts, type '#script'.

Thanks for the feedback. :)

Driver256 (Berndt)
Reply with quote
Driver256
Newbie


Joined: 21 Oct 2005
Posts: 8

PostPosted: Fri Oct 28, 2005 5:25 pm   More large math functions
 
The functions above seem to do the trick pretty well. The thing is, those big numbers are stored by ZMud as strings. So you cannot perform any comparisons or functions on them in ZScript with consistent results. For example,

Code:
temp=234556789012
temp2=1234567890123456
#show %eval(@temp < @temp2)


Returns 0 (false), even though the first number is smaller than the second. This is because when compared as strings (which they are) the first character "2" of the first 'number' is greater in ascii than the "1" in the second. It is doing alpabetical string comparison and not actually comparing the numerical values.

Code:
#show %eval(%float(@temp) < %float(@temp2))


doesn't work either. Note that:

Code:
#show %float(@temp)


Returns the bizzare answer of 2147483648.0. Yes, it's true, ZMud can't do big numbers. So, to compare these big 'strings' we can once again use VBScript. The syntax is simple:

Code:
#SCRIPT {Function islessthan(x,y):islessthan = (x<y):End Function}
#SCRIPT {Function isgreaterthan(x,y):isgreaterthan = (x>y):End Function}


Now try:

Code:
temp=234556789012
temp2=1234567890123456
#show %mss(islessthan(@temp,@temp2))


Which correctly returns -1 (true).

Therefore, use these VBScript functions in your #IF and other conditional statements when working with big numbers that are stored as strings.

-Berndt
Reply with quote
Vitae
Enchanter


Joined: 17 Jun 2005
Posts: 673
Location: New York

PostPosted: Tue Jul 14, 2009 11:54 pm   
 
*worship*

Since this is my 1st touch of #SCRIPT i have a question:
I don't seem to find the scripts in the settings when i do a search for like islessthan
I know I can type #script and get it listed, but where exactly is it?
Will i have to input all the #SCRIPTs if i shut down and restart zMud?
_________________
http://www.Aardwolf.com
Reply with quote
Driver256
Newbie


Joined: 21 Oct 2005
Posts: 8

PostPosted: Wed Jul 15, 2009 6:20 am   
 
Hi.

I haven't tinkered with ZMud for a few years now, but I'll try to give you an answer. :) Keep in mind that I don't know what version of ZMud you are using, and you may very well have a newer version that I know nothing about. Nevertheless, here we go!

Quote:

I don't seem to find the scripts in the settings when i do a search for like islessthan


Quote:

#SCRIPT {Function islessthan(x,y):islessthan = (x<y):End Function}


I'm not sure what you're getting at here. The "islessthan" here is just a VBScript function name defined by the user (by me). The only Zmud command in that line is #SCRIPT. The rest is VBScript and you probably wouldn't find much VBScript command syntax documented within the ZMud docs or helps. Here I am defining a VBScript function which I happened to name "islessthan" so that I can use it later in a ZMud script using the %mss function.

Quote:

I know I can type #script and get it listed, but where exactly is it?


The #SCRIPT command adds code to the Microsoft Script control (VBScript, Javascript). Microsoft Script control simplifies the use of active scripting engines and allows ZMud to run scripts in other automation languages like VBScript. Any scripting language that can be loaded into the Microsoft Script control can be used.

The ZMud #SCRIPT command adds procedures, subs, functions, etc. to the current scripting language. These "scripts" are stored in memory when the #SCRIPT command is executed.

To execute script using the Microsoft Script control use the #MSS command. #MSS with no paramaters resets the Script control object, which removes all variables, functions, etc. Evaluate expressions using the Script control with %mss.

Quote:

Will i have to input all the #SCRIPTs if i shut down and restart zMud?


Sort of. You include them in the appropriate ZMud settings files. That way they are automatically loaded when you join the mud or when you load a character, depending on which settings file(s) you put them in. Of course, you only would only load the scripts that you need to define for use later since #SCRIPTs are only useful in conjunction with a corresponding #MSS command or %mss function.

You can also enter them at the command line if you want to test things in real time. To enter a series of #SCRIPTs at the command line, you could put all the commands in a text file and execute it using the READ command.

I hope this helps.

-Berndt


Last edited by Driver256 on Fri Jul 17, 2009 1:10 pm; edited 2 times in total
Reply with quote
Vitae
Enchanter


Joined: 17 Jun 2005
Posts: 673
Location: New York

PostPosted: Wed Jul 15, 2009 10:49 pm   
 
Driver256 wrote:
The rest is VBScript and you probably wouldn't find much VBScript command syntax documented within the ZMud docs or helps.

Was talking about in the Settings...
like open zmud, click settings, got that lil bar at the top for searching in the setting for codes...
Didn't mean like documented in docs :-)
No worries tho, so, i input the #script and other than typing #script it's the only to see it.
That and putting it in an alias to load it or a help file to show me.
I know myself, if i don't have a way to see how to use it everytime i won't know wtf i'm doing :-)

Either way, thank you so much for the scripts.
was working for on a script needed to be able to work with huge #'s and can't afford cmud. this script was exactly what i needed.
_________________
http://www.Aardwolf.com
Reply with quote
Taz
GURU


Joined: 28 Sep 2000
Posts: 1395
Location: United Kingdom

PostPosted: Thu Jul 16, 2009 8:15 pm   
 
Put all your #script commands in an atconnect alias.

#alias atconnect {#SCRIPT {function add(x,y):add=x+y:end function}
#SCRIPT {function subtract(x,y):subtract=x-y:end function}
#SCRIPT {function multiply(x,y):multiply=x*y:end function}
#SCRIPT {function divide(x,y):divide=x/y:end function}
#SCRIPT {function power(x,y):power=x^y:end function}
#SCRIPT {Function islessthan(x,y):islessthan = (x<y):End Function}
#SCRIPT {Function isgreaterthan(x,y):isgreaterthan = (x>y):End Function}}
_________________
Taz :)
Reply with quote
Vitae
Enchanter


Joined: 17 Jun 2005
Posts: 673
Location: New York

PostPosted: Fri Jul 17, 2009 3:47 am   
 
yeap, figured that, but since i don't need them on a continuous basis, easier to just load them into an alias and load them when i do need them.
_________________
http://www.Aardwolf.com
Reply with quote
Vitae
Enchanter


Joined: 17 Jun 2005
Posts: 673
Location: New York

PostPosted: Mon Jul 27, 2009 4:06 pm   
 
1st, let me start of saying that I'd tried this while being awake for over 36hrs, and I am currently not even home near zmud for the next 2 weeks.

#SCRIPT {Function islessthan(x,y):islessthan = (x<y):End Function}

#var test 14235468435

#if (%mss(islessthan(@test,0))) {#echo YAY} {#Echo BOO}
BOO

#var ZeroOut 0
#if (%mss(islessthan(@test,@ZeroOut))) {#echo YAY} {#Echo BOO}
BOO

*boggle*

Not sure what's up.
_________________
http://www.Aardwolf.com
Reply with quote
Driver256
Newbie


Joined: 21 Oct 2005
Posts: 8

PostPosted: Mon Jul 27, 2009 7:03 pm   
 
I'm not sure what the confusion is. Your code is working fine...

Quote:

#var test 14235468435

#if (%mss(islessthan(@test,0))) {#echo YAY} {#Echo BOO}
BOO


This is correct. @test is not less than zero, therefore the conditional fails and returns BOO.

Quote:

#var ZeroOut 0
#if (%mss(islessthan(@test,@ZeroOut))) {#echo YAY} {#Echo BOO}
BOO


Same here, of course. @test is still not less than zero, so the conditional fails and returns BOO again.

Quote:

*boggle*


*ditto*
Reply with quote
Vitae
Enchanter


Joined: 17 Jun 2005
Posts: 673
Location: New York

PostPosted: Mon Jul 27, 2009 7:07 pm   
 
...guess i didn't think of it in the correct way...i mean...
I thought it was if @test < 0 was "Yes it is" then it would do the Yay...
not "no it is NOT less than zero and it "failed" with a boo...
hrmm..
thanks..
_________________
http://www.Aardwolf.com
Reply with quote
Driver256
Newbie


Joined: 21 Oct 2005
Posts: 8

PostPosted: Mon Jul 27, 2009 7:37 pm   
 
Quote:

I thought it was if @test < 0 was "Yes it is" then it would do the Yay...


Well, that's right.

If "@test < 0" was true then it would do the yay. But, of course, it's not true.

@test is not less than 0.

Think of any conditionals as being either TRUE or FALSE. No matter how complicated the conditional statement, it all boils down to that. So, for example, the statement "12 < 0" is FALSE -- 12 is not less than zero. If you think of it this way then it becomes simple to see which branch will be executed by the #IF statement.

I hope that doesn't make it any more confusing. :/
Reply with quote
Vitae
Enchanter


Joined: 17 Jun 2005
Posts: 673
Location: New York

PostPosted: Mon Jul 27, 2009 8:54 pm   
 
Works for me.
Unless i've gone totally mental i've got normal non-#script #IF < or > types of scripts and they work the way I expected this one to work.
i've just got to think in reverse.

so like @cashonhand < 0 {#Echo Unable to afford anything else} {execute the rest of the script}
_________________
http://www.Aardwolf.com
Reply with quote
Progonoi
Magician


Joined: 28 Jan 2007
Posts: 430

PostPosted: Mon Jul 27, 2009 9:34 pm   
 
Why do you have to think in reverse?

@cashonhand > 0 {execute the script} {#ec Unable to afford anything else}

Wouldn't it make more sense that way anyway?
_________________
The Proud new owner of CMud.

--------------------------------
Intel Core i5-650 3,2GHz
4 DD3 RAM
GTX 460 768MB
Win 7 Home Premium 64x
--------------------------------
Reply with quote
Fang Xianfu
GURU


Joined: 26 Jan 2004
Posts: 5155
Location: United Kingdom

PostPosted: Tue Jul 28, 2009 9:48 am   
 
I think this is a factor of your sleep deprivation Vitae. They're both identical:

#if (%mss(islessthan(@test,@ZeroOut))) {#echo YAY} {#Echo BOO}
#if (@test < @ZeroOut) {#echo YAY} {#echo BOO}

will both give the "boo" result, because @test has the value 14235468435, which isn't less than 0. To put it in terms of your cash script, it'd be:

#if (%mss(islessthan(@cashonhand,0))) {#echo not enough cash} {stuff}
#if (@cashonhand < 0) {#echo not enough cash} {stuff}

Which will both work exactly the way you expect. There's nothing unusual happening here, you probably just got what you meant by "yay" and "boo" mixed up - in this case, "yay" meant you didn't have enough gold, just like it did in your original script where the first result was the "out of cash" one.
_________________
Rorso's syntax colouriser.

- Happy bunny is happy! (1/25)
Reply with quote
Vitae
Enchanter


Joined: 17 Jun 2005
Posts: 673
Location: New York

PostPosted: Tue Jul 28, 2009 3:55 pm   
 
Thanks :-)
this is what happens when i haven't done any type of coding in nearly a year, then jump into something new with the #script and on top of that, add in no sleep.

for some reason i was just thinking of it in the sense of like i said:

Why yes I DO have more $ than 0 YAY!
maybe that's cause that's what i do in real life :-)

I found a dollar!
I have more than $0 now!
YAY!
_________________
http://www.Aardwolf.com
Reply with quote
Display posts from previous:   
Post new topic   Reply to topic     Home » Forums » Finished MUD Scripts All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

© 2009 Zugg Software. Hosted by Wolfpaw.net