l'erreur C2664 [débutant]
L'exemple suivant génère l'erreur C2664 :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| #include <cmath> // defines pow() and log()
#include <iostream> // defines cin and cout
#include <iomanip> // defines setw()
using namespace std;
int main()
{ long n;
cout << "Enter a positive integer: ";
cin >> n;
int d=0; // the discrete binary logarithm of n
double p2d=1; // = 2^d
for (int i=n; i > 1; i /= 2, d++)
{ // INVARIANT: 2^d <= n/i < 2*2^d
p2d=pow(2,d); // = 2^d
cout << setw(2) << p2d << " <= " << setw(2) << n/i
<< " < " << setw(2) << 2*p2d << endl;
}
p2d=pow(2,d); // = 2^d
cout << setw(2) << p2d << " <= " << setw(2) << n
<< " < " << setw(2) << 2*p2d << endl;
cout << " The discrete binary logarithm of " << n
<< " is " << d << endl;
double lgn = log(n)/log(2); // base 2 logarithm of n
cout << "The continuous binary logarithm of " << n
<< " is " << lgn << endl;
} |
alors j'ai fait les modifications suivantes:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| #include <cmath>
#include <iostream>
#include <iomanip>
using namespace std;
int main ()
{
double n;
cout << "enter a positive integer: ";
cin >> n;
double m = 2;
int d = 0;
double p2d = 1;
for (long i = n; i > 1; i /= 2, d++)
{
int q= n/i;
p2d=pow(m,d);
cout << setw(2) << p2d << " <= " << setw(2) << q << " < " << setw(2) << 2 * p2d << endl;
}
p2d=pow(m,d);
cout << setw(2) << p2d << " <= " << setw(2) << n << " < " << setw(2) << 2 * p2d << endl;
cout << "The discrete binary logarithm of " << n << " is " << d << endl;
long double lgn = log (n) / log (m);
cout << "The continuous binary logarithm of " << n << " is " << lgn << endl;
return 0;
} |
Code:
1 2 3 4 5 6 7 8 9 10
| enter a positive integer: 63
1 <= 1 < 2
2 <= 2 < 4
4 <= 4 < 8
8 <= 9 < 16
16 <= 21 < 32
32 <= 63 < 64
The discrete binary logarithm of 63 is 5
The continuous binary logarithm of 63 is 5.97728
Appuyez sur une touche pour continuer... |
Est ce que c'est correcte?
merci