0

i have this code

#include<iostream>
#include<math.h>
using namespace std;
int main(){
    int a[10],h=0;
    a[1]=1;
    a[2]=0;
    a[3]=2;
    a[4]=5;
    for(int i=1; i<=4; i++){
        cout<<h<<" + "<<pow(a[i],2)<<" = ";
        h = h + ((int)pow(a[i],2));
        cout<<h<<"\n";
    }
    cout<<"h = "<<h;
}

and the result will be like this

0 + 1 = 1
1 + 0 = 1
1 + 4 = 5
5 + 25 = 29
h = 29

but when we change variable to double we would get the right answer. so, can someone explain to me why we get that answer wen we use int ?

Daniel Ong
  • 11
  • 2
  • @PaulMcKenzie How is there any uninitialized value? The array is defined from a[1] to a[4]. – Aplet123 May 15 '20 at 12:10
  • 1
    `5 + 25 = 29` really ? – malat May 15 '20 at 12:11
  • 1
    I bet the 25 is really 24. You should not use pow() with integers. – drescherjm May 15 '20 at 12:12
  • 6
    In general, this is episode #5451564 in [Floating Point Math is Broken](https://stackoverflow.com/questions/588004/is-floating-point-math-broken?rq=1). Probably `pow(5, 2)` returns something like `24.999999999999999` and when casting to `int` the decimal part is truncated. – Yksisarvinen May 15 '20 at 12:12
  • 3
    The fact that an experienced programmer like @PaulMcKenzie misread this means your code is idiosyncratic. Do base arrays at zero please. The issue is caused by floating point rounding, and your formatter being kind to you. Personally I consider an implementation of pow that doesn't give you an exact result for integral arguments to be defective. – Bathsheba May 15 '20 at 12:13
  • 1
    @malat Congratulations, you spotted what the question is – Guy Incognito May 15 '20 at 12:13
  • 1
    Change `pow(a[i],2)` to `(int)pow(a[i],2)` and then the output will be the same wrong in both spots. – Eljay May 15 '20 at 12:14
  • `when we change variable to double we would get the right answer` - Would you please post the source of the program with changed variables to `double` (which one did you change?) and what would the "right" answer be? – KamilCuk May 15 '20 at 12:16
  • You must be using a rather old or non-standard compiler. Most modern c++ compilers do the right thing for `pow(x,small_int)` thanks to decades of complaints from the numerical computing community. In particular, `pow(x,2)` is typically evaluated as `x*x` as opposed to something like `exp(2*log(x))`. 5.0*5.0 is exactly 25.0 with most computers, but `exp(2*log(5.0))` may or may not be exactly 25.0. Even though most compilers dp now do the right thing with `pow(x,2)`, it remains better to write `x*x` in c++ code. – David Hammen May 15 '20 at 13:28
  • @DavidHammen 5.0*5.0 is exactly 25.0 on all computers that adhere to IEEE754 – Bathsheba May 15 '20 at 14:46
  • @Bathsheba - Many but not all computers adhere to IEEE754. That said, it's hard to imagine a computer where 5.0*5.0 is not exactly 25.0. – David Hammen May 15 '20 at 15:21
  • @DavidHammen: Possible on a float type with a 4 bit exponent for example - perhaps working in a "small number" environment. In that case 5 * 5 would be 16. – Bathsheba May 15 '20 at 16:06
  • thx @Yksisarvinen for explanation – Daniel Ong May 15 '20 at 17:15

0 Answers0