DCrown
DCrown

Reputation: 57

How to correctly print a 2.30 fixed point variable

I'm obtaining a DFT using a cortex-M3. I'm calculating the magnitude using the CMSIS DSP function arm_cmplx_mag_q31. The documentation says that it returns the result in the format 2.30 (which I assume is 2 bits for the integer and 30 for the fractional part; without sign bit since the magnitude can't be negative).

I'm trying to output the result to the user, but I'm finding hard to print out the correct value.

I've tried using typedef to define a new union where I can store the integer and fractional part like this

/* 2.30 union*/
typedef union {
  uint32_t full;
  struct {
    uint32_t fpart:30;
    uint8_t  ipart:2;
  } parts;
} fixed2_30_t;

then I store the 2.30 magnitude result into a fixed2_30_t variable and try to print its parts

fixed2_30_t   result;
result = magnitude;

sprintf(msg, "final results %0d.%010u", 
        result.parts.ipart, result.parts.fpart / 1073741824);

I'm dividing the fractional part by 2^30 to scale it back to a decimal, but I'm not obtaining sensible results, and I'm not entirely sure my zero padding is correct.

What would be the correct way to print it? And how do you determine the zero-padding to use?

Thanks!

Upvotes: 3

Views: 3117

Answers (2)

chux
chux

Reputation: 154169

How to correctly print a 2.30 fixed point variable

To print to 10 decimal places after the . with a rounded value, scale the fraction by 1010 and then divide by 232.

Use unsigned long long math to insure probability. Note that the product below, with a maximal .fpart is a 64-bit positive value.

sprintf(msg, "final results %0d.%010llu", 
    result.parts.ipart, 
    //                                    add half the divisor
    (result.parts.fpart * 10000000000LLu + 0x40000000u/2) / 0x40000000u);

Note: with less than 10 fractional digits, rounding may change the integer portion.

Upvotes: 2

Jonathan Leffler
Jonathan Leffler

Reputation: 754820

Assuming your Cortex M3 has sufficiently good support for 64-bit integer arithmetic (supports C99 or later — that requires 64-bit arithmetic), then this code shows how it could be done.

#include <assert.h>
#include <inttypes.h>
#include <stdio.h>

static const uint64_t TWO_THIRTY  = 1UL << 30;
static const uint64_t ONE_BILLION = 1000000000;

static uint32_t fraction(uint32_t fpart)
{
    assert(fpart < TWO_THIRTY);
    uint64_t result = (fpart * ONE_BILLION) / TWO_THIRTY;
    assert(result < ONE_BILLION);
    return result;
}

int main(void)
{
     for (uint32_t i = 0; i < 32; i++)
         printf("%10" PRIu32 " = 0.%.9" PRIu32 "\n", i, fraction(i));
     for (uint32_t i = 64; i < TWO_THIRTY; i += (20 * i) / 19 + 1)
         printf("%10" PRIu32 " = 0.%.9" PRIu32 "\n", i, fraction(i));
     for (uint32_t i = TWO_THIRTY - 32; i < TWO_THIRTY; i++)
         printf("%10" PRIu32 " = 0.%.9" PRIu32 "\n", i, fraction(i));
     return 0;
}

The output I get is:

         0 = 0.000000000
         1 = 0.000000000
         2 = 0.000000001
         3 = 0.000000002
         4 = 0.000000003
         5 = 0.000000004
         6 = 0.000000005
         7 = 0.000000006
         8 = 0.000000007
         9 = 0.000000008
        10 = 0.000000009
        11 = 0.000000010
        12 = 0.000000011
        13 = 0.000000012
        14 = 0.000000013
        15 = 0.000000013
        16 = 0.000000014
        17 = 0.000000015
        18 = 0.000000016
        19 = 0.000000017
        20 = 0.000000018
        21 = 0.000000019
        22 = 0.000000020
        23 = 0.000000021
        24 = 0.000000022
        25 = 0.000000023
        26 = 0.000000024
        27 = 0.000000025
        28 = 0.000000026
        29 = 0.000000027
        30 = 0.000000027
        31 = 0.000000028
        64 = 0.000000059
       132 = 0.000000122
       271 = 0.000000252
       557 = 0.000000518
      1144 = 0.000001065
      2349 = 0.000002187
      4822 = 0.000004490
      9898 = 0.000009218
     20317 = 0.000018921
     41704 = 0.000038839
     85603 = 0.000079724
    175712 = 0.000163644
    360673 = 0.000335902
    740329 = 0.000689485
   1519623 = 0.001415259
   3119227 = 0.002905006
   6402624 = 0.005962908
  13142229 = 0.012239654
  26976155 = 0.025123502
  55372108 = 0.051569294
 113658538 = 0.105852762
 233299105 = 0.217276723
 252826200 = 0.235462747
 292908132 = 0.272791955
 375181572 = 0.349415067
 544058633 = 0.506694086
 664650111 = 0.619003652
 686129076 = 0.639007497
 730217478 = 0.680068021
 820714724 = 0.764350149
1006472229 = 0.937350307
1073741792 = 0.999999970
1073741793 = 0.999999971
1073741794 = 0.999999972
1073741795 = 0.999999972
1073741796 = 0.999999973
1073741797 = 0.999999974
1073741798 = 0.999999975
1073741799 = 0.999999976
1073741800 = 0.999999977
1073741801 = 0.999999978
1073741802 = 0.999999979
1073741803 = 0.999999980
1073741804 = 0.999999981
1073741805 = 0.999999982
1073741806 = 0.999999983
1073741807 = 0.999999984
1073741808 = 0.999999985
1073741809 = 0.999999986
1073741810 = 0.999999986
1073741811 = 0.999999987
1073741812 = 0.999999988
1073741813 = 0.999999989
1073741814 = 0.999999990
1073741815 = 0.999999991
1073741816 = 0.999999992
1073741817 = 0.999999993
1073741818 = 0.999999994
1073741819 = 0.999999995
1073741820 = 0.999999996
1073741821 = 0.999999997
1073741822 = 0.999999998
1073741823 = 0.999999999

You can verify the using bc to verify the calculation, for example. I called the program fp71.

$ fp71 | awk '{print $1}' |
> { echo 'scale=9'; echo 'd=2^30'; while read value; do echo "$value / d"; done; } |
> bc
0
0
.000000001
.000000002
.000000003
.000000004
.000000005
.000000006
.000000007
.000000008
.000000009
.000000010
.000000011
.000000012
.000000013
.000000013
.000000014
.000000015
.000000016
.000000017
.000000018
.000000019
.000000020
.000000021
.000000022
.000000023
.000000024
.000000025
.000000026
.000000027
.000000027
.000000028
.000000059
.000000122
.000000252
.000000518
.000001065
.000002187
.000004490
.000009218
.000018921
.000038839
.000079724
.000163644
.000335902
.000689485
.001415259
.002905006
.005962908
.012239654
.025123502
.051569294
.105852762
.217276723
.235462747
.272791955
.349415067
.506694086
.619003652
.639007497
.680068021
.764350149
.937350307
.999999970
.999999971
.999999972
.999999972
.999999973
.999999974
.999999975
.999999976
.999999977
.999999978
.999999979
.999999980
.999999981
.999999982
.999999983
.999999984
.999999985
.999999986
.999999986
.999999987
.999999988
.999999989
.999999990
.999999991
.999999992
.999999993
.999999994
.999999995
.999999996
.999999997
.999999998
.999999999
$

These results agree — except I prefer the leading zero before the decimal point in the output.

Upvotes: 2

Related Questions