Black Lives Matter - Action and Equality. ... Adafruit joins the Stop Hate for Profit campaign.
0

apparent bug in pow()
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

apparent bug in pow()

by jhodges on Mon Jan 26, 2009 11:38 pm

So it appears i've either run across a bug in a pretty basic library, or (much more likely) I screwed up my arduino installation some time ago and now I'm paying for what i don't even remember exactly doing.

I'm runinng arduino-012 on ubuntu 8.10 64bit. For a while there I was trying to get the 328p board working, but ran into difficulties and gave up for a while. I did install the Arduino328.diff patch, and at some point made an update to some of the wiring libraries that were related to math in some way.. i don't remember exactly what i changed.

Anyhow, I have 4 switches mounted up that I want to read as binary values from zero to 15 for my preset sequences. for a long time, i've been working with just 2 switches, but recently got to the point where i wanted to start using it all. basically i loop through the code, and if it's set to 'on', i add 2**index to the final value where index increments from zero to three. I've prototyped the same code in python and c++ and it is theoretically sound. My issue is that I am actually getting wrong values back from pow().

I wrote this test program:
Code: Select all | TOGGLE FULL SIZE
int zero=0;
int one=1;
int two=2;
int three=3;

void setup()
{
  Serial.begin(9600);
  Serial.println(zero,DEC);
  Serial.println(one,DEC);
  Serial.println(two,DEC);
  Serial.println(three,DEC);
  zero=pow(2,zero);
  one=pow(2,one);
  two=pow(2,two);
  three=pow(2,three);
  Serial.println(zero,DEC);
  Serial.println(one,DEC);
  Serial.println(two,DEC);
  Serial.println(three,DEC);
}

and this is what i get back:

Code: Select all | TOGGLE FULL SIZE
0
1
2
3
1
2
3
7


However, i should be getting back 1 2 4 8.

Anybody have any ideas on this? I can't find any references to pow() in any of the arduino installed libraries.
jhodges
 
Posts: 12
Joined: Fri Feb 29, 2008 8:34 am

Re: apparent bug in pow()

by niksun on Tue Jan 27, 2009 12:30 am

Technically, "pow()" takes two doubles and returns a double. I wonder if that's the problem...
Niksun

If you thought before that science was certain--well, that is just an error on your part. -- Richard Feynman
niksun
 
Posts: 202
Joined: Sun Jul 27, 2008 9:59 pm

Re: apparent bug in pow()

by westfw on Tue Jan 27, 2009 2:14 am

Don't forget truncation going from float to int.
If pow(2.0, 2.0) returns 3.999999, assigning the value to an int will give you "3" try adding 0.5 to get rounding.

westfw
 
Posts: 1645
Joined: Fri Apr 27, 2007 1:01 pm
Location: SF Bay area

Re: apparent bug in pow()

by mtbf0 on Tue Jan 27, 2009 5:06 am

instead of pow(), use
Code: Select all | TOGGLE FULL SIZE
  zero = 1 << zero;
  one = 1 << one;
  two = 1 << two;
  three = 1 << three;
and see how your code shrinks and zips along much more quickly.
"i want to lead a dissipate existence, play scratchy records and enjoy my decline" - iggy pop, i need more
User avatar
mtbf0
 
Posts: 1645
Joined: Sat Nov 10, 2007 12:59 am
Location: oakland ca

Re: apparent bug in pow()

by jhodges on Tue Jan 27, 2009 8:55 pm

I tried some variations on doubles/floats and didnt' get any different results. However, I totally forgot about bitwise shift operations -- hellz yeah, that's the way to go! Thanks for the tip.
jhodges
 
Posts: 12
Joined: Fri Feb 29, 2008 8:34 am

Re: apparent bug in pow()

by jhodges on Tue Jan 27, 2009 10:46 pm

mtbf0 wrote:instead of pow(), use
Code: Select all | TOGGLE FULL SIZE
  zero = 1 << zero;
  one = 1 << one;
  two = 1 << two;
  three = 1 << three;
and see how your code shrinks and zips along much more quickly.


That worked like a charm, and reduced my code size a good bit. Thanks for the tip.

btw, i tested double again using floats, still no dice. Fortunately, I don't care anymore :)
jhodges
 
Posts: 12
Joined: Fri Feb 29, 2008 8:34 am

Please be positive and constructive with your questions and comments.