# Problem with pow(); function

**URL:** <https://forum.arduino.cc/t/problem-with-pow-function/216277>\
**Category:** Programming\
**Created:** [February 28, 2014, 8:04pm UTC](https://forum.arduino.cc/t/problem-with-pow-function/216277 "2014-02-28T20:04:11Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![naut](https://avatars.discourse-cdn.com/v4/letter/n/6a8cbe/32.png) [@naut](https://forum.arduino.cc/u/naut)\
**Post date:** [February 28, 2014, 8:04pm UTC](https://forum.arduino.cc/t/problem-with-pow-function/216277/1 "2014-02-28T20:04:11Z")

</div>

hi

I'm getting false results from the pow(); function, e.g. the following code prints 99 instead of 100.

```cpp
byte p = 2; 
unsigned int power10 = pow(10, p);
Serial.print("power10 = ");
Serial.println(power10);

```

with powers of 0 and 1 the results are correct. Power of 3 results in 999 instead of 1000.

Have I overlooked something?

thanks

---

<div class="post-metadata">

**Author:** ![Coding\_Badly](https://avatars.discourse-cdn.com/v4/letter/c/f14d63/32.png) [@Coding\_Badly](https://forum.arduino.cc/u/Coding_Badly)\
**Post date:** [February 28, 2014, 8:11pm UTC](https://forum.arduino.cc/t/problem-with-pow-function/216277/2 "2014-02-28T20:11:44Z")

</div>

Yes. Google. 😉  
[https://www.google.com/search?q=site%3Aforum.arduino.cc+pow](https://www.google.com/search?q=site%3Aforum.arduino.cc+pow)

---

<div class="post-metadata">

**Author:** ![anon94801460](https://avatars.discourse-cdn.com/v4/letter/a/278dde/32.png) [@anon94801460](https://forum.arduino.cc/u/anon94801460)\
**Post date:** [February 28, 2014, 8:13pm UTC](https://forum.arduino.cc/t/problem-with-pow-function/216277/3 "2014-02-28T20:13:35Z")

</div>

The result of the pow() function is a floating point number and that will very rarely be exactly equal to an integer value. In the "incorrect" case you are probably converting a result like 999.999 to 999 by the integer truncation.

---

<div class="post-metadata">

**Author:** ![naut](https://avatars.discourse-cdn.com/v4/letter/n/6a8cbe/32.png) [@naut](https://forum.arduino.cc/u/naut)\
**Post date:** [February 28, 2014, 11:54pm UTC](https://forum.arduino.cc/t/problem-with-pow-function/216277/4 "2014-02-28T23:54:29Z")

</div>

thanks guys,

ye, I should've searched b4 creating the topic.

In any case I replaced the pow() function with a switch function and a few cases.

---

<div class="post-metadata">

**Author:** ![robtillaart](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/robtillaart/32/1098811_2.png) [@robtillaart](https://forum.arduino.cc/u/robtillaart)\
**Post date:** [March 1, 2014, 2:51pm UTC](https://forum.arduino.cc/t/problem-with-pow-function/216277/5 "2014-03-01T14:51:28Z")

</div>

another option is create a function for integer power like

```cpp
long ipow(int a, uint8_t b)
{
  long rv = 1;
  for (uint8_t  i = 0; i < b; ++i) rv *= a;
  return rv;
}

```

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 5, 2021, 8:33pm UTC](https://forum.arduino.cc/t/problem-with-pow-function/216277/6 "2021-05-05T20:33:48Z")

</div>

Closed on May 5, 2021, 8:33pm UTC


