Adventure Game Studio

AGS Support => Advanced Technical Forum => Topic started by: fovmester on Wed 12/04/2006 14:38:55

Title: Implicit Int to Float conversion (SOLVED)
Post by: fovmester on Wed 12/04/2006 14:38:55
I know it is possible already to explicitly convert ints to floats with the IntToFloat command, but in most languages this particular conversion is handled implicitly.

Ex:
float nbr = 4; //interpreted as float nbr = 4.0;

This would be really useful to make the code look less cluttered. I mean what looks best:

float nbr = IntToFloat(4)
or
float nbr = 4;

The answer is obvious. So if you have time, CJ, I hope you can implement this!
Title: Re: Suggestion: implicit Int to Float conversion
Post by: Pumaman on Wed 12/04/2006 18:15:54
or just do:

float nbr = 4.0;

You can use that method for setting literal values to floats; if you're setting a float to an int variable, I think IntToFloat is reasonable as a way to specify that you know you're changing type.
Title: Re: Implicit Int to Float conversion
Post by: fovmester on Sat 15/04/2006 07:02:08
Yes I know you can do that, but consider this (because I do this all the time):

function moveSomething(Object* o, float speed);

...
...
moveSomething(oThing, 4);

This will give an error obviously, but is something I type all the time. Ok, I know you could and SHOULD do an explicit type conversion, but there IS a reason for most programming languages to implement implicit type conversions of this type and it's because people tend to view 4 equal to 4.0. In fact mathematically integers ARE a subset of floats so it makes sense.

Anyway, if it's too much hassle then don't do it, CJ. I know you might have more important things to fix. But think about it at least.
Title: Re: Suggestion: Implicit Int to Float conversion
Post by: Gilbert on Sat 15/04/2006 14:54:20
It doesn't make much sense since we have to introduce some convention on it.

For example, if you make 4 translated to 4.0 directly, there'll be ambiguity for the followings:
moveSomething(oThing, 4.0/3.0); //second parameter should be 1.333...
okaymoveSomething(oThing, 4/3); //what should the second parameter be? 1 or 1.333... ?

I know many languages support explicit translation of the types, but it makes sense only to experienced programmers who are familiar with how the values are to be treated in different cases. For AGS the story is a bit difference, the text script is just a scripting language not a full programming environment, I think currently it can only distinguish between integer operations and decimal operations by how the variables look like.
So:

float aa = 4/3;

should give an error as 4 and 3 are integers makes it understandable the operation is an integer division, thus yielding an integer quotient, which will cause a type-mismatch error when assigned to the float variable aa, this would help not-so-experienced users to check their scripting problems and reduce bugs in games.
Title: Re: Implicit Int to Float conversion
Post by: Kweepa on Sun 16/04/2006 04:07:00
Quote from: fovmester on Sat 15/04/2006 07:02:08
moveSomething(oThing, 4);
Again, why not just type this?

moveSomething(oThing, 4.0);
Title: Re: Suggestion: Implicit Int to Float conversion
Post by: fovmester on Mon 17/04/2006 07:36:26
Of course you should, but who remembers it?

Quote from: Gilbot V7000a on Sat 15/04/2006 14:54:20
I know many languages support explicit translation of the types, but it makes sense only to experienced programmers who are familiar with how the values are to be treated in different cases. For AGS the story is a bit difference, the text script is just a scripting language not a full programming environment, I think currently it can only distinguish between integer operations and decimal operations by how the variables look like.

I can buy that. I rest my case.