Round Up/Down Function? I'm looking to build a program that solves basic equations?

I know a little bit of Objective-C and I'm not too familiar with the differences between C++ and Obj. C I'm looking for a solution to a business need that I have. I'm looking to create a C++ program on my Windows computer to solve a few simple equations, one after the other. I don't know where to start or if using Python would be better suited for this problem.

Problem: I want to be able to open up the application on my computer and then be prompted to enter a value for 2 different variables (these values will be called x and p). I then need the program to run through a set of equations (6 in total). I then need the program to return the value of 3 different variables (we'll call them: c, g and h). A big issue I'm finding, is that I need to round up/down a couple of variables for the other equations to work, yet I don't know what function I need to use to make this happen. Here is a breakdown of how it should go:

Prompt-
Please enter the Buy Price:

1. 1000/x = t
2. t (rounded to the nearest one) = c (this value to display as the result)
3. c(x) = f
4. (p + f) / c = k
5. k (rounded to the nearest hundredth) = g (this value to display as the result)
6. g(c) - f = h (this value to display as the result)

--Translated----

Prompt-
Please enter the Buy Price: 30

1. 1,000/30 = 33.33333333333333
2. 33.33333333333333 = 33
3. 33(30) = 990
4. (140 + 990) / 33 = k
(1,130) / 33 = 34.24242424242424
5. 34.24242424242424 = 34.24
6. 34.24(33) - 990 = 139.92

Result:

Contracts: 33
Sell Price: $34.24
Gross Profit: $139.92

--------------------------------------------------


Has anyone ever worked with a website or program builder that can easily go through these set of equations? Or can anyone tell me how to use the round up/down function in C++? Also how would I go about carrying the value of a variable over to the next equation in order to solve it?

Much appreciated!
Last edited on
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
#include <iostream>

double round(double x){
    if (x >= 0)
        return floor(x + 0.5);
    return ceil(x - 0.5);
}

struct result{
    double c, g, h;
};

result evaluate(double x, double p){
    result ret;
    double t = 1000.0 / x;
    ret.c = round(t);
    double f = ret.c * t;
    double k = (p + f) / ret.c;
    ret.g = round(k * 100.0) * 0.01;
    ret.h = ret.g * ret.c - f;
    return ret;
}

int main(){
	double x, p;
    std::cout << "Enter x:\n";
	std::cin >> x;
	std::cout << "Enter p:\n";
	std::cin >> p;
	auto r = evaluate(x, p);
	std::cout <<
		"c: " << r.c << "\n"
		"g: " << r.g << "\n"
		"h: " << r.h << "\n";
}
Note that the above code may not be suitable for financial applications if very high precision is required.
if working with $USA I recommend working as if 1.0 = 1 penny, not 1 dollar. Then you may choose to use integers in some places, avoiding some issues (but unable to handle fractional cents, of course).

second, watch rounding too much. If you round stuff, then compute, then round again, then compute something else, after a while your rounding accumulates and creates error. keeping the full doubles as long as possible before rounding, or rounding only the result before display, may be better depending on what is needed.


Assuming that we are using a standard library implementation which supports our locale
(ie. libraries other than the GNU library on non-linux platforms):

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
#include <iostream>
#include <locale>
#include <iomanip>
#include <cmath>

int main()
{
    // set the appropriate locale for stdin, stdout
    // in this example, we use en_US.UTF-8 / en-US
    // note: locale names are implementation specific
    std::locale en_us( "en_US.UTF-8" ) ;
    std::cin.imbue(en_us) ;
    std::cout.imbue(en_us) ;

    // get the monetary values p and x
    // since the locale is en_US.UTF-8, the monetary unit is a cent
    // note: using the lowest denomination monetary unit for all our 
    // computations minimises/eliminates rounding errors
    // ie. input of $123.45 or USD 123.45 or just 123.45 is returned as 12345
    // http://en.cppreference.com/w/cpp/io/manip/get_money
    long double p, x ;
    std::cout << "enter p and x: " ;
    std::cin >> std::get_money(p) >> std::get_money(x) ;

    // to print out monetary values, we use std::put_money
    // http://en.cppreference.com/w/cpp/io/manip/put_money
    std::cout << std::showbase ; // show the currency symbol on output
    std::cout << "\np: " << std::put_money(p) 
              << "\nx: " << std::put_money(x) << "\n\n" ;

    // compute the integer value p/x, rounded to the nearest integer
    // http://en.cppreference.com/w/cpp/numeric/math/round
    const long long c = std::llround(p/x) ;
    std::cout << "  #contracts: " << c << '\n' ;

    const long double f = c * x ; // compute f (in cents)
    const long double delta = 14000 ; // $140.00 ie. 14000 cents
    // compute k (to the nearest cent) using std::llround
    const long double k = std::llround( (f+delta) / c ) ; 
    std::cout << "  sell price: " << std::setw(8) << std::put_money(k) << '\n' ;
    
    // calculate gross profit (in cents) and print out the amount
    const long double g = std::llround( k*c - f ) ;
    std::cout << "gross profit: " << std::setw(8) << std::put_money(g) << '\n' ;
}

http://coliru.stacked-crooked.com/a/eb07aff4c4c6620b
http://rextester.com/LAD67991
Topic archived. No new replies allowed.