NXC [BUG]: type casting int -> float not as expected

Discussion specific to NXT-G, NXC, NBC, RobotC, Lejos, and more.
Post Reply
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

NXC [BUG]: type casting int -> float not as expected

Post 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?)
spillerrec
Posts: 358
Joined: 01 Oct 2010, 06:37
Location: Denmark
Contact:

Re: NXC [BUG]: type casting int -> float not as expected

Post 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.
My blog: http://spillerrec.dk/category/lego/
RICcreator, an alternative to nxtRICeditV2: http://riccreator.sourceforge.net/
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: NXC [BUG]: type casting int -> float not as expected

Post 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")
afanofosc
Site Admin
Posts: 1256
Joined: 26 Sep 2010, 19:36
Location: Nashville, TN
Contact:

Re: NXC [BUG]: type casting int -> float not as expected

Post 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
Multi-platform LEGO MINDSTORMS programming
http://bricxcc.sourceforge.net/
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: NXC [BUG]: type casting int -> float not as expected

Post 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
afanofosc
Site Admin
Posts: 1256
Joined: 26 Sep 2010, 19:36
Location: Nashville, TN
Contact:

Re: NXC [BUG]: type casting int -> float not as expected

Post 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
Multi-platform LEGO MINDSTORMS programming
http://bricxcc.sourceforge.net/
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: NXC [BUG]: type casting int -> float not as expected

Post 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)...^^
muntoo
Posts: 834
Joined: 01 Oct 2010, 02:54
Location: Your Worst Nightmare
Contact:

Re: NXC [BUG]: type casting int -> float not as expected

Post 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.
Image

Commit to LEGO Mindstorms Robotics Stack Exchange:
bit.ly/MindstormsSE


Commit to LEGO Stack Exchange: bit.ly/Area51LEGOcommit
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: NXC [BUG]: type casting int -> float not as expected

Post by HaWe »

muntoo,
what do you mean by that? can you pls explain?
muntoo
Posts: 834
Joined: 01 Oct 2010, 02:54
Location: Your Worst Nightmare
Contact:

Re: NXC [BUG]: type casting int -> float not as expected

Post 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 */ );
Image

Commit to LEGO Mindstorms Robotics Stack Exchange:
bit.ly/MindstormsSE


Commit to LEGO Stack Exchange: bit.ly/Area51LEGOcommit
Post Reply

Who is online

Users browsing this forum: Semrush [Bot] and 10 guests