Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: paravantis on Mon 02/03/2009 20:27:23

Title: More mathematical functions and double precision variables?
Post by: paravantis on Mon 02/03/2009 20:27:23
AGS is amazing and tons of fun to use! I have already spent a few hours at it with my 10 year old son!

I have a rather unusual question to ask of the developers and the more experienced users. Is there any chance a fuller mathematical function set may be implemented in a future, e.g. including exp and log functions?

Also -and this is a related issue- is it at all likely that double precision variables may be implemented?

If so, AGS would be a fantastic (if somewhat unlikely) tool for educational applications, e.g. an adventure "game" into the world of high school Math or even freshman Statistics! I am salivating at dreaming of my freshmen trying to discover their way to a correct ...confidence interval!

Kindly bear with me if I am a bit off target here BUT I have been greatly excited and carried away by AGS's potential and kinda pondering about what may lie next in the adventure path...

John Paravantis
University of Piraeus
GREECE
Title: Re: More mathematical functions and double precision variables?
Post by: Trent R on Mon 02/03/2009 21:58:29
Haha, I like your enthusiam.

I believe there's a few Math modules around... lemme see if I can find them.
[Edit]: Dang. Seems I'm wrong... but nonetheless you can actually script a lot of math functions using AGS script. For example, this recent thread (http://www.adventuregamestudio.co.uk/yabb/index.php?topic=36714.0) for natural logs.


~Trent
Title: Re: More mathematical functions and double precision variables?
Post by: Pumaman on Mon 02/03/2009 23:18:02
There's a good chance that Log/Logn/Exp will be added to a future version.

However, it's unlikely that double-precision floating point will be added, as it's simply too specialist for a tool like AGS.
Title: why not drop single precision altogether in favor of double precision?
Post by: paravantis on Tue 03/03/2009 22:38:05
Thank you both for responding.

Why is it such a big deal to implement double precision?

Why not drop single precision altogether and base your programming of an upcoming version on double precision floating point routines?

If I am being naive, please do forgive me. It is just that in 2009, it would seem more natural to use double precision as the default floating number type in whatever programming environment you use (C/C++ or whatever).

Nevertheless, it would be amiss if I ended my post by not thanking you for considering the implementation of exp and log functions!

With friendly regards from Greece,
John Paravantis
University of Piraeus
Title: Re: More mathematical functions and double precision variables?
Post by: Pumaman on Tue 03/03/2009 22:59:36
It's a big deal because the scripting engine has been written to support single-precision floating point, and not double-precision floating point. Therefore, it would need the scripting engine to be updated to support the new type and all the associated gubbins.

It's possible, of course, but it involves a fair amount of work and is not a high priority since I think you're the only person that has requested it so far.
Title: Re: More mathematical functions and double precision variables?
Post by: Gilbert on Wed 04/03/2009 00:49:46
As far as I remember AGS doesn't support the long integer type either. If double precision float has to be supported we need to bring in long support at the same time (actually IMO for the target types of games created with AGS long is probably more useful). I doubt whether many people really need them so I don't know if it's really worth implementing them for the time being.


Actually, personally I would want better typecasting (so we can use less IntToFloat() and FloatToInt() ) instead but I it's also fine as it is at the moment.
Title: Re: More mathematical functions and double precision variables?
Post by: Dualnames on Wed 04/03/2009 08:17:30
Geia sou gianni. Welcome to the AGS Forums, it's always good to see a fellow greek. Some mathematical functions are missing from AGS due to its nature and the group of people its targetting. But I must agree log and exp are a must. Abs function is really easy to script, and I'd love to hand the code to you if you care for it.
Title: Re: More mathematical functions and double precision variables?
Post by: paravantis on Wed 04/03/2009 14:03:56
Dualnames, efcharisto poly for your kind words! I think I can manage coding the abs function, but if you have a snippet of code you could post, that would be great too!

Thank you all for contributing to this topic. It is too bad that coding double precision and long integers amounts to a lot of work.

I look forward to a future version that will contain a full set of (single precision) math functions.

John Paravantis
Title: Re: More mathematical functions and double precision variables?
Post by: Dualnames on Fri 06/03/2009 08:41:32
Quote from: paravantis on Wed 04/03/2009 14:03:56
Dualnames, efcharisto poly for your kind words! I think I can manage coding the abs function, but if you have a snippet of code you could post, that would be great too!

Thank you all for contributing to this topic. It is too bad that coding double precision and long integers amounts to a lot of work.

I look forward to a future version that will contain a full set of (single precision) math functions.

John Paravantis

function Abs(int number) {
String abs1=abs1.Format("%d,number");
if (abs1.IndexOf("-")!=-1) {
String abs2=abs1.ReplaceCharAt(0, '');
number = abs2.AsInt;
}
return number;
}

Scripted quite fast but I think it works.
Title: Re: More mathematical functions and double precision variables?
Post by: Gilbert on Fri 06/03/2009 09:00:22
String.Format() is static, so you don't call it using an existing String variable, and the quotes in the formating was placed wrongly.


function Abs(int number) {
  String abs1=String.Format("%d",number);
  if (abs1.IndexOf("-")!=-1) {
    String abs2=abs1.ReplaceCharAt(0, '');
    number = abs2.AsInt;
  }
  return number;
}


However, as IndexOf() scans the whole String for the appearance of a certain string, I'm not sure but I think it's not so economic. As the negative sign should only appear as the first entry, it's probably better to just check this. Also, I shortened something which may or may not save a bit on performance.

function Abs(int number) {
  String abs1=String.Format("%d",number);
  if (abs1.Chars[0]=='-') {
    abs1=abs1.ReplaceCharAt(0, '');
  }
  return abs1.AsInt;
}

Edit: Actually I'm not sure whether you can set a certain character to '' with ReplaceCharAt() to remove that character (as the manual doesn't say you can) I doubt if the codes work. Of course, you can change that '' to ' ' instead to change the negative sign to a space, which should still work with AsInt.

HOWEVER, I just don't understand, unless the AGS script engine is badly optimised for comparison of numbers (and here in particular, comparison with just 0) why would you do this in such complicated manner, with String and such? (IMO it could even be worse if you do all those String operations, either to performance and memory usage). I'd rather use the straight ahead and simple approach :P :

function Abs(int number) {
   if (number<0) number=-number;
  return number;
}

Title: Re: More mathematical functions and double precision variables?
Post by: Dualnames on Fri 06/03/2009 09:10:25
Nicely done there. Well, I always like the hard way :D :D :D
Title: Re: More mathematical functions and double precision variables?
Post by: paravantis on Fri 06/03/2009 10:08:02
You guys must be just about the nicest forum I have ever bumped into!

Well, my brute force engineering/modeling background would have lead me to the

number=-number

approach too BUT, I must give it to Dualnames (excellent name that), the twisted way was more fun! ;D

Thanks again to all of you,
John
Title: Re: More mathematical functions and double precision variables?
Post by: Dualnames on Fri 06/03/2009 10:12:12
I just justify the means to the cause.