[NXC] floor function not working!!!!
Posted: 30 May 2011, 15:27
Hello,
As I was creating game I needed a float to round to the floor.
I made a test code:
If you run the program, you will notice that the number above the bottom number will add by 0.2 every 600 milliseconds. Also the bottom number will show the floor of the top number. It works on some numbers but sometimes it thinks that the floor of, lets say, 2 is 1, which is incorrect, but when 2 turns to 2.2 then it says that the floor of 2.2 is 2, which is correct.
Can someone help me?
Is there a round function(nearest 100th,10th,etc)?
If not, can someone show me how?
I will just show the entire game code:
If you run it you will encounter some problems because the floating-point values aren't exactly correct. Help?
Thanks,
nxtboy III
As I was creating game I needed a float to round to the floor.
I made a test code:
Code: Select all
float x;
float y;
float ix;
float iy;
task main()
{
while(1)
{
x+=0.2;
ix=floor(x);
NumOut(0,0,ix);
NumOut(0,8,x);
Wait(600);
}
}
Can someone help me?
Is there a round function(nearest 100th,10th,etc)?
If not, can someone show me how?
I will just show the entire game code:
Code: Select all
int map[10][6];
float fmapx,fmapy;
int mapx,mapy;
bool gl;
int x,y;
int padd;
#define EPSILON 0.125 // Make this "as small as possible", and make sure it's a power of 2.
// Try 0.015625, but that may fail with larger numbers.
inline long myfloor(float x)
{
return(x + EPSILON);
}
task dostuff()
{
while(1)
{
RectOut(x,y,9,9,DRAW_OPT_FILL_SHAPE | DRAW_OPT_CLEAR);
RectOut(0,40,100,8,DRAW_OPT_FILL_SHAPE | DRAW_OPT_CLEAR);
if(SENSOR_1)
{
StopAllTasks();
}
if(fmapy == myfloor(fmapy))
{
padd=0;
}
else
{
padd=1;
}
if(ButtonPressed(BTNLEFT,0) && ((map[mapx-1][mapy] == 0 && map[mapx-1][mapy+padd] == 0) || fmapx > myfloor(fmapx)))
{
// until(!ButtonPressed(BTNLEFT,0));
// PlayToneEx(500,30,1,0);
fmapx-=0.2;
x-=2;
gl=1;
}
// mapx=(fmapx-frac(fmapx));
/* if(fmapx == myfloor(fmapx))
{
gl=0;
}*/
if(ButtonPressed(BTNRIGHT,0) && map[mapx+1][mapy] == 0 && map[mapx+1][mapy+padd] == 0)
{
fmapx+=0.2;
x+=2;
}
if(fmapx == myfloor(fmapx))
{
PlayToneEx(700,10,1,0);
padd=0;
}
else
{
padd=1;
}
if(ButtonPressed(BTNCENTER,0) && map[mapx][mapy+1] == 0 && map[mapx+padd][mapy+1] == 0)
{
fmapy+=0.2;
y+=2;
}
if(ButtonPressed(BTNEXIT,0) && ((map[mapx][mapy-1] == 0 && map[mapx+padd][mapy-1] == 0) || fmapy > myfloor(fmapy)))
{
fmapy-=0.2;
y-=2;
}
mapy=myfloor(fmapy);
mapx=myfloor(fmapx);
RectOut(x,y,9,9,DRAW_OPT_FILL_SHAPE);
NumOut(0,40,mapx);
NumOut(15,40,fmapx);
//NumOut(40,40,(frac(fmapx)));
NumOut(70,40,myfloor(5));
Wait(40);
}
}
task draw()
{
while(1)
{
for(int c=0; c < 10; c++)
{
for(int d=0; d < 6; d++)
{
if(map[c][d] == 1)
{
RectOut(c*10,d*10,9,9);
}
}
}
}
}
task main()
{
SetAbortFlag(BTNSTATE_NONE);
SetSensorTouch(S1);
mapx=1;
mapy=1;
fmapx=1.0;
fmapy=1.0;
x=10;
y=10;
for(int c=0; c < 10; c++)
{
map[c][0]=1;
}
for(int c=0; c < 6; c++)
{
map[0][c]=1;
}
for(int c=0; c < 10; c++)
{
map[c][5]=1;
}
for(int c=0; c < 6; c++)
{
map[9][c]=1;
}
map[4][3]=1;
Precedes(draw,dostuff);
}
Thanks,
nxtboy III