finding the exact value of pi (a programming book problem.)

Started by
3 comments, last by Antheus 12 years, 3 months ago
I'm brushing up on C++ concepts and problem solving, not some problems I know how to do but this one has be stomped, since i haven't studied calculus yet (yes I know it's spelled wrong.) The problem in the book basically gave me the code to find it but, with one catch:
the code was in the wrong order and had a bug in it. now i wrote it out as best as i can but i don't think it's right because i don't get the value of pi per say. I get a one or sometimes a 4, 64, 256, ect.

here is the code:


void valueOfPi()
{
double Pi = 0.0;
int i;
int n;
cout << "Enter the value of n: ";
cin >> n;
cout << endl;

for(i = 0; i < n; i++)
{
if(i % 2 == 0)
{
Pi += (1 / (2 * i + 1));
}
else if(i % 2 == 1)
{
Pi -= (1 / (2 * i + 1));
}
else
{

Pi *= 4;
}
cout << showpoint << fixed << setprecision(i) << Pi << endl;
}
}


some stuff i added since it seemed some stuff was missing from the original.
Advertisement
Your expressions involving i are all using integer arithmetic. You need to change at least one of the operands to a double if you want double arithmetic. I.e. use 2.0 instead of 2 or cast something to double, etc.

1>c:\users\tim sweeny\documents\visual studio 2008\projects\x\irrapp\chapter5_programming_problems.h(348) : error C2296: '%' : illegal, left operand has type 'double'
1>c:\users\tim sweeny\documents\visual studio 2008\projects\x\irrapp\chapter5_programming_problems.h(348) : error C2297: '%' : illegal, right operand has type 'double'
if(i % static_cast<double>(2.0) == 0)
{
Pi += (1 / (2 * i + 1));
}
else
{
Pi -= (1 / (2 * i + 1));
}

Pi *= 3.0;


when i tried that, i got those errors on top. any ideas?
Read the error message:


[color=#000000]chapter5_programming_problems[color=#666600].[color=#000000]h[color=#666600]([color=#006666]348[color=#666600])[color=#000000] [color=#666600]:[color=#000000] error C2296[color=#666600]:[color=#000000] [color=#008800]'%'[color=#000000] [color=#666600]:[color=#000000] illegal[color=#666600],[color=#000000] left operand has type [color=#008800]'double'

[color=#008800]on line 348, the '%' is illegal because the left operand is type 'double'

[color=#008800]- in other words, you can't use modulus with double, it's an integer-only operator
Don't change i and n, at least not to solve original problem.

When writing mathematical expressions, it is customary to indicate that floating point types are involved. Or:
Pi += (1.0 / (2 * i + 1));
// or
Pi += (1.0f / (2 * i + 1));
Alternatively:
Pi += (1 / (2.0 * i + 1));
// or
Pi += (1 / (2 * i + 1.0));


It's not really arbitrary, there are strict rules on how types get promoted, but above serves as a hint to programmer to show that floating point and integer math are getting mixed.



Floating point type promotion is just one of those annoying things which has caused many bugs. Strangely enough, Java has exactly the same kind of problem.

This topic is closed to new replies.

Advertisement