4
$\begingroup$

I am working with a library that needs input in a Fixed Point notation. I’d like to figure out a way to convert the floating point results into fixed point representation.

The fixed point length is 16 bit. Numbers can be represented with a variable number of bits and I can specify the format of the bits. For instance, I can represent a number as Signed[3.13], upto 3 bits are used to represent the integer portion, and 13 bits are used for the fraction (resolution of 2^-13).

Q1. What is the best method to convert Mathematica floating point output into a 16 bit number. Ideally, I’d like a function like:

f[x_, signbits_, integerbits_, fractionbits_]

Q2. Given a set of numbers, what is the best method to determine the ideal number of integer bits and fractional bits used for the representation in order to minimize truncation errors

data = {0.0000618365, 0.0000701533, -0.0000747471, 0.0000595436, 0.0000705533, \
0.0000728675, 0.0000711056, 0.0000684559, 0.0000753624, -0.0000557638}
$\endgroup$
4
  • $\begingroup$ have you had a look at ComputerNumber? $\endgroup$
    – Stefan
    Commented Feb 27, 2014 at 12:22
  • $\begingroup$ I’ve actually figured this out and was going to post a detailed post when I find some time this weekend. $\endgroup$
    – Pam
    Commented Feb 27, 2014 at 15:28
  • $\begingroup$ Pam, I notice that you never posted the answer you intended to. I have trouble returning to such things myself, but in case you merely forgot here's a reminder. :-) $\endgroup$
    – Mr.Wizard
    Commented Jul 3, 2014 at 19:25
  • $\begingroup$ Thanks. Am away for the long weekend... Will post next week! $\endgroup$
    – Pam
    Commented Jul 3, 2014 at 23:40

1 Answer 1

6
$\begingroup$

Answering my own question from a while ago.

Turns out the easiest method is using MMA’s built-in Computer Arithmetic package

<< ComputerArithmetic`

(*Set Math Parameters*)

SetArithmetic[6, 10, ExponentRange -> {-20, 20}];

fpConvert[x_, integerbits_, fractionbits_] := ComputerNumber[IntegerPart[x] + Round[FractionalPart[x], 2^-fractionbits]];

Let’s test the output:

N[fpConvert[Pi, 3, 2], {8, 8}]
N[fpConvert[Pi, 3, 4], {8, 8}]
N[fpConvert[Pi, 3, 8], {8, 8}]
N[fpConvert[Pi, 3, 13], {8, 8}]

Works fine.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.