Re: scaling coefficients for c




sonos wrote:
Hi,
I understand the requirement of scaling coefficients for fixed point
microcontrollers. However, I cannot seem to get past a simple issue... How
do I code a fraction in binary form for use in c code or asm language?

for example, consider the fraction in base 10:
dec == 0.58642

It depends. if your microcontroller has 8 bit arithmetic only, you'll
have to scale your fractions to fit the range 0..255 or -128..127. Not
only that, you'll have to scale your intermediate results so they don't
overflow or underflow.

if your microcontroller has 16 bit arithmetic, you'll have to scale
your fractions to fit the range 0..65535 or -32768..32767

For the 8-bit case, you're going to lose a lot of resolution. One
first cut might be to scale everything up by a factor of 100, so in
your case:

int dec=59

but I suspect your C can do 16-bit math, so a better choice might be
1000:

int dec=586;

again it depends on the exact math you want to do. For If you're going
to be multiplying, you have to ensure the product doesnt overflow 15 or
16 bits, so you have to scale the inputs so they're no more than 8 bits
each.

A smarter way would be to take the log2() of each operand and scale
them so the sum of the logs is less than 16 or 15.

Perhaps if you gave us some more example code we could make better
suggestions.



then whenever you add numbers, you just make sure all the operands have
been similarly scaled, and the sum will be scaled by 100 also.

.



Relevant Pages

  • Re: rules of fixed-point arithmetic
    ... Scale factor and base are known at compile time. ... the answer is to just not divide. ... One of the more convenient ways to do that is to define a division operator as x/, where M is a power-of-two scale factor that you select to ensure that your result is a fraction in the appropriate range: trading precision and range. ...
    (comp.arch)
  • [SLE] pstops problem
    ... I seem to have hit a problem with pstops. ... I want to scale up these latter ... The clue for this discovery was the statement in 'man pstops' ... The optional scale parameter scales the page by the fraction ...
    (SuSE)
  • Re: VNC versions and features
    ... > I believe all versions have the ability to scale the screen. ... > by percentage or fraction. ... more zeros and remove the obvious. ...
    (comp.security.firewalls)
  • Re: Screensize
    ... scale them by a fraction (e.g. increase linear dimensions by 50% in ... I would think that aliasing effects would appear and ... the bitmaps will scale smoothly. ...
    (rec.games.roguelike.development)