scaling coefficients for c

S

sonos

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
and the same fraction in binary
bin == 0.1001011
and the same fraction in hex
0.961F9F01C

but how do I set a c variable in binary form for fractions?

thanks
 
T

Tom St Denis

sonos said:
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
and the same fraction in binary
bin == 0.1001011
and the same fraction in hex
0.961F9F01C

but how do I set a c variable in binary form for fractions?

Why would you? It'd only be in the source, so do a one time conversion
to decimal and be done with.

float myfirstfloat = 0.58642.

???

Am I missing something here?

Tom
 
W

Walter Roberson

and the same fraction in hex
0.961F9F01C
but how do I set a c variable in binary form for fractions?

unsigned long scaling = 0x961F9F01;


Note: The final 'C' of your fraction doesn't fit into a typical 32 bit
integral number. You will need to decide whether to truncate
(as shown here), or to round (final 1 becomes a 2), or to see if your
compiler supports variables longer than 32 bits and use a longer
fraction.
 
A

Ancient_Hacker

sonos said:
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.
 
J

Joe Wright

Tom said:
Why would you? It'd only be in the source, so do a one time conversion
to decimal and be done with.

float myfirstfloat = 0.58642.

???

Am I missing something here?

Tom
The semicolon. That's 'float myfirstfloat = 0.58642;'.
 
T

Thad Smith

sonos said:
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
and the same fraction in binary
bin == 0.1001011
and the same fraction in hex
0.961F9F01C

but how do I set a c variable in binary form for fractions?

I have two general rules:
1. Make your program human readable.
2. Make the compiler do the work.

I would define a type, scale factor, and conversion macro for the scaled
values:

typedef unsigned short tVmeas; /* input voltage * VMEAS_SCALE/volt */
#define VMEAS_SCALE 4096 /* tVmeas scale factor */
#define VOLTS_TO_VMEAS(v) ((tVmeas)((v)*VMEAS_SCALE+0.5))
....
tVmeas vin = VOLTS_TO_VMEAS(0.58642); /* input voltage, with default
value */

The scale factor is defined in one place (it need not be a power of 2).
There is a specific type, tVmeas, that is commented to show the units
of any associated variable. The macro VOLTS_TO_TVMEAS, if used with a
constant expression, is normally evaluated at compile time so that there
is no floating point arithmetic in the generated code. Compile time
conversion is not guaranteed, but works for all the compilers that I
have used, including many targeting 8-bit processors.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top