Problem 35 incorrect answer

Back to General discussions forum

stephenr512     2015-11-03 00:02:09

I came across something a little strange while answering Problem 35: Savings Calculator.

One of my answers showed up as incorrect.
Starting amount: 25
Required sum: 450
Interest rate: 2%

My program gave the result as 146 years, but the required result was shown as 147 years. I checked my code, but couldn't see a problem so I checked an online savings calculator site, which agreed my result of 146 years. As a test, a resubmitted my code without any changes, but with a new set of data, and this time it went through as correct. Obviously not a major problem, but just thought I'd let you know.

Rodion (admin)     2015-11-03 04:17:18
User avatar

Stephen, Hi! Thanks for your message!

I think this little discrepancy is because your solution does not perform this operation:

>At the end of each year ... rounded down to whole cents (as in example above).

As we see it only becomes important when the term is long and percents are small...

stephenr512     2015-11-03 09:41:05

Aargh! "Read the question!" - gets me everytime!

Thanks for that, Rodion. Rounding down to whole cents does indeed give 147 years. I went back and submitted a corrected solution.

jokubolakis     2015-11-03 17:52:45

I have a question about this same problem. My solution sometimes gives the right answers and sometimes makes a mistake of one year. Should I be concerned and try to make it better or could it be just an error of rounding or something?

P.S. The errors happen very rarelly.

Quandray     2015-11-03 18:35:08
User avatar

Hi,

Does your code round down to whole cents? This bit looks like it may round up

if ((int)temp%10 >= 5) {
    temp += 10;
}
jokubolakis     2015-11-04 13:29:38

Oh wow. As English is not my main language I wouldn't have noticed the word down. I fixed my code, Thank You.

uso2015     2016-01-28 16:34:16

Hi! I got the corrected solution. however, when I run the example, the last two lines are 29 9316.79 30 10062.13 while your answer is 29 9316.82 30 10062.16 the other are same.

Then I find the problem(int(5033.61 * 100) = 503360), how I solve the problem?
Quandray     2016-01-29 08:27:56
User avatar

Sorry, I can't help you with python, but if

int(5033.61 * 100) = 503360

What is

5033.61 * 100
Matthew Cole     2016-01-29 22:50:39

From the Python2 documentation:

int(x=0): 

Return an integer object constructed from a number or string x, or return 0 if no arguments are given. If x is a number, it can be a plain integer, a long integer, or a floating point number. If x is floating point, the conversion truncates towards zero. If the argument is outside the integer range, the function returns a long object instead.

int(x, base=10): 

If x is not a number or if base is given, then x must be a string or Unicode object representing an integer literal in radix base. Optionally, the literal can be preceded by + or - (with no space in between) and surrounded by whitespace. A base-n literal consists of the digits 0 to n-1, with a to z (or A to Z) having values 10 to 35. The default base is 10. The allowed values are 0 and 2-36. Base-2, -8, and -16 literals can be optionally prefixed with 0b/0B, 0o/0O/0, or 0x/0X, as with integer literals in code. Base 0 means to interpret the string exactly as an integer literal, so that the actual base is 2, 8, 10, or 16.

Now, as for the question of whether int(5033.61 * 100) = 503360, Python 2.7.2 does not agree with this assertion. In fact, it creates an integer object of value 503361.

Furthermore, 5033.61 * 100 returns a value of 503361.00000000006. (Why is this happening?)


So, to summarize, for all Python versions (through 2.7.11 and 3.5):

  • If you want an integral value, and precision/rounding rules aren't important to you, or just want naive truncation, or are trying to get an integer out of a non-base 10 value or have an object which represents an integer but isn't an int ... go ahead and use the int() constructor to truncate.
  • If you want a non-integral value, and precision/rounding rules aren't important to you, but you want to control the number of decimal places in the actual value, use the round() built-in functions.
  • If you want a non-integral value, and precision/rounding rules are very important (see the #6: Rounding task), use the decimal module, which has a Decimal.quantize() method.
Please login and solve 5 problems to be able to post at forum