Page 1 of 1
NXC [BUG]: type casting int -> float not as expected
Posted: 13 Jun 2011, 08:19
by HaWe
hi,
... although I know about workarounds .... ,
calculating the following
Code: Select all
float V=BatteryLevel()/1000;
printf("Volt=%6.3f",V);
I always get results like
7.000 (BatteryLevel()=7312)
8.000 (BatteryLevel()=8294)
why don't I get values like 7.312 or 8.294 ?
As we got float as the result type I had expected that it should work automatically correctly casted to float.
what's faulty with the implicite type casting?
(and could we probably have explicite type casting soon?)
Re: NXC [BUG]: type casting int -> float not as expected
Posted: 13 Jun 2011, 10:27
by spillerrec
If BatteryLevel() returns an integer it will still do integer division even though you try to store it in a float. That is how it is supposed to work in C/C++. So use the workarounds until we have proper casting.
Re: NXC [BUG]: type casting int -> float not as expected
Posted: 13 Jun 2011, 11:21
by HaWe
As this is meant to be a bug report, using a workaround is not the issue (and finally we don't have explicite typecasting like in C/C++).
If I remember correctly, John wrote somewhere sometimes that NXC had implicite typecasting, so it should do implicitely a fp calculation depending on the target type... (that's at least what I expected from "implicite")
Re: NXC [BUG]: type casting int -> float not as expected
Posted: 13 Jun 2011, 15:38
by afanofosc
If either BatteryLevel() or 1000 were a float then you would get a float as the result. Since neither is a float then you get an integer result. Just add .0 to your 1000 so that is is a floating point type or multiply BatteryLevel() by 0.001 instead of dividing it by 1000.
This is working exactly as it should and is not a bug in NXC.
John Hansen
Re: NXC [BUG]: type casting int -> float not as expected
Posted: 13 Jun 2011, 15:51
by HaWe
Just add .0 to your 1000 so that is is a floating point type
That's exactly my workaround, or with variables I set
float fvar=int ivar; fvar/=1000
But the workarounds are not the point, I really thought implicite type casting means: it calculates depending on the target type implicitely automatically, just if I had written explicitely
V=(float)BatteryLevel()/1000
Re: NXC [BUG]: type casting int -> float not as expected
Posted: 13 Jun 2011, 16:01
by afanofosc
That would make NXC very not like C so it does not do that. NXC simply lets you cast things implicitly where some other languages require an explicit cast.
John Hansen
Re: NXC [BUG]: type casting int -> float not as expected
Posted: 13 Jun 2011, 16:13
by HaWe
That would make NXC very not like C
for heaven's sake, I would never wish THAT!
Then better have an additional (float)...^^
Re: NXC [BUG]: type casting int -> float not as expected
Posted: 13 Jun 2011, 22:52
by muntoo
Consider this:
Code: Select all
float x = (1234 / 1000);
// float x = (float) (1234 / 1000)
// 1234 / 1000 == 1
// float x = (float) (1);
// float x = 1.0;
The "implicit cast" is to the final result.
Re: NXC [BUG]: type casting int -> float not as expected
Posted: 14 Jun 2011, 07:59
by HaWe
muntoo,
what do you mean by that? can you pls explain?
Re: NXC [BUG]: type casting int -> float not as expected
Posted: 14 Jun 2011, 15:54
by muntoo
doc-helmut wrote:muntoo,
what do you mean by that? can you pls explain?
I mean that the expression
(1234 / 1000)
is always evaluated first.
Then, it is assigned to the
float
. Since the value of that expression is
1
,
x
is equal to
1.0
.
The implicit cast is always around the whole expression:
Code: Select all
float x = float( /* expression */ );