# C++ Question

**URL:** <https://boards.straightdope.com/t/c-question/58836>\
**Category:** Factual Questions\
**Created:** [March 21, 2001, 6:33am UTC](https://boards.straightdope.com/t/c-question/58836 "2001-03-21T06:33:32Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ell](https://avatars.discourse-cdn.com/v4/letter/e/e47774/32.png) [@Ell](https://boards.straightdope.com/u/Ell)\
**Post date:** [March 21, 2001, 6:33am UTC](https://boards.straightdope.com/t/c-question/58836/1 "2001-03-21T06:33:32Z")

</div>

This is for a comp assignment but thanks for your help.

When this is used: cout \<\< pow(10,2);  
It returns 100 ( as you would expect)

but when I try: int(pow(10,2));

it returns 99!

Please help! It only does it for 10^2 ( ^1, ^3, ^4 all work!)

---

<div class="post-metadata">

**Author:** ![Blunt](https://avatars.discourse-cdn.com/v4/letter/b/ccd318/32.png) [@Blunt](https://boards.straightdope.com/u/Blunt)\
**Post date:** [March 21, 2001, 7:28am UTC](https://boards.straightdope.com/t/c-question/58836/2 "2001-03-21T07:28:49Z")

</div>

This may not be of any help to you, but I just tried it with MS Visual C++, and it returned 100 for both pow(10,2), and int(pow(10,2)). I’m not sure why it didn’t work for you though, sorry.

---

<div class="post-metadata">

**Author:** ![kferr](https://avatars.discourse-cdn.com/v4/letter/k/71e660/32.png) [@kferr](https://boards.straightdope.com/u/kferr)\
**Post date:** [March 21, 2001, 1:17pm UTC](https://boards.straightdope.com/t/c-question/58836/3 "2001-03-21T13:17:50Z")

</div>

I got the same result as Blunt using the Solaris Workshop compiler. What compiler and OS are you using?

---

<div class="post-metadata">

**Author:** ![cmkeller](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/cmkeller/32/118_2.png) [@cmkeller](https://boards.straightdope.com/u/cmkeller)\
**Post date:** [March 21, 2001, 2:54pm UTC](https://boards.straightdope.com/t/c-question/58836/4 "2001-03-21T14:54:14Z")

</div>

You’re in luck. I had this exact problem in an actual software production environment. We were using the formula year % int(pow(10, year\_digits)) to solve the Y2K problem. But some systems would give us dates that were about 80 years off.

It’s a floating-point processing problem. The pow() function returns a floating-point number which is close enough to 100 so that a floating-point variable sees “100.” However, if you cast it to an int, it will truncate the stuff after the decimal point and you will end up with 99.

If I recall correctly, this bug affected Windows 95B and no other operating system.

---

<div class="post-metadata">

**Author:** ![c\_goat](https://avatars.discourse-cdn.com/v4/letter/c/f4b2a3/32.png) [@c\_goat](https://boards.straightdope.com/u/c_goat)\
**Post date:** [March 21, 2001, 3:15pm UTC](https://boards.straightdope.com/t/c-question/58836/5 "2001-03-21T15:15:41Z")

</div>

You could also write your own pow() function that handles integers only, so you don’t have the floating-point truncate problem. Here’s one that should work.

```auto

long ipow(long base, long exp)
{
  long i;
  long retval = 1;

  for(i=0; i<exp; i++)
  {
    retval*=base;
  }

  return retval;
}

```

---

<div class="post-metadata">

**Author:** ![Joe\_Cool](https://avatars.discourse-cdn.com/v4/letter/j/ecb155/32.png) [@Joe\_Cool](https://boards.straightdope.com/u/Joe_Cool)\
**Post date:** [March 21, 2001, 7:36pm UTC](https://boards.straightdope.com/t/c-question/58836/6 "2001-03-21T19:36:06Z")

</div>

Am I correct in thinking that c++ calculates pow(a,b) as e[sup]b\*ln(a)[/sup]? That would account for the error.

---

<div class="post-metadata">

**Author:** ![tracer](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/tracer/32/20578_2.png) [@tracer](https://boards.straightdope.com/u/tracer)\
**Post date:** [March 21, 2001, 8:10pm UTC](https://boards.straightdope.com/t/c-question/58836/7 "2001-03-21T20:10:52Z")

</div>

cmkeller wrote:

> [@](#):
>
> If I recall correctly, this bug affected Windows 95B and no other operating system.

It’s **O/S-specific**?! Good lord, when did the operating system start providing floating-point functions?!

---

<div class="post-metadata">

**Author:** ![Punoqllads](https://avatars.discourse-cdn.com/v4/letter/p/d2c977/32.png) [@Punoqllads](https://boards.straightdope.com/u/Punoqllads)\
**Post date:** [March 21, 2001, 8:34pm UTC](https://boards.straightdope.com/t/c-question/58836/8 "2001-03-21T20:34:41Z")

</div>

> [@](#):
>
> \*Originally posted by tracer \*  
> It’s **O/S-specific**?! Good lord, when did the operating system start providing floating-point functions?! \*\*

Um, the early 1980’s, I think. Libm.so, anyone?

---

<div class="post-metadata">

**Author:** ![zev\_steinhardt](https://avatars.discourse-cdn.com/v4/letter/z/97f17d/32.png) [@zev\_steinhardt](https://boards.straightdope.com/u/zev_steinhardt)\
**Post date:** [March 21, 2001, 9:26pm UTC](https://boards.straightdope.com/t/c-question/58836/9 "2001-03-21T21:26:29Z")

</div>

> [@](#):
>
> \*Originally posted by c\_goat \*  
> \*\*You could also write your own pow() function that handles integers only, so you don’t have the floating-point truncate problem. Here’s one that should work.
> 
> ```auto
> 
> long ipow(long base, long exp)
> {
> long i;
> long retval = 1;
> 
> for(i=0; i<exp; i++)
> {
> retval*=base;
> }
> 
> return retval;
> }
> 
> ```
> 
> \*\*

Only if you don’t want to figure out 5[sup]2.5[/sup] or any other fractional exponent. 🙂

Zev Steinhardt

---

<div class="post-metadata">

**Author:** ![Ell](https://avatars.discourse-cdn.com/v4/letter/e/e47774/32.png) [@Ell](https://boards.straightdope.com/u/Ell)\
**Post date:** [March 23, 2001, 3:51am UTC](https://boards.straightdope.com/t/c-question/58836/10 "2001-03-23T03:51:01Z")

</div>

Thanks a lot guys!

Got it working! Another A on the way 🙂 (I hope!)

p.s. I was using VIDE on Windows NT (stupid cheap arse uni).  
p.p.s **zev** , I’m only getting to the power of integers.
