Real number variables?

Started by Kennedy, Sat 25/10/2003 07:05:44

Previous topic - Next topic

Kennedy

Edit by strazer:

AGS 2.7 Beta 8 introduced the float data type.




Would it be possible to add support for real numbers rather than just integers?
This would allow for complex mathemeatical equations like natural logs, trigonometry funtions, and exponenetials.

Fuzzpilz

#1
Well, no. You can't accurately represent irrational numbers. I'm assuming you're asking for floating-point numbers; CJ is not planning on adding these as far as I'm aware. Try using fixed point numbers and power series.

For example:

sin(x) = x^1/1! - x^3/3! + x^5/5! - x^7/7! + ...
cos(x) = x^0/0! - x^2/2! + x^4/4! - x^6/6! + ...

You can use these to approximate, which is what computers and calculators do.

If you don't want to, or need more accuracy, get somebody to write a plugin for you that would add support for floating point numbers. It shouldn't take more than maybe half an hour. (I think I have one lying about somewhere that I made a while ago, I'll post again later)

Fuzzpilz

Here. (If you need to rename it, that's no problem. The new filename has to start with AGS, though.)

Here's how it works: it uses AGS' int variables to store 32-bit floating point numbers. The following script functions are imported:

int int_to_float(int);

Converts a normal integer to a floating point number.

int float_to_int(int);

Converts a floating point number back to a normal integer.

int fadd(int,int);
int fmul(int,int);
int fdiv(int,int);

Adds, multiplies, divides two floating point numbers.

int fcomp(int,int);

Compares floating point numbers. Returns 0 if they're equal, -1 if the first is smaller, 1 if the second is smaller. Note that floating point maths aren't always very accurate, so you might be better off seeing if they're within a reasonable distance of one another.

int sin(int);
int cos(int);
int exp(int);
int log(int);
int pow(int,int);
int sqrt(int);

What it says on the box.

There is no fabs() because the standard abs() should work just fine. (unless it turns out AGS doesn't have one; in which case the usual way of replacing it should work)

Kweepa

Nice - this could be very useful!

PS. I'm not sure why you mentioned irrational numbers, since Kennedy didn't. Real and floating point numbers are pretty much the same thing. You can't "accurately" represent a lot of rational numbers either. See
http://mathworld.wolfram.com/RealNumber.html
Still waiting for Purity of the Surf II

Fuzzpilz

I know that. I have no idea what I was thinking up there, but it was probably stupid. I need to get somebody to shoot me in the head before I get a chance to press "Post" next time.

Kweepa

Still waiting for Purity of the Surf II

Isegrim

#6
Fuzz, the plugin is really, really useful...
Although I still hope that floats will eventually become part of AGS itself, for it is still a bit annoying to write for example "fmul()" instead of a single * , and not to be able to use floats and ints in the same equation...

So let's hope CJ is inspired by your work ;)

Another thing:

is it me, or is the pow function a bit wrong?
Quote
float_to_int(pow(int_to_float(3),int_to_float(3)))
delivers 20...

This post was generated automatically and therefore bears no signature.

SMF spam blocked by CleanTalk