Can not get the write answer to populate

So I need to find out why in that the Variable profitSell will not calculate to match a standard calculator.
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
  /***************************************************
Program Stock Market 

This program <Is designed to calculate the cost of stock market shares.>

Author: <Eric Goldberg>
Created: <5/18/17>
Revisions: <date> <what changes were made>
***************************************************/


#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;
int main()
{

	//Variables
	int Shares = 600;
	
	double CostShare = 21.77,
		   Sell = 2,
		   BrokerFee = .02,
		   TotalCostBroker,
		   TotalBroker,
		   TotalCost,
		   Total,
		   ProfitSell;
		   

	// Calculate variables 
	TotalCost = Shares*CostShare;
	TotalBroker = TotalCost * BrokerFee; 
	Total= TotalBroker + TotalCost;
	ProfitSell= Sell / TotalCost;

	//Display Data to user.
	

	cout << "The cost of the stocks with out Broker fee:	$" << TotalCost << endl;
	cout << "The cost of Broker Fees:	$" << TotalBroker << endl;
	cout << "The Total Cost is:		$" << Total << endl; 
	
	//Turn a profit

	cout << "TO make a profit you will need to sell each stock for:		$" << ProfitSell << endl;
	



	
	return 0;
}
I believe the answer you're looking for is std::setprecision(int);.
Or is it perhaps floating point round off/precision errors?
See this link:
https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html


Well, the final answer *is* right, its just that it's rounded because the precision is set lower than a standard calculator, i think. There doesn't appear to be any truncation rounding errors going on in his math. Though, I only took a brief look at the compilation output.
Actually the values differ from my calculator:
0.000150113636022
0.000153116 // From the program.

Which is being caused by floating point errors. If it was just being caused by rounding the last number from the program should be either a 2 or a 4 but not a 6.

Last edited on
Huh, that's funny.

On my calculator, I get
1.5311590874291838922064002449855e-4
which expands to
0.00015311590874291838922064002449855
which rounds to
0.000153116
which matches the output I get from his program which is
$0.000153116
.

On my calculator, I'm taking 21.77 * 600 = 13062.
Then dividing 2 by that. 2/13062 = 0.000153116 (rounded).

Perhaps we're doing something different?
Your broker works for free, Aaron.
@jlb's takes his 2% cut.
Therein lies the difference. Who is right depends on the broker. I think the original program should divide by Total in line 37 (giving @jlb's answer) or what it does at present (which gives yours).

It's not the 6th sig fig that is the biggest difference ... it's the 0 or 3 after the ...15...
Last edited on
I'm confused. Haha. Where are you getting the 2% cut in ProfitSale?

CostShare = 21.77
Shares = 600;
Sell = 2;
TotalCost = Shares*CostShare;
ProfitSell= Sell / TotalCost;

TotalCost = 600 * 21.77 = 13062;
ProfitSell = 2/13062 = 1.5311590874291838922064002449855e-4

Program output $0.000153116
Your calculation is correct for the program as it stands Aaron.

If you were to change
ProfitSell= Sell / TotalCost;
to
ProfitSell= Sell / Total;
you would get @jlb's answer. Try dividing your answer by 1.02.

That is all. I think the original program doesn't reflect real life. Stockbrokers are never poor ...
Last edited on
Ahh!~ Ok, I thought I was going crazy.
Topic archived. No new replies allowed.