本文发表在 rolia.net 枫下论坛Here is a program in ANSI Standard C:
double sum, a[20];
int i;
/*
* Some code to initialize array a with legitimate
* floating point values whose sum is less than "maximum double".
*/
sum = 0.0;
for (i = 0; i < 20; i++)
sum += a[i];
printf("%e\n", sum);
sum = 0.0;
i = 20;
while (i--)
sum += a[i];
printf("%e\n", sum);
Question:
Will the 2 "printed" values:
1) always be the same? if so, explain why.
2) always be different? if so, explain why.
3) sometimes be the same and sometimes different? if so, give an
example.
Please be as complete as possible.
-------------------------------
Here is some code written in C++. Can you find the error, and how would you
fix it without changing the interface?
class Widget
{
public:
Widget() { Init(); }
virtual ~Widget() { Destroy(); }
virtual void Init();
virtual void Destroy();
long GetNum() { return *pNumber; }
void SetNum(long num) { *pNumber = num; }
private:
long *pNumber;
};
class Bolt : public Widget;
{
public:
Bolt() { Init(); }
virtual ~Bolt() { Destroy(); }
virtual void Init();
virtual void Destroy();
long GetNum2() { return *pNumber2; }
void SetNum2(long num) { *pNumber2 = num; }
private:
long *pNumber2;
};
void Widget::Init()
{
pNumber = new long;
}
void Widget::Destroy()
{
delete pNumber;
}
Bolt::Init()
{
pNumber = new long;
pNumber2 = new long;
}
Bolt::Destroy()
{
delete pNumber2;
}更多精彩文章及讨论,请光临枫下论坛 rolia.net
double sum, a[20];
int i;
/*
* Some code to initialize array a with legitimate
* floating point values whose sum is less than "maximum double".
*/
sum = 0.0;
for (i = 0; i < 20; i++)
sum += a[i];
printf("%e\n", sum);
sum = 0.0;
i = 20;
while (i--)
sum += a[i];
printf("%e\n", sum);
Question:
Will the 2 "printed" values:
1) always be the same? if so, explain why.
2) always be different? if so, explain why.
3) sometimes be the same and sometimes different? if so, give an
example.
Please be as complete as possible.
-------------------------------
Here is some code written in C++. Can you find the error, and how would you
fix it without changing the interface?
class Widget
{
public:
Widget() { Init(); }
virtual ~Widget() { Destroy(); }
virtual void Init();
virtual void Destroy();
long GetNum() { return *pNumber; }
void SetNum(long num) { *pNumber = num; }
private:
long *pNumber;
};
class Bolt : public Widget;
{
public:
Bolt() { Init(); }
virtual ~Bolt() { Destroy(); }
virtual void Init();
virtual void Destroy();
long GetNum2() { return *pNumber2; }
void SetNum2(long num) { *pNumber2 = num; }
private:
long *pNumber2;
};
void Widget::Init()
{
pNumber = new long;
}
void Widget::Destroy()
{
delete pNumber;
}
Bolt::Init()
{
pNumber = new long;
pNumber2 = new long;
}
Bolt::Destroy()
{
delete pNumber2;
}更多精彩文章及讨论,请光临枫下论坛 rolia.net