fork(1) download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. // A modified version of Lightness Races in Orbit's code given here: http://stackoverflow.com/a/37861479/3555000
  4. // I thank Lightness Races in Orbit for the contribution
  5. void print_binary(long double y)
  6. {
  7. const long double x = y;
  8. unsigned char a[sizeof(long double)];
  9.  
  10. copy(
  11. reinterpret_cast<const unsigned char*>(&x),
  12. reinterpret_cast<const unsigned char*>(&x) + sizeof(long double),
  13. &a[0]
  14. );
  15. for (auto el : a)
  16. {
  17. bitset<8>k(el);
  18. cout << k.to_string() ;
  19. }
  20.  
  21. cout << endl;
  22. }
  23.  
  24. int main()
  25. {
  26. int a[] = {20,29,31}, res=0;
  27. for(int i = 0; i<3; i++)
  28. {
  29. cout<<"i = "<<i<< ", a["<<i<< "] = "<<a[i]<<"\npow(" << a[i] <<","<<i+1 << "):\nBinary: ";
  30. long double temp = pow(a[i],i+1);
  31. print_binary(temp);
  32. res+=temp;
  33. cout<<setprecision(50)<<fixed<< "Decimal: " <<temp <<", Result = "<<res<<endl;
  34. }
  35. return 0;
  36. }
  37.  
Success #stdin #stdout 0s 3416KB
stdin
Standard input is empty
stdout
i = 0, a[0] = 20
pow(20,1):
Binary: 000000000000000000000000000000000000000000000000000000001010000000000011010000000000000000000000
Decimal: 20.00000000000000000000000000000000000000000000000000, Result = 20
i = 1, a[1] = 29
pow(29,2):
Binary: 000000000000000000000000000000000000000000000000010000001101001000001000010000000000000000000000
Decimal: 841.00000000000000000000000000000000000000000000000000, Result = 861
i = 2, a[2] = 31
pow(31,3):
Binary: 000000000000000000000000000000000000000000000000101111101110100000001101010000000000000000000000
Decimal: 29791.00000000000000000000000000000000000000000000000000, Result = 30652