Searching \ for '[PIC]: Rounding the result of a flo ating poi' in subject line. ()
Help us get a faster server
FAQ page: www.piclist.com/techref/microchip/devices.htm?key=pic
Search entire site for: 'Rounding the result of a flo ating poi'.

Exact match. Not showing close matches.
'[PIC]: Rounding the result of a flo ating poi'
2002\07\05@045245 by

Hmm, you haven't understood what I was trying to get across,

I have to scale a value in the range 0-359 to within the range 0-114.

My 0-359 value is in the form of 3 bytes - representing hundreds, tens, units - so, I have to multiply the hundreds value (of the 0-359 value) by 31.7, multiply the tens value by 3.17 and the units value by 0.317.

I then add up the three results to give me a value in the range 0-114.

However, I'm not at all sure how I go about multiplying 8bit values by floating point values in the first place. Once I've worked out how to do this however, I assume I'm going to be left with a value that's also a floating point, so I need to know how to appropriately round the value to an 8bit integer value.

I guess what I need is an 8bit*16bit(?) floating point=8bit routine.

An example: InitialHundreds:2 InitialTens:7 InitialUnits:3 (273 degrees)

2*31.7=63.4
7*3.17=22.19
3*0.317=0.951

Total: 86.541 (for the 0-114 value)

Final 8 Bit value stored: 01010111 (87)

Hopefully this is a little clearer now? Any tips on how to proceed would be appreciated.

Chris.

{Original Message removed}
>My 0-359 value is in the form of 3 bytes - representing
>hundreds, tens, units - so, I have to multiply the
>hundreds value (of the 0-359 value) by 31.7, multiply
>the tens value by 3.17 and the units value by 0.317.

>I then add up the three results to give me a value
>in the range 0-114.

>However, I'm not at all sure how I go about multiplying
>8bit values by floating point values in the first place.
>Once I've worked out how to do this however, I assume I'm
>going to be left with a value that's also a floating point,
>so I need to know how to appropriately round the value
>to an 8bit integer value.

>I guess what I need is an 8bit*16bit(?) floating point=8bit routine.

At a guess I would say you need to get stuck into this bit of code using C
to make the math handling easier. Then use assembler to program after that
if your not too happy with C.

My recommendation would be to get Hitech C Lite for the 16F84/16F627 and try
doing this as a C module. I don't know if it will do linkable modules, but
you may get that enthused you do the whole thing in C :)

--
http://www.piclist.com hint: To leave the PICList
piclist-unsubscribe-requestmitvma.mit.edu

{Quote hidden}

Thanks for the tip - I wasnt aware there was a 'free' version of a PIC C compiler at all, might well be handy in the future.

I've only ever had experience with Visual Basic before(!) - Oh how I long for the CInt() function right now ;)

Anyway, I hope I've found something which will do the trick, the PICList code generator:
http://www.piclist.com/techref/piclist/codegen/constdivmul.htm

Seems to have generated some code I hope I can just plug-in-and-play with. Fingers crossed.

Chris.

The Information contained in this E-Mail and any subsequent correspondence
is private and is intended solely for the intended recipient(s).
For those other than the recipient any disclosure, copying, distribution,
or any action taken or omitted to be taken in reliance on such information
is prohibited and may be unlawful.

--
http://www.piclist.com hint: To leave the PICList
piclist-unsubscribe-requestmitvma.mit.edu

>Anyway, I hope I've found something which will do
>the trick, the PICList code generator:

Yes that will probably be suitable too. Another item on the web site I keep
forgetting. :)

--
http://www.piclist.com hint: To leave the PICList
piclist-unsubscribe-requestmitvma.mit.edu

First, fix your email settings to properly wrap lines at a reasonable value,
like 72 to 80.  You are sending unwrapped lines that are getting truncated
by some transport somewhere, and make a reply to you using Outlook Express
look like a mess.

> Hmm, you haven't understood what I was trying to get across,
>
> I have to scale a value in the range 0-359 to within the range 0-114.
>
> My 0-359 value is in the form of 3 bytes - representing hundreds, tens,
units - so, I have to multiply the hundreds value (of the 0-359 value) by
31.7, multiply the tens value by 3.17 and the units value by 0.317.
>
> I then add up the three results to give me a value in the range 0-114.
>
> However, I'm not at all sure how I go about multiplying 8bit values by
floating point values in the first place. Once I've worked out how to do
this however, I assume I'm going to be left with a value that's also a
floating point, so I need to know how to
{Quote hidden}

A simplifying trick would be to arrange the result value to be fixed point
with 8 fraction bits.  Then you don't need to use floating point.  Just
multiply each value by 256 times more than in your example, then ignore the
low byte of the result.  Using the same 273 --> 87 example:

SCALE = (115 / 360) * 256 = .3194 * 256 = 81.7778

RESULT = HUNDREDS*(SCALE*100) + TENS*(SCALE*10) + UNITS*SCALE
= 2*(8177) + 7*(817) + 3*81
= 16,354 + 5,719 + 243
= 22,316 = 572Ch

TruncateFraction(572Ch) = 57h = 87  Voila!

Note that this was all done using integer arithmetic.  For this algorithm,
you will need 16 bit integer add an multiply routines.  I don't give away my
PIC integer and floating point libraries, but I bet there is lots of free
code out there.  Note also that TruncateFraction requires no cycles at all.
You simply use the high byte of the 16 bit output value as the result.

*****************************************************************
Embed Inc, embedded system specialists in Littleton Massachusetts
(978) 742-9014, http://www.embedinc.com

--
http://www.piclist.com hint: To leave the PICList
piclist-unsubscribe-requestmitvma.mit.edu

(Olin, apologies for still sending with messed up wrapping, I'm forced into using outlook at work - did have a quick look for some sort of wrapping setting, couldn't find any.)

Thanks to all those who responded to my PIC arithmetic question, I used code from the PICList code generator in the end - but I've certainly learnt some useful things from your responses too.

Amazingly, it even seems to work too(!) My LCD PIC is receiving numbers from the Keypad PIC, doing the conversion and then sending them onto the Hub pic to do the appropriate relay switching (I'm building a glorified antenna positioner unit, in case anyone cares!). I'm really quite worried by how satisfying the sound of relays switching has become though ;)

Anyway, next on the list is to implement reading/writing settings to/from an EEPROM - no doubt I'll be asking questions about that soon (although I do have some routines from the net I'll try first, honest!)

Thanks,

Chris.

The Information contained in this E-Mail and any subsequent correspondence
is private and is intended solely for the intended recipient(s).
For those other than the recipient any disclosure, copying, distribution,
or any action taken or omitted to be taken in reliance on such information
is prohibited and may be unlawful.

--
http://www.piclist.com hint: To leave the PICList
piclist-unsubscribe-requestmitvma.mit.edu

More... (looser matching)
- Last day of these posts
- In 2002 , 2003 only
- Today
- New search...