Integers with Decimal Value

Started by , Sat 13/03/2004 22:00:16

Previous topic - Next topic

AGS Newbie

Hello everyone ! I've run into a bit of a snag working with integers and would appreciate any help you could give me. How do you declare an integer variable without a whole number value ?

For example:

int number=1.80;

Trying to declare the above just results in an expected, " ;".
Is there another way to do this ?

Thanks.

Alynn

the problem with that is, an integer can not have decimals, if it is a decimal it's no longer an integer... what you want is a float.

You will probably have to look up some plugins for floating operators... I think there may be one on the plugins page but you'd have to look to be sure.

AGS Newbie

Thanks for your input. There doesn't seem to be a plugin that will support this kind of float, unless I just cannot find one, which is unfortunate, because it might be difficult to work around the situation. Does AGS itself support this kind of feature at all ? Would I have to write my own plugin for AGS to support this ?

RickJ

Why not just scale everything by 100 or 1000?   A mod operator was added to the latest beta version to make wasy to obtain remainders.    

Fuzzpilz

#4
I recommend fixed point numbers - the newest betas of AGS finally have bitshift operators, so there should be no problems. They're basically the same thing RickJ recommends, except they use a power of 2 instead of 10, and are a bit faster and more accurate if done right - though they're also a bit harder to understand if you have little experience with such things.

If you absolutely have to use floats, I made a floating-point plugin a while back. It works by storing 32-bit (single precision) floating point numbers in AGS' integer variables. Because of the way these work, however, you can't use normal operators and functions with these variables without converting. It adds the following functions:

int float_to_int(int);
Interprets the argument as a float, rounds it and returns the normal int value.

int int_to_float(int);
Interprets the argument as a normal integer and returns a float of the same value (only as close as possible with single-precision floats, of course).

int fcomp(int,int);
Interprets the arguments as floats and returns 0 if they're equal, -1 if the first is smaller than the second, and 1 if the first is larger than the second.

int fadd(int,int);
int fmul(int,int);
int fdiv(int,int);
Floating point addition, multiplication and division.

int fabs(int);
Floating point absolute value.

int sin(int);
int cos(int);
Floating point sine and cosine.

int sqrt(int);
Floating point square root.

int exp(int);
int log(int);
Floating point exponential and natural logarithm.

int pow(int,int);
First argument to the "second argumentth" power. As with all the others, both have to be floats.

EDIT: Fixed some bugs I only just now found out about, added fabs because what I said in an earlier thread about normal abs working fine is nonsense.

SMF spam blocked by CleanTalk