Author Topic: int()  (Read 3313 times)

Peter

  • Guest
int()
« on: November 15, 2012, 02:38:35 PM »
Hi Cyber,

There is a bug with INTstatement. There is a pixel difference of 16 pixeln!
Try the small demo in order to see what I  mean.

If you click on the red wall, you will put another wall on it.
This happens with a difference of 16 pixel!
Look also at  the (x/y position) and (mousex/mousey position). Move the mouse slow.
Code: [Select]
function Sprite(id,x,y,w,h,fx)
   drawimage(w*fx,0,w,h,x,y,id)
end

function SetText(x,y,txt,r,g,b)
   color(r,g,b)
   drawtext(x,y,txt)
end

screen(320,240,0,"Ohh")
setframetimer(60)
backcolor(255,255,255)

x,y,z,b,s = 0,0,0,0,0
b=loadimage("png/brick1.png")
s=loadimage("png/xsteel.png")

repeat
key=getkey()
cls()
x= int(mousex()/32)
y= int(mousey()/32)
for z=0,10 do
   Sprite(b,z*32,32,32,32,0)
end
if mouseb()==1 and y==1 then
   Sprite(s,x*32,y*32,32,32,1)
end
SetText(0, 0,"x= "..x.."  mousex "..mousex(),0,0,0)
SetText(0,12,"y= "..y.."  mousey "..mousey(),0,0,0)
sync()
until key==27
closewindow()

Cybermonkey

  • Administrator
  • *****
  • Posts: 0
Re: int()
« Reply #1 on: November 15, 2012, 03:20:18 PM »
Hm, seems you're right. Since I am using the round function of Pascal, there seems to be the fault.
Anyway - as a workaround - you can use the lua built-in functions:
Code: [Select]
x= math.floor(mousex()/32)
y= math.floor(mousey()/32)
Then it seems to work correctly.

Or you change the calculation, because int() rounds mathematically correct which means 2.6 = 3 and 2.4 = 2 etc. I could change it to trunc() which means 2.1 ... 2.9 = 2 but is this correct? How does other int() functions work in other BASICs?
See this code for clarification:
Code: [Select]
screen (800,600,0,"Integer-Test")
drawtext (0,0,int (2.6))
drawtext (0,10,math.floor (2.6))
drawtext (0,20,math.ceil (2.6))

drawtext (0,40,int (2.4))
drawtext (0,50,math.floor (2.4))
drawtext (0,60,math.ceil (2.4))

drawtext (0,80,int (2.5))
drawtext (0,90,math.floor (2.5))
drawtext (0,100,math.ceil (2.5))

sync()
inkey()
closewindow()
Result:
Quote
3
2
3

2
2
3

2
2
3
For an example you can also have a look at the multix.lua in the examples download.
« Last Edit: November 15, 2012, 03:27:46 PM by Cybermonkey »

Tomaaz

  • Guest
Re: int()
« Reply #2 on: November 15, 2012, 03:33:31 PM »
I think mostly int(x) return the largest integer smaller or equal to x (like math.floor() in Lua), while round(x) returns the nearest integer.

Examples:

Code: [Select]
int(5.2) => 5
round(5.2) => 5

int(5.8) => 5
round(5.8) => 6

int(-5.2) => -6
round(-5.2) => -5

round(x) wouldn't be the same as math.ceil() in Lua, as depends on situation, it can return both smaller or larger integer.
« Last Edit: November 15, 2012, 03:46:00 PM by Tomaaz »

Tomaaz

  • Guest
Re: int()
« Reply #3 on: November 15, 2012, 03:59:35 PM »
But it is possible to make int() return an integer part of the number,
Code: [Select]
int(5.2) => 5
int(-5.2) => -5 (not -6)

so there would be four slightly different functions: int(), floor(), ceil() and round(). Not a bad idea IMHO.
« Last Edit: November 15, 2012, 04:02:43 PM by Tomaaz »

Peter

  • Guest
Re: int()
« Reply #4 on: November 15, 2012, 06:52:35 PM »
math.floor is good,  in my case here.
You can try  a  game,  section examples.