This is a group for folks designing and programming embedded systems using the Rabbit Semiconductor C-programmable microcontroller. Rabbit Semi is a spin-off from Z-World who makes a variety of embedded modules and tools. This group is not affiliated with either Rabbit or Z-World, but is a user forum for sharing ideas, asking questions,
flaunting knowledge, and other typical user group stuff. The Rabbit is a powerful uC, supported by a full-featured C-compiler.
power function error: pow(x,y) - Fernando Campos - Apr 11 16:29:39 2008
I am having a problem with the power function using DC 8.10. The
problem is that when I use the function: pow(x,y), and say I have pow
(10,2), the answer should be 100, however the answer from the
compiler is 99.99999. And of course, if I type-cast this to an
interger, I will get as a result 99, instead of 100.
Here is the small program, and I show the print out:
main(){
float fvalue; //floating value
int ivalue; //integer value
int i; //index
for(i=0;i<=3;i++){ //run iteration 0 to 3
fvalue = pow(10,i);
ivalue = (int)fvalue;
printf("i=%d; fvalue=%f; ivalue=%d\n",i,fvalue,ivalue);
}
}
The print out is the following:
i=0; fvalue=1.000000; ivalue=1
i=1; fvalue=9.999999; ivalue=9
i=2; fvalue=99.999992; ivalue=99
i=3; fvalue=999.999810; ivalue=999
Is there a function to round-up the floating value so that I can work
around this problem?
Thank you.
------------------------------------

(You need to be a member of rabbit-semi -- send a blank email to rabbit-semi-subscribe@yahoogroups.com )
Re: power function error: pow(x,y) - mehiegl - Apr 11 17:46:32 2008
The simplest way to round a float to the nearest integer value is to
add 0.5 to the float, then cast it to the appropriately sized integer
type. Or subtract 0.5 if the number value is negative.
--- In r...@yahoogroups.com, "Fernando Campos"
wrote:
>
> I am having a problem with the power function using DC 8.10. The
> problem is that when I use the function: pow(x,y), and say I have
pow
> (10,2), the answer should be 100, however the answer from the
> compiler is 99.99999. And of course, if I type-cast this to an
> interger, I will get as a result 99, instead of 100.
> Here is the small program, and I show the print out:
>
> main(){
> float fvalue; //floating value
> int ivalue; //integer value
> int i; //index
>
> for(i=0;i<=3;i++){ //run iteration 0 to 3
> fvalue = pow(10,i);
> ivalue = (int)fvalue;
> printf("i=%d; fvalue=%f; ivalue=%d\n",i,fvalue,ivalue);
> }
> }
>
> The print out is the following:
> i=0; fvalue=1.000000; ivalue=1
> i=1; fvalue=9.999999; ivalue=9
> i=2; fvalue=99.999992; ivalue=99
> i=3; fvalue=999.999810; ivalue=999
>
> Is there a function to round-up the floating value so that I can
work
> around this problem?
>
> Thank you.
>
------------------------------------

(You need to be a member of rabbit-semi -- send a blank email to rabbit-semi-subscribe@yahoogroups.com )Re: power function error: pow(x,y) - Steve Trigero - Apr 11 17:56:18 2008
You have to create your own rounding procedure. Add some fractional amount
to the result that is within an error range you can tolerate. Adding .5 might be a
little course, but it all depends on how accurate you want to be.
Steve
----- Original Message ----
From: Fernando Campos
To: r...@yahoogroups.com
Sent: Friday, April 11, 2008 1:27:17 PM
Subject: [rabbit-semi] power function error: pow(x,y)
I am having a problem with the power function using DC 8.10. The
problem is that when I use the function: pow(x,y), and say I have pow
(10,2), the answer should be 100, however the answer from the
compiler is 99.99999. And of course, if I type-cast this to an
interger, I will get as a result 99, instead of 100.
Here is the small program, and I show the print out:
main(){
float fvalue; //floating value
int ivalue; //integer value
int i; //index
for(i=0;i<=3; i++){ //run iteration 0 to 3
fvalue = pow(10,i);
ivalue = (int)fvalue;
printf("i=%d; fvalue=%f; ivalue=%d\n" ,i,fvalue, ivalue);
}
}
The print out is the following:
i=0; fvalue=1.000000; ivalue=1
i=1; fvalue=9.999999; ivalue=9
i=2; fvalue=99.999992; ivalue=99
i=3; fvalue=999.999810; ivalue=999
Is there a function to round-up the floating value so that I can work
around this problem?
Thank you.

(You need to be a member of rabbit-semi -- send a blank email to rabbit-semi-subscribe@yahoogroups.com )Re: power function error: pow(x,y) - Fernando Campos - Apr 11 18:40:29 2008
Thank you for your ideas to round the number. I added 0.5 to my
value, and then type-casted to (int), and it worked fine. My concern
was as to why Dynamic C had this error in the first place? Shouldn't
the Rabbit people fix this bug in their compiler?
Anyway, thanks for your help in getting my problem solved!
--- In r...@yahoogroups.com, "Fernando Campos"
wrote:
>
> I am having a problem with the power function using DC 8.10. The
> problem is that when I use the function: pow(x,y), and say I have
pow
> (10,2), the answer should be 100, however the answer from the
> compiler is 99.99999. And of course, if I type-cast this to an
> interger, I will get as a result 99, instead of 100.
> Here is the small program, and I show the print out:
>
> main(){
> float fvalue; //floating value
> int ivalue; //integer value
> int i; //index
>
> for(i=0;i<=3;i++){ //run iteration 0 to 3
> fvalue = pow(10,i);
> ivalue = (int)fvalue;
> printf("i=%d; fvalue=%f; ivalue=%d\n",i,fvalue,ivalue);
> }
> }
>
> The print out is the following:
> i=0; fvalue=1.000000; ivalue=1
> i=1; fvalue=9.999999; ivalue=9
> i=2; fvalue=99.999992; ivalue=99
> i=3; fvalue=999.999810; ivalue=999
>
> Is there a function to round-up the floating value so that I can
work
> around this problem?
>
> Thank you.
>
------------------------------------

(You need to be a member of rabbit-semi -- send a blank email to rabbit-semi-subscribe@yahoogroups.com )Re: Re: power function error: pow(x,y) - Scott Henion - Apr 11 18:44:00 2008
Fernando Campos wrote:
> Thank you for your ideas to round the number. I added 0.5 to my
> value, and then type-casted to (int), and it worked fine. My concern
> was as to why Dynamic C had this error in the first place? Shouldn't
> the Rabbit people fix this bug in their compiler?
>
> Anyway, thanks for your help in getting my problem solved!
>
It is not a bug. It is that nature of floating point. You need to learn
how to use FP effectively. If you need accuracy it it much better to use
properly scaled ints or longs.
Any compiler will have the same issues.
--
------------------------------------------
| Scott G. Henion| s...@shdesigns.org |
| Consultant | Stone Mountain, GA |
| SHDesigns http://www.shdesigns.org |
------------------------------------------
Rabbit libs: http://www.shdesigns.org/rabbit/
today's fortune
People need good lies. There are too many bad ones.
-- Bokonon, "Cat's Cradle" by Kurt Vonnegut, Jr.
------------------------------------

(You need to be a member of rabbit-semi -- send a blank email to rabbit-semi-subscribe@yahoogroups.com )
RE: Re: power function error: pow(x,y) - "IDE Solutions.us" - Apr 11 18:55:34 2008
Good comments Scott!
I agree, I have had to do float to long conversions ever since I started
using the original =93C=94 compiler, it is a standard process for me.
You may want to elaborate on what you described as =93If you need accuracy =
it
much better to use properly scaled int or long=94.
This user and others may benefit from you etiquette comments on this
subject.
If you have any functions to share that may help others to get a good head
start as well.
I wanted to thank you for your comments over the years, and I find them
helpful from time to time as well, Thanks!
Jim Ashby
IDE Solutions, Inc.
From: r...@yahoogroups.com [mailto:r...@yahoogroups.com] On
Behalf Of Scott Henion
Sent: Friday, April 11, 2008 5:42 PM
To: r...@yahoogroups.com
Subject: Re: [rabbit-semi] Re: power function error: pow(x,y)
Fernando Campos wrote:
> Thank you for your ideas to round the number. I added 0.5 to my=20
> value, and then type-casted to (int), and it worked fine. My concern=20
> was as to why Dynamic C had this error in the first place? Shouldn't=20
> the Rabbit people fix this bug in their compiler?=20
>
> Anyway, thanks for your help in getting my problem solved!
>=20
It is not a bug. It is that nature of floating point. You need to learn
how to use FP effectively. If you need accuracy it it much better to use
properly scaled ints or longs.
Any compiler will have the same issues.
--=20
------------------------------------------
| Scott G. Henion| s...@shdesigns.org |
| Consultant | Stone Mountain, GA |
| SHDesigns http://www.shdesigns.org |
------------------------------------------
Rabbit libs: http://www.shdesigns.org/rabbit/
today's fortune=20
People need good lies. There are too many bad ones.
-- Bokonon, "Cat's Cradle" by Kurt Vonnegut, Jr.
=20
------------------------------------

(You need to be a member of rabbit-semi -- send a blank email to rabbit-semi-subscribe@yahoogroups.com )
Re: power function error: pow(x,y) - mehiegl - Apr 12 6:33:49 2008
For the case he described, rounding to the nearest integer, 0.5 is
all the resolution you need.
I have a more involved procedure for rounding values to the second
decimal place of float values that one of our product displays in
scientific notation (0.00E+00) format. It's kind of floating point
intensive and I've never tried it on a Rabbit based design. It was
originally written for an instrument bases on a Pentium class chip
with an FPU.
Mark
--- In r...@yahoogroups.com, Steve Trigero
wrote:
>
> You have to create your own rounding procedure. Add some
fractional amount
> to the result that is within an error range you can tolerate.
Adding .5 might be a
> little course, but it all depends on how accurate you want to be.
>
> Steve
>
> ----- Original Message ----
> From: Fernando Campos
> To: r...@yahoogroups.com
> Sent: Friday, April 11, 2008 1:27:17 PM
> Subject: [rabbit-semi] power function error: pow(x,y)
>
> I am having a problem with the power function using
DC 8.10. The
> problem is that when I use the function: pow(x,y), and say I have
pow
> (10,2), the answer should be 100, however the answer from the
> compiler is 99.99999. And of course, if I type-cast this to an
> interger, I will get as a result 99, instead of 100.
> Here is the small program, and I show the print out:
>
> main(){
> float fvalue; //floating value
> int ivalue; //integer value
> int i; //index
>
> for(i=0;i<=3; i++){ //run iteration 0 to 3
> fvalue = pow(10,i);
> ivalue = (int)fvalue;
> printf("i=%d; fvalue=%f; ivalue=%d\n" ,i,fvalue, ivalue);
> }
> }
>
> The print out is the following:
> i=0; fvalue=1.000000; ivalue=1
> i=1; fvalue=9.999999; ivalue=9
> i=2; fvalue=99.999992; ivalue=99
> i=3; fvalue=999.999810; ivalue=999
>
> Is there a function to round-up the floating value so that I can
work
> around this problem?
>
> Thank you.
>
>
>
>
>