Implicit Int to Float conversion (SOLVED)

Started by fovmester, Wed 12/04/2006 14:38:55

Previous topic - Next topic

fovmester

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!

Pumaman

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.

fovmester

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.

Gilbert

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.

Kweepa

Quote from: fovmester on Sat 15/04/2006 07:02:08
moveSomething(oThing, 4);
Again, why not just type this?
Code: ags

moveSomething(oThing, 4.0);
Still waiting for Purity of the Surf II

fovmester

#5
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.

SMF spam blocked by CleanTalk