Solved

Division Precision

Posted on 2003-11-10
10
837 Views
Last Modified: 2010-04-01
Im writing a progarm to calculate PI and my problem is c++ rounds everything in division so i have an endless answer of "3" ive done seprecsision for cout statement and that is fine but i need to fix the division my equation is PI = 4-4/3+4/5-4/7+4/9-4/11+4/13-4/15...

Here is my actual math part of my script:

  Cal = 4/Odd;
  Odd += 2;
  Calc -= Cal;
  Cal = 4/Odd;
  Odd += 2;
  Calc += Cal;
  Pi = Calc;

This is in a for loop then it saves and repeats
0
Comment
Question by:Deathbob
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
  • +2
10 Comments
 
LVL 17

Expert Comment

by:rstaveley
ID: 9719892
C++ treats 4 as an integer.

Write 4.0 to get a division on a double.

e.g.

     Cal = 4.0/Odd;

Make sure all of your variebles are doubles for double precision.
0
 
LVL 5

Assisted Solution

by:waelothman
waelothman earned 65 total points
ID: 9719944
dividing integer with integer always give an integer value so you have to do 1 of 3 ways

as rstaveley say make any number of them float

or write it
cal = (float) 4/Odd
0
 
LVL 1

Author Comment

by:Deathbob
ID: 9720376
As i counted i think i can get 52 decimal places give or take, but can i get more?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:Deathbob
ID: 9720379
THANKS FOR YOUR HELP BY THE WAY, BUT IM GONNA TRY AND PUSH IT TO THE MAX
0
 
LVL 13

Expert Comment

by:SteH
ID: 9720762
Yes,
1) go to extended instead of double. This increases the number of bits used for the mantissa.

Then you have the problem that the mantissa is limited and the value won't change the exponent of the number 3.1415e0: At some place the number calc + 4/2n+1 can't be distinguished from calc + 4/2n+1 - 4/2n+3 with the chosen precision of your var.  To reach a higher precision you have to remove the leading. Now it helps to remove some known digits of pi and so changing the exponent of it. Thereby you increase the available bits for the mantissa to distinguish these numbers.
0
 
LVL 32

Expert Comment

by:jhance
ID: 9721385
>>BUT IM GONNA TRY AND PUSH IT TO THE MAX

I hate to be the bearer of bad news here but you're not going to live long enough to push this to the max.  PI is a transcendental irrational number and has an infinite number of digits.  Using computers PI has been calculated to many millions of digits but you're unlikely to even approach that using your methods.
0
 
LVL 1

Author Comment

by:Deathbob
ID: 9723665
Its for fun and becides just the thought of haveing a program that can attempt this is nice (sence of acomplishment).
0
 
LVL 32

Expert Comment

by:jhance
ID: 9723706
Well take at look at this:

http://projectpi.sourceforge.net/

If you're going to try and accomplish something, at least use a technique that is potentially usefule.
0
 
LVL 1

Author Comment

by:Deathbob
ID: 9723729
how do you define an extended variable?

Here is my script

//Pi Calculation Software Alpha

//START ODD VALUE : 3
//START CALC VALUE : 4
//:Placed in files:

#include <iostream.h>
#include <fstream.h>
#include <stdlib.h>
#include <math.h>
#include <iomanip>


char Buffer[100];
int Odd;
int Oddc;
double Cal;
double Calc;
double Pi;
int i;
int pre = 52;

void LoadFiles();
void DoIt();
void ShowIt();
void SaveIt();

void main ()
{
 LoadFiles();
 system("pause");
 DoIt();
}

void LoadFiles()
{
 ifstream ODDB ("Odd.txt");
 ODDB.getline(Buffer,100);
 Odd = atoi(Buffer);
 ODDB.close();

 ifstream CalcB ("Calc.txt");
 CalcB.getline(Buffer,100);
 Calc = atof(Buffer);
 CalcB.close();

}

void DoIt()
{
//Calculates and saves every 10000 calculations
 for (i = 0; i <= 5000; i++)
 {
  Cal = (float) 4/Odd;
  Odd += 2;
  Calc -= Cal;
  Cal = (float) 4/Odd;
  Odd += 2;
  Calc += Cal;
  Pi = Calc;
  ShowIt();

 }

 SaveIt();

 DoIt();

}

void ShowIt()
{

  cout << "Calc: ";
  cout << Odd/2-1;
  cout << " PI: ";
  cout << setprecision(pre) << Pi;
  cout << "\n";


}

void SaveIt()
{


 ofstream PiC ("Pi.txt");
 PiC << setprecision(pre) << Pi;
 PiC.close();


 ofstream ODDC ("Odd.txt");
 ODDC << Odd;
 ODDC.close();


 ofstream CalcC ("Calc.txt");
 CalcC << setprecision(pre) << Calc;
 CalcC.close();

}
0
 
LVL 13

Accepted Solution

by:
SteH earned 60 total points
ID: 9729349
If you arre referring to my answer:

long double      is a 80 bit floating point number

double is a 64 bit float,
and
float is 32 bit float.

In your above example you mix new and old style STL headers:
#include <iostream.h> // old style
#include <fstream.h>   // old style
#include <iomanip>      // new style
This can be dangerous. The preferred way should be only the new style (without the .h)
#include <iostream>
#include <fstream>
#include <iomanip>

0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question