434,587 Members | 1,071 Online
Need help? Post your question and get tips & solutions from a community of 434,587 IT Pros & Developers. It's quick & easy.

# scaling coefficients for c

 P: n/a 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 Replies

 P: n/a 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

 P: n/a In article , sonos and the same fraction in hex0.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

 P: n/a 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

 P: n/a Tom St Denis wrote: sonos wrote: >Hi,I understand the requirement of scaling coefficients for fixed pointmicrocontrollers. However, I cannot seem to get past a simple issue... Howdo 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.58642and the same fraction in binarybin == 0.1001011and the same fraction in hex0.961F9F01Cbut 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

 P: n/a 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 discussion thread is closed

Replies have been disabled for this discussion.