Jump to content
IGNORED

A Table of Fractions?


jrok

Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 by jrok
Link to comment
Share on other sites

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

Link to comment
Share on other sites

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? :lol: 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 by jrok
Link to comment
Share on other sites

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 by SeaGtGruff
Link to comment
Share on other sites

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.)

Link to comment
Share on other sites

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...