S Connor wrote:hi, I used an int to represent the rate of interest, which is 5%
to find 105% of a given value I used an int 105.
To find the following years interest I thought would be 105*105 and the following year 105*105*105 etc. This soon became too long for an int to hold.
It sure would!
Think about the display on a calculator:
It is made up of so many digits, right? One place to start might be to observe that each individual digit is an integer. From there, you could have an array, or a class, that used individual integer digits to do what a calculator display does.
But, that's probably more complicated than it has to be... In that picture, the display is showing a number that has a "whole" part (87), and a "decimal" part (93). The whole part could be represented by a single integer, couldn't it? Now, what about the decimal part? Well, it could also be represented by an integer, but it's a little more complicated than when you use an integer to represent the whole part. Why? Because the whole part is just 87, but the decimal part is 93
somethings. 93 what? 93
hundredths. Now, two decimal places of accuracy may be enough for your problem, but it may not. Suppose you wanted
three decimal places. The number in the display would be "87.930," which we know is numerically equal to 87.93. But, if you represented the decimal part with 930, and still treated that as 930
hundredths, your result would, numerically, be 96.300, because you'd be combining 87 and 9.30 into the final value. So, to make it work, you have to remember that, if you want three decimal places of accuracy, 930 means 930
thousandths.
In this scheme, you start by deciding how many decimal places of accuracy you want, and treat your decimal part integer accordingly.
To do math, you will need routines that can add, subtract, multiply, and divide numbers represented this way. Adding and subtracting are pretty straightforward. To multiply, remember that, for any two numbers, X and Y, where X = A + B and Y = C + D, X * Y = A * C + A * D + B * C + B * D.
Division is messier, but leave that until after you have addition, subtraction, and multiplication working.
Hope that helps.