## Sin table and fixed point arithmetic in C

**Moderator:** Moderators

### Sin table and fixed point arithmetic in C

Hi!

I've been trying to understand how to implement fixed point arithmetic in C, but I can't wrap my head around it.

Are there any specific C tutorials around?

I'd like to start experimenting with a sine table (an analog clock demo, for example) but I'm kinda lost without decimals.

One possible approach would be working with values 100x larger and divide them to obtain the x,y pixel values.

Of course, for performance reasons, instead of 100x, I would actually use a power of 2.

I guess that would work, but I would still like to learn how to do fixed-point arithmetic in C on the NES.

As always, many thanks in advance!

I've been trying to understand how to implement fixed point arithmetic in C, but I can't wrap my head around it.

Are there any specific C tutorials around?

I'd like to start experimenting with a sine table (an analog clock demo, for example) but I'm kinda lost without decimals.

One possible approach would be working with values 100x larger and divide them to obtain the x,y pixel values.

Of course, for performance reasons, instead of 100x, I would actually use a power of 2.

I guess that would work, but I would still like to learn how to do fixed-point arithmetic in C on the NES.

As always, many thanks in advance!

### Re: Sin table and fixed point arithmetic in C

Using values 256 times larger is indeed how fixed point is usually done. While someone else digs up tutorials, you could read through "Fixed-point arithmetic" on Wikipedia.

### Re: Sin table and fixed point arithmetic in C

I guess you want to convert an angle into X and Y coordinates with a fixed length.

You could actually make your own table of answers, and just use it as a look up table. That would be the typical 1980s way to do it.

But, if you want to know how to do it with fixed point and multiplication, you would make your own tables by multiplying 256 by the sine value of each angle. sine and cosine answers, from 0 to 256, where 256 represents 1.00.

So length radius is Z. Angle is A. All signed int16, fixed point 8.8. Shift Z right 8 to make it 8 bit. Then multiply THAT by the 0-256 sine table to get a 16 bit answer...(fixed point 8.8) the upper 8 bits being the answer you want...so shift right 8 again to get an 8 bit final answer for X and Y.

X = ((Z >> 8) * cos_table[A] ) >> 8;

Y = ((Z >> 8) * sin_table[A] ) >> 8;

I think that's right. I didn't test it.

You could actually make your own table of answers, and just use it as a look up table. That would be the typical 1980s way to do it.

But, if you want to know how to do it with fixed point and multiplication, you would make your own tables by multiplying 256 by the sine value of each angle. sine and cosine answers, from 0 to 256, where 256 represents 1.00.

So length radius is Z. Angle is A. All signed int16, fixed point 8.8. Shift Z right 8 to make it 8 bit. Then multiply THAT by the 0-256 sine table to get a 16 bit answer...(fixed point 8.8) the upper 8 bits being the answer you want...so shift right 8 again to get an 8 bit final answer for X and Y.

X = ((Z >> 8) * cos_table[A] ) >> 8;

Y = ((Z >> 8) * sin_table[A] ) >> 8;

I think that's right. I didn't test it.

nesdoug.com -- blog/tutorial on programming for the NES

### Re: Sin table and fixed point arithmetic in C

I explained fixed math arithmetic in a simple way with a practical example once here. Maybe it clicks after reading different people's explanations. This is for binary fixed point though, something which is very useful for game programming even today.

### Re: Sin table and fixed point arithmetic in C

8.8 fixed point in C is as simple as using 16-bit values for the calculations, and when you're ready to use the result, right shift it 8 times. cc65 in that case is smart enough to simply do LDA highbyte instead of doing any actual bit shifting.

### Re: Sin table and fixed point arithmetic in C

Hi guys!

Thank you for the fast replies!

@Memblers, @dougeff

So let me see if I'm on the right track:
Assuming the code is correct, what goes on the

Thank you for the fast replies!

@Memblers, @dougeff

So let me see if I'm on the right track:

Code: Select all

```
static int velocity_x;
void update() {
// if it was a float: velocity_x += 0.1f;
velocity_x += ???
position_x += velocity_x >> 8;
}
```

**???**mark?### Re: Sin table and fixed point arithmetic in C

@wonder , just do a rule of three:

Code: Select all

```
+------------+-------------+
| real value | fixed point |
+------------+-------------+
| 1.0 | 256 |
| 0.5 | 128 |
| 0.1 | 26 |
+------------+-------------+
```

### Re: Sin table and fixed point arithmetic in C

Thanks @NOOPr!!NOOPr wrote:@wonder , just do a rule of three:Code: Select all

`+------------+-------------+ | real value | fixed point | +------------+-------------+ | 1.0 | 256 | | 0.5 | 128 | | 0.1 | 26 | +------------+-------------+`

Wow, the solution was so simple I almost feel ashamed of asked such a question in the first place.

I think I understand the concept pretty well now.

Now I wonder if there would be a pre-processor macro that would to that at compile time... probably not.

Code: Select all

`REAL2FIXED(0.5); // outputs 128`

### Re: Sin table and fixed point arithmetic in C

Acceleration speed is a trial and error thing that needs to be play tested.

nesdoug.com -- blog/tutorial on programming for the NES