IGNORED

# Fixed point arithmetics

## Recommended Posts

Does anybody know of a library of fixed point arithmetic for, say, Action, CC65 or Assembler?

The library should have the most important functions like sin, log etc.

##### Share on other sites

have a look at codebase64.org

##### Share on other sites

I couldn't find anything on codebase64.org or in MADS. All they have are floating point libraries, but no fixed point libraries.

##### Share on other sites

In here are many small parts of what you want:

I'm afraid there's no "all in one" library.

Functions like sin, log etc. are usually done with tables.

Working with fixed point math is like working with integers. You just take the part of result you want.

Plenty of integer routines on the codebase64...

##### Share on other sites

Yes, I know them. I just had hoped somebody had a library handy. Well, back to the drawing board

##### Share on other sites

From what I recall of Elite, your X, Y and Z position in space uses a fixed point representation of 3 bytes each. This is little more magic than using the low byte as the fractional part (0/256 -> 255/256) and the middle byte as the low integer (0 -> 255) and the high byte as high integer part (256 × (0 -> 255)).

Operations work similar to usual integer math but after a multiply you take the result from the 2nd byte upwards, effectively dividing your result by 256.

The top three rows show the breakdown of 2 values to be multiplied and the result.

On the right are the same bytes represented as an 24-bit integer.

The lower line then splits the result from multiplying those back into its component bytes.

This shows us the that result is obtained by (effectively) dividing by 256.

Similarly, a division is done by first multiplying the dividend by 256 (copy it one byte higher).

Note though the precision loss. A12 is A10 / A11 but A14 is the actual value represented by the final value.

This gives a range of 0 -> 65535+(255/256).

But IIRC Elite might treat this a signed and the 8th bit of the high byte is the sign bit.

Hence you'd then have a range of around the +/- 32768 range.

Edited by Wrathchild
##### Share on other sites

I don't want to offend anyone, who wants to help, but I had asked for a library, not explanations of fixed point.

The four base operation are not the problem.

The problem are all the stuff like sin, log etc.

But there seem to be no library for that stuff, so I will have to program that myself.

Edited by JoSch
##### Share on other sites

None taken, I tried by looking at the sources (the BBC ones are also available) but lifting the routines from those doesn'the appear straight forward, should have mentioned that

##### Share on other sites

That's my problem, too.

##### Share on other sites

The problem are all the stuff like sin, log etc.

But there seem to be no library for that stuff, so I will have to program that myself.

What's the purpose if I may ask ?

Trig. functions are used often in demos, games etc. but I can't think of where one would use Log ? (beside to do fast mul-div ...)

##### Share on other sites

What's the purpose if I may ask ?

Trig. functions are used often in demos, games etc. but I can't think of where one would use Log ? (beside to do fast mul-div ...)

Well, log will not be needed directly, but IIRC there are implementations of sqrt, which use that. My point really was to have a complete library.

You can find the cc65-fixed-point-part for sin/cos here:

https://github.com/cc65/cc65/blob/master/libsrc/common/cc65_sincos.s

Nice find. I didn't see that.

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

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.

×   Your previous content has been restored.   Clear editor

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

×