473,385 Members | 1,655 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

scaling coefficients for c

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
Sep 21 '06 #1
5 1951

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

Sep 21 '06 #2
In article <AI******************************@giganews.com>,
sonos <so***@nospam.comwrote:
[scaling coefficients]
>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.
--
Okay, buzzwords only. Two syllables, tops. -- Laurie Anderson
Sep 21 '06 #3

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.

Sep 21 '06 #4
Tom St Denis wrote:
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
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
The semicolon. That's 'float myfirstfloat = 0.58642;'.

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Sep 21 '06 #5
sonos wrote:
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.

--
Thad
Sep 23 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Atul Kshirsagar | last post by:
Hello, GIL prevents my C++ application embedding and extending python to scale even though I spawn multiple C++ threads. I read lot of references on internet about using multiple processes rather...
0
by: Colin | last post by:
Hi, It seems vs.net does some automatic scaling in the ide. like if i take a project develloped under 96 dpi low res and load it under 120 dpi hi res it does all kinds of automatic resizing. this...
1
by: susie_richie_30 | last post by:
Hi, I am trying to apply gray scaling to color as well as black/white images. I have tried using the pixel by pixel approach to achieve the scaling. But the particular approach has a issue with...
0
by: Steven | last post by:
Hello, I have a problem in scaling printing. I use standard print dialog. When I click the button of "Property" in this dialog, it shows the printer's property dialog. As usual, there is a...
0
by: Steven | last post by:
Hello, I have a problem in scaling printing. I use standard print dialog. When I click the button of "Property" in this dialog, it shows the printer's property dialog. As usual, there is a...
3
by: Larry Serflaten | last post by:
I am taking a 256 color bitmap from a file and scaling it up X 16 to a 32bppPARGB bitmap in memory. I copy that image to the screen. After scaling, the edges of all the lines and colors are...
17
by: IanIpp | last post by:
We have a 3 month old quad processor/dual core server running SQL Server 2005 and already it is getting close to hitting the CPU wall. An 8 way CPU box is prohibitively expensive and out of the...
4
by: xDrDoSx | last post by:
does any one have good tutorials or articles on PHP and database scaling?
1
by: candacefaye1 | last post by:
1. write a C++ program to decide if the coefficients of a quadratic equation have real roots. The three choices will be to write the message “zero divide” when A is zero, write the message “no real...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.