Data types
Blitz3D Forums/Blitz3D Programming/Data types
| ||
This is a strange one... I have 6 variables xoff# & yoff# - both at 0.0 (Ive checked) mapx & mapy (integers) both set at 10 totx & toty for holding totals. Why does the following code yield different output depending whether or not the INT function is used? If you use the code as follows, the squares are correctly identified by their x & y coordinates. If you comment out the lines containing the mx&my calculations and uncomment the lines above it...you can see that the coordinate display is 16 pixels out of alignment!! Am I going crazy? Graphics3D 640,480,32,2 mapx=10:mapy=10 xoff#=0 yoff#=0 While Not KeyDown(1) ;mx=(MouseX()+xoff)/32 ;my=(MouseY()+yoff)/32 mx=(MouseX()+Int(xoff))/32 my=(MouseY()+Int(yoff))/32 totx=mapx+mx toty=mapy+my Cls Color 0,0,100 For y=0 To 14 For x=0 To 20 Rect x*32,y*32,32,32,0 Next Next Color 255,255,255 Text 10,10,"Mapxy : "+mapx+" , "+mapy Text 10,30,"Offsetxy : "+xoff+" , "+yoff Text 10,50,"Totxy : "+totx+", "+toty Flip Wend |
| ||
Without using the "int" keyword the value mousex() (integer) + xoff (float) gets converted to a float for the rest of the calculation involving the /32. When you use the int keyword it guarantees that all the calculations are done using integer calculations. I don't think this is a bug. |
| ||
In the case where all integers are used you are doing integer division. The result is "chopped", i.e. any remainder is simply discarded. Since we are dealing with positive numbers this means rounding down. In the other case you are mixing integers and floating point numbers. This means that floating point arithmetic is used. The result is assigned to an integer variable. Unlike just about every other language Blitz converts to integer by rounding, which can up or down. Here's an example. 10/32 rounds down in both cases while 20/32 can go up or down. aInt = ( 10 + 0 ) / 32 aFloat = ( 10 + 0.0 ) / 32 bInt = ( 20 + 0 ) / 32 bFloat = (20 + 0.0 ) / 32 Print Print " aInt = " + aInt Print "aFloat = " + aFloat Print " bInt = " + bInt Print "bFloat = " + bFloat WaitKey |
| ||
Oh duh! Like I said...very strange. (or it is to me anyways) But now I know better. Apologies for posting in the wrong forum :) |