jrok Posted July 12, 2009 Share Posted July 12, 2009 I raised this question over at a homebrew thread, but I suppose it is more appropriate for this forum. Has anyone devised a method to store fractional values inside a batariBasic data statement? The end result I'm thinking of would express something along the lines of: data fractable 0.0625, 0.125, 0.5, 1.0, 1.5, 0.5, 1.5, 0.75 end Along the same lines, is there any way in which to define a fractional constant? const fraction = 2.75 Thanks, Jarod. Quote Link to comment Share on other sites More sharing options...
GroovyBee Posted July 12, 2009 Share Posted July 12, 2009 Google for fixed point arithmetic. Quote Link to comment Share on other sites More sharing options...
+batari Posted July 12, 2009 Share Posted July 12, 2009 I raised this question over at a homebrew thread, but I suppose it is more appropriate for this forum. Has anyone devised a method to store fractional values inside a batariBasic data statement? The end result I'm thinking of would express something along the lines of: data fractable 0.0625, 0.125, 0.5, 1.0, 1.5, 0.5, 1.5, 0.75 end Along the same lines, is there any way in which to define a fractional constant? const fraction = 2.75 Thanks, Jarod. If you are talking about the fractional part of an 8.8 fixed point type, you could build a table like this: data fractable 0.0625*256, 0.125*256, 0.5*256, 1.0*256, 1.5*256, 0.5*256, 1.5*256, 0.75*256 end To use them, assign them to the second variable of a 8.8 type. For example, if you dim my88=a.b, then assign the above to b. A fractional constant should be done using def. When using def, you should use a more unique identifier to avoid issues, as any instance of the identifier would be replaced with 2.75. For example: def fraction_1 = 2.75 Then you could assign it using my88=fraction, for example. Quote Link to comment Share on other sites More sharing options...
jrok Posted July 12, 2009 Author Share Posted July 12, 2009 (edited) If you are talking about the fractional part of an 8.8 fixed point type, you could build a table like this: data fractable 0.0625*256, 0.125*256, 0.5*256, 1.0*256, 1.5*256, 0.5*256, 1.5*256, 0.75*256 end To use them, assign them to the second variable of a 8.8 type. For example, if you dim my88=a.b, then assign the above to b. A fractional constant should be done using def. When using def, you should use a more unique identifier to avoid issues, as any instance of the identifier would be replaced with 2.75. For example: def fraction_1 = 2.75 Then you could assign it using my88=fraction, for example. Thanks! I think I do understand. So, if I wanted to access a table of the following fixed point values to gradually increase the vertical speed of a moving missile object: data speedtable 0.625, 0.125, 0.25, 0.5, 1 end ...I could write something along the lines of: dim speed = a.b dim speedpointer = c data speedtable 16, 32, 64, 128, 256 end loop speed = speedtable[c] if speedpointer<4 then speedpointer = speedpointer + 1 missile0y = missile0y+b goto loop Not that I'd necessarily want to do it that way But would that be the general idea? Edited July 12, 2009 by jrok Quote Link to comment Share on other sites More sharing options...
SeaGtGruff Posted July 12, 2009 Share Posted July 12, 2009 Thanks! I think I do understand. So, if I wanted to access a table of the following fixed point values to gradually increase the vertical speed of a moving missile object: data speedtable 0.625, 0.125, 0.25, 0.5, 1 end ...I could write something along the lines of: dim speed = a.b dim speedpointer = c data speedtable 16, 32, 64, 128, 256 end loop speed = speedtable[c] if speedpointer<4 then speedpointer = speedpointer + 1 missile0y = missile0y+b goto loop You've got a few mistakes. In your original numbers, the first value should be 0.0625. In your speedtable array, the last value should be 0. In your loop, you would say b=speedtable[c], because you're setting the fractional part. Then you'd also need to say something like if speedpointer<4 then a=0 else a=1. Michael Quote Link to comment Share on other sites More sharing options...
jrok Posted July 13, 2009 Author Share Posted July 13, 2009 (edited) Thanks! I think I do understand. So, if I wanted to access a table of the following fixed point values to gradually increase the vertical speed of a moving missile object: data speedtable 0.625, 0.125, 0.25, 0.5, 1 end ...I could write something along the lines of: dim speed = a.b dim speedpointer = c data speedtable 16, 32, 64, 128, 256 end loop speed = speedtable[c] if speedpointer<4 then speedpointer = speedpointer + 1 missile0y = missile0y+b goto loop You've got a few mistakes. In your original numbers, the first value should be 0.0625. In your speedtable array, the last value should be 0. In your loop, you would say b=speedtable[c], because you're setting the fractional part. Then you'd also need to say something like if speedpointer<4 then a=0 else a=1. Michael Yeah, what the heck is 256? I was pretty sleepy when I typed that, sorry. So I believe I understand the concept, but for some reason the method is eluding me. I tried the following test program: include div_mul.asm include fixed_point_math.asm dim yspeed = a.b dim speedpointer = c dim ypos = d.e dim dir = f data speedtable 16, 16, 16, 16, 32, 32, 32, 32, 64, 64, 64, 64, 128, 128, 128, 128, 0, 0, 0, 0 end player0x = 76 ypos=40.0 player0: %11111111 %11111111 %11111111 end loop COLUP0 = $0E speedpointer = speedpointer + 1 if speedpointer > 19 && dir=0 then dir = 1 : speedpointer = 0 if speedpointer > 19 && dir=1 then dir = 0 : speedpointer = 0 if speedpointer<16 then a = 0 else a = 1 yspeed = speedtable[speedpointer] if dir=0 then ypos = ypos+b if dir=1 then ypos = ypos-b player0y = ypos drawscreen goto loop I assumed the above code would set P0 at Y pos 40, gradually increase it's rate of speed in one direction then gradually increase its speed in the opposite direction. Am I missing something? fixedpointtable.bas fixedpointtable.bas.bin Edited July 13, 2009 by jrok Quote Link to comment Share on other sites More sharing options...
SeaGtGruff Posted July 13, 2009 Share Posted July 13, 2009 (edited) I assumed the above code would set P0 at Y pos 40, gradually increase it's rate of speed in one direction then gradually increase its speed in the opposite direction. Am I missing something? I haven't tried your program yet, but you're still trying to set the whole part where you should be setting the fractional part: dim yspeed = a.b if speedpointer<16 then a = 0 else a = 1 yspeed = speedtable[speedpointer] That third line should be "b = speedtable[speedpointer]" because speedtable contains the *fractional* part of the speed, and b is the fractional part of yspeed. Michael Edited July 13, 2009 by SeaGtGruff Quote Link to comment Share on other sites More sharing options...
+batari Posted July 13, 2009 Share Posted July 13, 2009 You have the basic idea I was trying to get across with using the values*256 in your table. However, I meant the following example literally: data fractable 0.0625*256, 0.125*256, 0.5*256, 1.0*256, 1.5*256, 0.5*256, 1.5*256, 0.75*256 end That is, in a data table, you can specify expressions containing constant values and they will be calculated for you during assembly and turned into single bytes in the binary (provided the calculated values do not exceed 255.) Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.