I want to calculate a price for an element using the percentage of the current value. For example: An apple costs 4$ right now and for every apple you buy, the price increases by 7%. So the first apple would cost 4$, the next 4.28$ (price + 7% of 4$), the next 4.58$ (price + 7% of 4.28$) and so on. My script is working somehow, but as the numbers grow larger, i struggle to find the right data type for this. Since i have to round to the last 2 digits, i'm doing something like this:
int64_t c = 0;
double ergebnis = 0;
double komma1 = 0;
komma1 = komma1 + ((komma1 / 100) * 7);
c = komma1 * 100;
ergebnis = c / 100.0;
The problem is, that if i bought 50000 apples, the number would grow so large, that the result would just become a negative number. I'm using "Visual Studio 2017 Community Edition".
#include "stdafx.h"
#include "stdint.h"
#include "iostream"
#include "Windows.h"
using namespace std;
int main()
{
cout.precision(40);
int price = 4;
long double cost = 0;
long double b = 4;
int d = 0;
while (d != 50000)
{
cost = price + (price / 100 * 7);
price = cost * 100.00;
cost = price / 100.00;
d = d++;
}
cout << "Number is: " << cost << endl;
Sleep(5000);
return 0;
}
This pretty much describes my problem.
I already used google and found the idea to use "int64_t" as integer, but i think the real problem is the double here, right?
So, tl;dr:
- Any way to use pretty large numbers in C++?
Any way to solve my problem using another solution? like splitting the numbers up or something? Like:
if (number == 1.000.000) { million = million++; number = 4; }