[NXC] convert float to int
-
- Posts: 117
- Joined: 27 Dec 2010, 19:27
[NXC] convert float to int
Is it possible to convert a float number to an integer (rounding the number to floor or ceil), becoming an integer after the operation?
From what I searched and tried, using floor() or ceil() just rounds the number, but the returning value is still a float. I'm trying to pass an integer argument to a function, that had to be calculated as a float before.
The specific example is: I have the expression "angle = 112.5*X". After calculating the angle I want to send the closest integer to PosRegSetAngle(). If I just use floor() or ceil() it doesn't work.
From what I searched and tried, using floor() or ceil() just rounds the number, but the returning value is still a float. I'm trying to pass an integer argument to a function, that had to be calculated as a float before.
The specific example is: I have the expression "angle = 112.5*X". After calculating the angle I want to send the closest integer to PosRegSetAngle(). If I just use floor() or ceil() it doesn't work.
Re: [NXC] convert float to int
This should do the conversion:
C-Style casts should go on the wishlist...
-----
You don't need to explicitly (or "explicitly-implicitly", in this case ) convert
This works fine:
Code: Select all
#define f2i(x) float2int(x)
int float2int(float x)
{
return(x);
}
Code: Select all
if(sin(f2i(1.5)) == sin(1.5))
{
TextOut(0, 0, "You should not see this text.", 0);
}
int(x)
-----
You don't need to explicitly (or "explicitly-implicitly", in this case ) convert
float
to int
when passing arguments of a different type (in NXC).This works fine:
Code: Select all
int add(int a, int b)
{
return(a + b);
}
NumOut(0, 0, add(19.84, 23.999)); // Prints 42
Commit to LEGO Mindstorms Robotics Stack Exchange:
bit.ly/MindstormsSE
Commit to LEGO Stack Exchange: bit.ly/Area51LEGOcommit
-
- Posts: 117
- Joined: 27 Dec 2010, 19:27
Re: [NXC] convert float to int
Hm, I just noticed the following behaviours:
I hope that can be useful for some debugging.
Code: Select all
float a = 2.5;
int b = 1;
long c = a*b; // c is equal to 2
Code: Select all
float a = -2.5;
int b = 1;
long c = a*b; // c is equal to 0
Code: Select all
float a = 2.5;
int b = -1;
long c = a*b; // c is equal to 0
Code: Select all
long c = -2.5; // c is equal to -2
Code: Select all
int a = 2.5;
int b = -1;
long c = a*b; // c is equal to -2
Code: Select all
float a = 2.5;
float b = -1;
long c = a*b; // c is equal to 0
Code: Select all
float a = -2.5;
float b = -1;
long c = a*b; // c is equal to 2
Re: [NXC] convert float to int
Code: Select all
long c = -2.5; // c is equal to -2
c = -3;
, though.)Code: Select all
float a = 2.5;
int b = -1;
long c = a*b; // c is equal to 0
I think that may solve some bugs in one of my programs...
Commit to LEGO Mindstorms Robotics Stack Exchange:
bit.ly/MindstormsSE
Commit to LEGO Stack Exchange: bit.ly/Area51LEGOcommit
Re: [NXC] convert float to int
the mathematically correct way to round a fp value to an integer is this one:
or, nested:
HTH!
Code: Select all
inline long round(float f)
{
if (f>=0) return (f + 0.5);
else return (f - 0.5);
}
Code: Select all
inline long round(float f)
{
return (f>=0? f+0.5 : f-0.5);
}
Re: [NXC] convert float to int
Technically, the
round()
method is usually implemented as such:
Code: Select all
inline long round(float x)
{
return(x + 0.5);
}
Commit to LEGO Mindstorms Robotics Stack Exchange:
bit.ly/MindstormsSE
Commit to LEGO Stack Exchange: bit.ly/Area51LEGOcommit
-
- Posts: 117
- Joined: 27 Dec 2010, 19:27
Re: [NXC] convert float to int
I'm not that worried about the rounding. The incorrect cast is quite a big issue, I would say.
I worked around the problem in my program right now, but I will certainly get stuck with this later again, trying to understand what is wrong. I hope John had the chance to look at this topic.
I worked around the problem in my program right now, but I will certainly get stuck with this later again, trying to understand what is wrong. I hope John had the chance to look at this topic.
Re: [NXC] convert float to int
ok, but:muntoo wrote:Technically, theround()
method is usually implemented as such:Code: Select all
inline long round(float x) { return(x + 0.5); }
1.) then what's not working for the TO's float conversion?
2.) what's about negative fp values?
Re: [NXC] convert float to int
What do you mean by "it doesn't work"? Do you get a compiler error or the wrong value? Can you post a simple example that shows what is going wrong?ricardocrl wrote: The specific example is: I have the expression "angle = 112.5*X". After calculating the angle I want to send the closest integer to PosRegSetAngle(). If I just use floor() or ceil() it doesn't work.
John Hansen
Multi-platform LEGO MINDSTORMS programming
http://bricxcc.sourceforge.net/
http://bricxcc.sourceforge.net/
Re: [NXC] convert float to int
Could you please post the F12 code listing for these snippets. Is it possible that you have an old compiler version? Here's what the latest compiler produces:ricardocrl wrote: Hm, I just noticed the following behaviours:
I hope that can be useful for some debugging.
Code: Select all
task main()
{
{
float a = 2.5;
int b = 1;
long c = a*b; // c is equal to 2
// mov __main_7qG2_a_7qG2_001, __constValf2P5
// mov __main_7qG2_b_7qG2_001, __constVal1
// mul __main_7qG2_c_7qG2_001, __main_7qG2_a_7qG2_001, __constVal1
}
{
float a = -2.5;
int b = 1;
long c = a*b; // c is equal to 0
// mov __main_7qG2_a_7qG2_001, __constValNegf2P5
// mov __main_7qG2_b_7qG2_001, __constVal1
// mul __main_7qG2_c_7qG2_001, __main_7qG2_a_7qG2_001, __constVal1
}
{
float a = 2.5;
int b = -1;
long c = a*b; // c is equal to 0
// mov __main_7qG2_a_7qG2_001, __constValf2P5
// mov __main_7qG2_b_7qG2_001, __constValNeg1
// mul __main_7qG2_c_7qG2_001, __main_7qG2_a_7qG2_001, __constValNeg1
}
{
long c = -2.5; // c is equal to -2
// mov __main_7qG2_c_7qG2_001, __constValNegf2P5
}
{
int ai = 2.5;
int b = -1;
long c = ai*b; // c is equal to -2
// mov __main_7qG2_ai_7qG2_001, __constValf2P5
// mov __main_7qG2_b_7qG2_001, __constValNeg1
// mul __main_7qG2_c_7qG2_001, __main_7qG2_ai_7qG2_001, __constValNeg1
}
{
float a = 2.5;
float bf = -1;
long c = a*bf; // c is equal to 0
// mov __main_7qG2_a_7qG2_001, __constValf2P5
// mov __main_7qG2_bf_7qG2_001, __constValNeg1
// mul __main_7qG2_c_7qG2_001, __main_7qG2_a_7qG2_001, __constValNeg1
}
{
float a = -2.5;
float bf = -1;
long c = a*bf; // c is equal to 2
// mov __main_7qG2_a_7qG2_001, __constValNegf2P5
// mov __main_7qG2_bf_7qG2_001, __constValNeg1
// mul __main_7qG2_c_7qG2_001, __main_7qG2_a_7qG2_001, __constValNeg1
}
}
John Hansen
Multi-platform LEGO MINDSTORMS programming
http://bricxcc.sourceforge.net/
http://bricxcc.sourceforge.net/
Who is online
Users browsing this forum: No registered users and 2 guests