Wrox Programmer Forums
|
C++ Programming General discussions for the C++ language. For questions specific to Microsoft's Visual C++ variant, see the Visual C++ forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C++ Programming section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old April 30th, 2011, 03:55 PM
Registered User
 
Join Date: Apr 2011
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Exact math

Hello -
I am trying to make a program that will calculate some flat sheet metal layouts for me, and am running into trouble because the "double" numbers I am using keep going to the wrong number of decimals. I need to have them exactly what I need or I will not be able to add by 1/10000 of an inch and get good answers. I have Visual Studio 2008 and am working in (learning) C++.
I took the number I need (as a double) and multiplied it be 10000. Then called that an "int" and sent it back to the calling program. Re-typed it as a double here and divided by 10000 to have something with exactly 4 decimals, but no luck.
What else can I do? I have looked on google and can't find help that I can understand.

Any help will be gratefully appreciated.
Thanks,
SmokyRick

Here is the code of my program:

Code:
// tryout4pl_dec.cpp  RLC 2011-04-08
// trying to get a true 4 place decimal from a one with more or less digits.

#include <iostream>
#include <cmath>
using std::cin;  using std::cout;  using std::endl;

// declare functions
double dec_pl(double nmbr, int pwr);

// declare variables
double nmbr = 0.0;
double pwr = 1;
double returned = 0.0;

// here is where the work is done
double dec_pl(double nmbr, double pwr)
{
	double number1 = 0.0;
	int number2 = 0;
	double number3 = 0.0;
	double power = pow(10, pwr);
	number1 = nmbr * power;
	cout << "number1 = nmbr * power = " << number1 << endl;
	number2 = (static_cast<int>(number1));
	cout << "number2 = (static_cast<int>(number1)) = " << number2 << endl;
	number3 = (static_cast<double>(number2)) / power;
	cout << "number3 = (static_cast<double>(number2)) / power = " << number3 << endl;
	return number3;
}

// and here is main()
int main()
{
	cout << "Enter a number to be converted: ";
	cin >> nmbr;
	cout << "\nEnter a number of decimal places to convert to: ";
	cin >> pwr;
	returned = dec_pl(nmbr, pwr);
	cout << endl << returned << endl << endl;
	return 0;
}

Last edited by SmokyRick; April 30th, 2011 at 04:34 PM.. Reason: to add in the code from my program
Reply With Quote
  #2 (permalink)  
Old September 19th, 2011, 04:28 PM
MtD MtD is offline
Registered User
 
Join Date: Sep 2011
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

You're using flotating-point double-precision numbers, those cannot represent all the real (not even all the rational) numbers exactly. You might have better luck with Boost Rational Number Library (header-only, no need to build separately):

http://www.boost.org/doc/libs/release/libs/rational/

Example program:
http://www.boost.org/doc/libs/1_37_0...al_example.cpp

The above, sample run:
http://www.ideone.com/cVA7X
Reply With Quote





Similar Threads
Thread Thread Starter Forum Replies Last Post
How to find the exact PHP template I need? Mario2027 PHP How-To 0 December 8th, 2010 05:08 AM
Exact year month and days mankoti_mankoti2000 Access 1 July 3rd, 2008 10:05 AM
STRING EXACT MATCH ricespn Beginning VB 6 6 November 12th, 2007 01:18 PM
exact mb we can upload??.. asudhakar C# 1 April 4th, 2007 07:31 AM
Exact Match problem demiwolf XSLT 1 February 14th, 2004 10:28 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.