Searching \ for '[PIC]:MPLAB floating point in C' in subject line. ()
Make payments with PayPal - it's fast, free and secure! Help us get a faster server
FAQ page: www.piclist.com/techref/microchip/languages.htm?key=mplab
Search entire site for: 'MPLAB floating point in C'.

Exact match. Not showing close matches.
PICList Thread
'[PIC]:MPLAB floating point in C'
2002\02\22@100538 by Rich Mcelroy

flavicon
face
Does anyone have math libraries for using floating point variables in c.  I
am working on a PIC and I have seen microchip's AN575.  That would be very
useful if the application I was working with was in asm.  I have looked all
over for help on doing this in c.  Maybe someone here can help.  Thanks


Richard McElroy
CST Corporation
450 S. Cips St.
Watseka, IL 60970

815-432-6859 ext. 775

--
http://www.piclist.com hint: The list server can filter out subtopics
(like ads or off topics) for you. See http://www.piclist.com/#topics


2002\02\22@102231 by Dale Botkin

flavicon
face
What are you trying to do?  Float <--> integer conversion is just a matter
of casting, and I haven't seen a C compiler recently that didn't support
basic operations (+,-,/,*).  Standard C math functions will do a lot...
are you using a compiler without floating point support?

Dale
--
"Curiosity is the very basis of education and if you tell me that
curiosity killed the cat, I say only the cat died nobly."
         - Arnold Edinborough


On Fri, 22 Feb 2002, Rich Mcelroy wrote:

> Does anyone have math libraries for using floating point variables in c.  I
> am working on a PIC and I have seen microchip's AN575.  That would be very
> useful if the application I was working with was in asm.  I have looked all
> over for help on doing this in c.  Maybe someone here can help.  Thanks

--
http://www.piclist.com hint: The list server can filter out subtopics
(like ads or off topics) for you. See http://www.piclist.com/#topics


2002\02\22@102848 by Rich Mcelroy

flavicon
face
I'm using Bytecraft's compiler for MPLAB.  it does basic math, but the
results I'm getting indicate that it is droping anything after the integer
on my calculations.  I'm trying to calculate a percentage:

Percent = A/B * 100;  something like that.  I have been getting a zero for
percent everytime.

{Original Message removed}

2002\02\22@103721 by Dale Botkin

flavicon
face
Can you post more of the code...  like the variable declarations for
Percent, A & B, etc.  I've never used Bytecraft's compiler, but something
like that shouldn't be a challenge...

Dale
--
"Curiosity is the very basis of education and if you tell me that
curiosity killed the cat, I say only the cat died nobly."
         - Arnold Edinborough


On Fri, 22 Feb 2002, Rich Mcelroy wrote:

> I'm using Bytecraft's compiler for MPLAB.  it does basic math, but the
> results I'm getting indicate that it is droping anything after the integer
> on my calculations.  I'm trying to calculate a percentage:
>
> Percent = A/B * 100;  something like that.  I have been getting a zero for
> percent everytime.

--
http://www.piclist.com hint: The list server can filter out subtopics
(like ads or off topics) for you. See http://www.piclist.com/#topics


2002\02\22@103926 by michael brown

flavicon
face
----- Original Message -----
From: "Rich Mcelroy" <spam_OUTrmcelroyTakeThisOuTspamCSTSURVEY.COM>
To: <.....PICLISTKILLspamspam@spam@MITVMA.MIT.EDU>
Sent: Friday, February 22, 2002 9:48 AM
Subject: Re: [PIC]:MPLAB floating point in C


> I'm using Bytecraft's compiler for MPLAB.  it does basic math, but the
> results I'm getting indicate that it is droping anything after the integer
> on my calculations.  I'm trying to calculate a percentage:
>
> Percent = A/B * 100;  something like that.  I have been getting a zero for
> percent everytime.

Try multiplying A by 100 first and then divide by B.     eg. x = (A * 100) /
B

IOW, eliminate the fractional parts by scaling them to integers before doing
the divide.

michael brown

--
http://www.piclist.com hint: The list server can filter out subtopics
(like ads or off topics) for you. See http://www.piclist.com/#topics


2002\02\22@104342 by Eoin Ross

flavicon
face
Try A*100/B - will have the same end effect to getting a percent - of course no decimal places.

>>> rmcelroyspamKILLspamCSTSURVEY.COM 02/22/02 10:48AM >>>
I'm using Bytecraft's compiler for MPLAB.  it does basic math, but the
results I'm getting indicate that it is droping anything after the integer
on my calculations.  I'm trying to calculate a percentage:

Percent = A/B * 100;  something like that.  I have been getting a zero for
percent everytime.

{Original Message removed}

2002\02\22@110018 by Peter Onion

flavicon
face
On 22-Feb-02 Rich Mcelroy wrote:
> I'm using Bytecraft's compiler for MPLAB.  it does basic math, but the
> results I'm getting indicate that it is droping anything after the integer
> on my calculations.  I'm trying to calculate a percentage:
>
> Percent = A/B * 100;  something like that.  I have been getting a zero for
> percent everytime.

What are the types of A & B please ?

If they are integers, then the result of the division will be an integer.

I would try

(100.0 * A) / B

That should "promote" the result to be a floating point type.  See Page 44 of
the Second edition of K&R.

Peter.

--
http://www.piclist.com hint: The list server can filter out subtopics
(like ads or off topics) for you. See http://www.piclist.com/#topics


2002\02\22@110854 by Rich Mcelroy

flavicon
face
Multiplying A by 100 first would work if it wasn't already a large number.
I have declared it as an unsigned long and it is usually around 4500-6000,
if I multiply by 100 I overflow it.

{Original Message removed}

2002\02\22@110903 by Rich Mcelroy

flavicon
face
A is a signed long, and B is an unsigned long.  two large values.

-----Original Message-----
From: Peter Onion [.....ponionKILLspamspam.....SRD.BT.CO.UK]
Sent: Friday, February 22, 2002 10:04 AM
To: EraseMEPICLISTspam_OUTspamTakeThisOuTMITVMA.MIT.EDU
Subject: Re: [PIC]:MPLAB floating point in C


On 22-Feb-02 Rich Mcelroy wrote:
> I'm using Bytecraft's compiler for MPLAB.  it does basic math, but the
> results I'm getting indicate that it is droping anything after the integer
> on my calculations.  I'm trying to calculate a percentage:
>
> Percent = A/B * 100;  something like that.  I have been getting a zero for
> percent everytime.

What are the types of A & B please ?

If they are integers, then the result of the division will be an integer.

I would try

(100.0 * A) / B

That should "promote" the result to be a floating point type.  See Page 44
of
the Second edition of K&R.

Peter.

--
http://www.piclist.com hint: The list server can filter out subtopics
(like ads or off topics) for you. See http://www.piclist.com/#topics

--
http://www.piclist.com hint: The list server can filter out subtopics
(like ads or off topics) for you. See http://www.piclist.com/#topics


2002\02\22@113352 by Peter Onion

flavicon
face
On 22-Feb-02 Rich Mcelroy wrote:
> Multiplying A by 100 first would work if it wasn't already a large number.
> I have declared it as an unsigned long and it is usually around 4500-6000,
> if I multiply by 100 I overflow it.

Hmmmm I guess the long must only be 16 bits ?   Not quite what K&R intended.

Ok, so multiply by 1.0 as that is a float and will cause the result to be a
float.

((1.0 * A) / B) * 100.0

That should do the trick I think.

Peter

--
http://www.piclist.com hint: The list server can filter out subtopics
(like ads or off topics) for you. See http://www.piclist.com/#topics


2002\02\22@114208 by Dale Botkin

flavicon
face
On Fri, 22 Feb 2002, Rich Mcelroy wrote:

> A is a signed long, and B is an unsigned long.  two large values.

That's probably the problem.  Dunno about Bytecraft, but the compiler
probably isn't quite intiutive enough to know that you want to perform an
operation with two longs and get a float result.  It's probably going to
do a long mult, then try to map the result into a float variable...  the
results aren't going to be pretty, I've found.

Try this:

float Percent;
signed long A;
unsigned long B;

Percent = (float)A/(float)B;
Percent *= 100;

You may want to take a look at the resulting code size.  I find my
compiler often generates much smaller, cleaner code with something like
this:

float Percent, fA, fB;
signed long A;
unsigned long B;

fA = (float)A;
fB = (float)B;
Percent = fA/fB;
Percent *= 100;

Dale
--
"Curiosity is the very basis of education and if you tell me that
curiosity killed the cat, I say only the cat died nobly."
         - Arnold Edinborough

--
http://www.piclist.com hint: The list server can filter out subtopics
(like ads or off topics) for you. See http://www.piclist.com/#topics


2002\02\22@114637 by Rich Mcelroy

flavicon
face
Dale,

that would be great, and I actually tried to do that first, but Bytecraft
doesn't recognize "float" as a type def.  Do you know of a compiler for
MPLAB that does?

Richard

{Original Message removed}

2002\02\22@115032 by Dale Botkin

flavicon
face
Huh?  No float support at all?  Wow.  CCS, Hi-Tech, even CC5X do.

Dale
--
"Curiosity is the very basis of education and if you tell me that
curiosity killed the cat, I say only the cat died nobly."
         - Arnold Edinborough


On Fri, 22 Feb 2002, Rich Mcelroy wrote:

> Dale,
>
> that would be great, and I actually tried to do that first, but Bytecraft
> doesn't recognize "float" as a type def.  Do you know of a compiler for
> MPLAB that does?

--
http://www.piclist.com hint: The list server can filter out subtopics
(like ads or off topics) for you. See http://www.piclist.com/#topics


2002\02\22@120756 by Al Williams

flavicon
face
Just to point out, you can get floating point wrapped up in a chip:
http://www.al-williams.com/awce/pak1.htm.

Also, if you do use the Microchip routines, drop by our site and pick up
our Windows float/ieee/microchip float conversion program -- a free
download. http://www.al-williams.com/awce (look for Fconvert towards the
bottom).

Good luck!

Al Williams
AWC
* Easy RS-232 Prototyping
http://www.al-williams.com/awce/rs1.htm



> {Original Message removed}

2002\02\22@125745 by Kevin Blain

flavicon
face
That's why then

Do somehing like Percent = (double) A / (double) B

Cast K&R page 45

{Quote hidden}

http://www.piclist.com/#topics

--
http://www.piclist.com hint: The list server can filter out subtopics
(like ads or off topics) for you. See http://www.piclist.com/#topics




*****************************************************************
This email has been checked by the altohiway e-Sweeper Service
*****************************************************************

--
http://www.piclist.com hint: The list server can filter out subtopics
(like ads or off topics) for you. See http://www.piclist.com/#topics


2002\02\22@125957 by Rich Mcelroy

flavicon
face
Bytecraft doesn't recognize the type defs - double or float, but thank you
for the advice

-----Original Message-----
From: Kevin Blain [spamBeGonekevinbspamBeGonespamWOODANDDOUGLAS.CO.UK]
Sent: Friday, February 22, 2002 10:13 AM
To: TakeThisOuTPICLISTEraseMEspamspam_OUTMITVMA.MIT.EDU
Subject: Re: [PIC]:MPLAB floating point in C


That's why then

Do somehing like Percent = (double) A / (double) B

Cast K&R page 45

{Quote hidden}

http://www.piclist.com/#topics

--
http://www.piclist.com hint: The list server can filter out subtopics
(like ads or off topics) for you. See http://www.piclist.com/#topics




*****************************************************************
This email has been checked by the altohiway e-Sweeper Service
*****************************************************************

--
http://www.piclist.com hint: The list server can filter out subtopics
(like ads or off topics) for you. See http://www.piclist.com/#topics

--
http://www.piclist.com hint: The list server can filter out subtopics
(like ads or off topics) for you. See http://www.piclist.com/#topics


2002\02\22@134139 by Herbert Graf

flavicon
face
Are A and B integers? If so the reason you are getting zero is because the
compiler is doing integer math. You have to use "casting". Try doing it this
way instead: ((float)A)/B * 100.0. While this doesn't look like it is any
different from what you are doing it most certainly is. The (float) tells
the compiler to treat A as a float (even though it is an integer) and the
compiler converts the A to a float. Then it sees the division, since A is a
float the division must be done with two floats, so the compiter converts B
to a float as well, and then does the division, with the result being a
float. The 100.0 isn't really necessary but it does give a hint to what you
are doing if someone else is reading your code. Please note that I don't
have that compiler, I'm simply speaking about standard ANSI C, I'm sure that
compiler has these features. TTYL

> {Original Message removed}

2002\02\22@151010 by Olin Lathrop

face picon face
> Does anyone have math libraries for using floating point variables in c.
I
> am working on a PIC and I have seen microchip's AN575.  That would be very
> useful if the application I was working with was in asm.  I have looked
all
> over for help on doing this in c.  Maybe someone here can help.  Thanks

You can mix assembler and C, the processor won't know.  At worst you might
have to write a small assembler wrapper for the canned routines to implement
whatever the C calling conventions are for your compiler.

You could also ask your compiler vendor about floating point math libraries.


********************************************************************
Olin Lathrop, embedded systems consultant in Littleton Massachusetts
(978) 742-9014, RemoveMEolinspam_OUTspamKILLspamembedinc.com, http://www.embedinc.com

--
http://www.piclist.com hint: The list server can filter out subtopics
(like ads or off topics) for you. See http://www.piclist.com/#topics


2002\02\22@173102 by uter van ooijen & floortje hanneman

picon face
> That's probably the problem.  Dunno about Bytecraft, but the compiler
> probably isn't quite intiutive enough to know that you want to perform an
> operation with two longs and get a float result.  It's probably going to
> do a long mult, then try to map the result into a float variable...  the
> results aren't going to be pretty, I've found.

But that is exactly what the compiler is required to do, if it implements C.
If you want a fractional result, cast one of the operands to float (or
multiply by 1.0)

((float)A)/B
or
1.0*A/B

etc.

Wouter van Ooijen

--
Van Ooijen Technische Informatica: http://www.voti.nl
Jal compiler, Wisp programmer, WLoader bootloader, PICs kopen

--
http://www.piclist.com hint: The list server can filter out subtopics
(like ads or off topics) for you. See http://www.piclist.com/#topics


2002\02\22@184222 by David Koski

flavicon
face
If you are not already using floating point in your program, using it now is
likely to increase your code size a lot.  If you have it, maybe that is okay.
But this is why C gets a "bad rap" over assembly.  It is usually important to
consider the result of your C statements before accepting a quick solution.  I
suggest "rolling your own" function in C to handle the overflow/underflow that
results from integer math.

David
RemoveMEdavidTakeThisOuTspamspamKosmosIsland.com

On Fri, 22 Feb 2002 10:29:35 -0600
Rich Mcelroy <EraseMErmcelroyspamspamspamBeGoneCSTSURVEY.COM> wrote:

> A is a signed long, and B is an unsigned long.  two large values.
>
> {Original Message removed}

2002\02\23@060709 by Olin Lathrop

face picon face
> Bytecraft doesn't recognize the type defs - double or float, but thank you
> for the advice

I don't like compilers on this tiny resource-limited systems, but I don't
know what amazes me more: That somebody tries to pass off crap like that as
a commercial product or that somebody else actually bought it.  There has
got to be a "real" compiler out there for PICs.


********************************************************************
Olin Lathrop, embedded systems consultant in Littleton Massachusetts
(978) 742-9014, RemoveMEolinKILLspamspamembedinc.com, http://www.embedinc.com

--
http://www.piclist.com hint: The PICList is archived three different
ways.  See http://www.piclist.com/#archives for details.


2002\02\23@101009 by Dale Botkin

flavicon
face
On Fri, 22 Feb 2002, Olin Lathrop wrote:

> > Bytecraft doesn't recognize the type defs - double or float, but thank you
> > for the advice
>
> I don't like compilers on this tiny resource-limited systems, but I don't
> know what amazes me more: That somebody tries to pass off crap like that as
> a commercial product or that somebody else actually bought it.  There has
> got to be a "real" compiler out there for PICs.

Geez, not again.  The compiler I use, while not 100.000 (or 99.999...)
percent ANSI compliant, is a "real" compiler, as evidenced by the wide
variety of "real" code it has produced over the past year and a half for
"real" products using "real" PICs.  I'm sure there are even better ones
available.  So far I have not found a single instance of something that
can't be done, and done well, with the compiler I use -- the one that
shortens my development time and allows me to complete projects in far
less time than I would without it.

Don't just assume that because someone else uses different tools than your
particular favorites that it just won't work or will somehow produce
vastly inferior results.  Some people don't like assemblers, some don't
like C, some don't like BASIC.  In each case, obviously, someone has found
a set of circumstances for which the language or the means of translating
it to machine code is a good fit.

Dale

--
http://www.piclist.com hint: The PICList is archived three different
ways.  See http://www.piclist.com/#archives for details.


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