-1

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; }

Pascal Cuoq
  • 79,187
  • 7
  • 161
  • 281
Couchuser
  • 9
  • 4
  • Try to use `long double`, it has a pretty high precision. Also, you might want to look at a third party library like Boost for very high numbers. – Rakete1111 Sep 24 '17 at 13:50
  • 3
    "The obvious problem is here" What obvious problem? Don't see any. "-19974798308344 or something" There's no "or something" in the set of integers. Please post a [mcve]. This should include a buildable runnable program, any input it requires, desired output, and actual output. – n. m. could be an AI Sep 24 '17 at 14:01
  • `price = cost * 100.00; cost = price / 100.00;` This makes no sense. What should this fragment of code do? Why your `price` is an integer? `d = d++` doesn't do what you think it does. Use a `for` loop. – n. m. could be an AI Sep 24 '17 at 14:42
  • yeah, gsamaras already pointed that out. I'm just doing some terribly stupid mistakes here... Well, the `cost*100` part should round the numbers, but i think i can just use "roundl", right? – Couchuser Sep 24 '17 at 14:56
  • Hello, please do not use greetings or thanks. Questions are hoped to be useful long after the day you are asking them, and at night, so you don't need to wish the reader a good day. – Pascal Cuoq Sep 24 '17 at 20:14
  • Read about [arbitrary precision arithmetic](https://en.wikipedia.org/wiki/Arbitrary-precision_arithmetic) then use [GMPlib](http://gmplib.org/) – Basile Starynkevitch Sep 24 '17 at 20:18

3 Answers3

1

In your edited code:

First, this:

d = d++;

may be undefined (you should get a compilation warning), like this:

warning: operation on 'd' may be undefined [-Wsequence-point]
         d = d++;
         ~~^~~~~

Replace this line of code with:

d++;

which is equivalent to:

d = d + 1;

price is an int. You then perform integer division price / 100, which will give an int as its result! So 4/100 in an integer division gives 0. Cast one operand to a floating point value, to get a division that will yield a floating point number as a result. E.g. (long double)price / 100.

However, what you describe as your algorithm doesn't match your expected results. You say that you want to charge from 4$, to 4.28$, then to 4.58$. The formula to achieve this is not the one you describe, but this:

cost = cost + cost * 7%

Moreover, these two lines of code make no sense:

price = cost * 100.00;
cost = price / 100.00;

just discard them.

Furthermore, since you know how many times you want the iteration to execute, use a for loop, instead of a while loop.

Putting everything together, you get:

 #include "iostream"

using namespace std;

int main()
{
    cout.precision(40);
    long double cost = 4;

    for(int i = 0; i < 50000; ++i)
    {
        cost = cost + ((long double)cost / 100 * 7);
    }
    cout << "Cost is: " << cost << endl;
    return 0;
}
gsamaras
  • 71,951
  • 46
  • 188
  • 305
  • I just noticed something: on another forum i read something about divison by 100 and multiplicate that again and stuff... but can't i just use "roundl" from math.h? I just googled that, but i dont quite understand, how to define the numbers after the decimal point. And thanks for the clarification with d++. I always wondered what that warning meant. Seems like VS just wanted to tell me to stop being stupid :P – Couchuser Sep 24 '17 at 14:54
  • Found the post: [link](https://stackoverflow.com/questions/1343890/rounding-number-to-2-decimal-places-in-c) – Couchuser Sep 24 '17 at 15:00
  • Well, i'm not getting the number you got, i got "inf". I'm using the exact code you posted up there. But... why? – Couchuser Sep 24 '17 at 15:09
  • Because the compuer you use has smaller limits than mine's. Try it on wandbox.org and you will see! – gsamaras Sep 24 '17 at 15:13
  • whew, good to know. Thank you for this much help! i really appreciate that. And sorry for asking such foolish questions. Being a beginner sucks.... :D – Couchuser Sep 24 '17 at 15:21
  • @Couchuser don't be so harsh on yourself. Everyone was a beginner at some point of time! ;) For future questions, try to post a minimal example, like the one you edited in your question now, from the start! Glad I helped, cheers! – gsamaras Sep 24 '17 at 15:23
1

Your attempt to calculate compound interest went horribly wrong. The program doesn't do anything remotely similar to what your description says it should do.

Here's how your loop should look like:

 long double price = 4.0;
 for (int i = 0; i < 50000; ++i)
 {
    price = price * 1.07; // == (100.0 + 7.0)/100.0
 }

The regular double doesn't have a range to represent your price, but long double is OK. If you want to go bigger, you need a third party large numbers library. Note that the nearest integer to your answer has ~1500 decimal digits, so calculations with such numbers can be rather slow.

The same result can be obtained without looping:

  std::cout "The number is " << 4 * std::pow (1.07L, 50000) << "\n";
n. m. could be an AI
  • 112,515
  • 14
  • 128
  • 243
0

Try long double instead of double. Difference between long double and double in C and C++.

Beware of the floating point precision and for conversion when performing calculations with variables that are sensitive to precision.

Any way to use pretty large numbers in C++?

Use a library, like Gnu Multiple Precision (GMP):

"a free library for arbitrary precision arithmetic, operating on signed integers, rational numbers, and floating-point numbers. There is no practical limit to the precision except the ones implied by the available memory in the machine GMP runs on. GMP has a rich set of functions, and the functions have a regular interface."

However, these libraries are for advanced usage. Make sure you really need to use that large numbers (large numbers are time consuming when being processed).

gsamaras
  • 71,951
  • 46
  • 188
  • 305
  • Thank you for this answer! It seems that the **long double** is actually working a lot better, but i'm still getting a negative number. I added an example above, maybe this will clear things up :-) – Couchuser Sep 24 '17 at 14:30