TopCoder problem "SignificanceArithmetic" used in SRM 362 (Division II Level Three)



Problem Statement

    Significance arithmetic is a set of rules used in scientific calculations, and is used extensively in scientific application software. Specifically, significance arithmetic is used to determine the correct number of significant digits, or measured digits, in a calculation. The significant digits in a number are all its digits except for leading zeros that are two or more places to the left of the decimal point. For instance, 00.30, 0.00 and 0.60 each contain three significant digits, while 5 and 00 each only contain one significant digit.



We need to compute the result of the addition of two numbers. The result must be rounded to n significant digits, where n is determined as follows:

First determine the number with the least number of digits to the right of the decimal point (this may be zero). Then the result will not contain any significant digits to the right of this digit, and all other digits are significant (except leading zeros that are two or more places to the left of the decimal point). For example, the result of 1.56 + 1236.1 will have only one significant digit immediately to the right of the decimal point, but all digits to the left of the decimal point will be significant.



There are several cases to consider when rounding a number to n significant digits:

1) If the number has less than n significant digits, the number is padded with trailing zeros. For example, 2.5 rounded to 3 significant digits is 2.50.

2) If the digit immediately to the right of the nth digit is greater than 5, the number is rounded up. For example, 2.56 rounded to 2 significant digits is 2.6.

3) If the digit immediately to the right of the nth digit is less than 5, the number is rounded down. For example, 2.54 rounded to 2 significant digits is 2.5.

4) If the digit immediately to the right of the nth digit is 5 and there are non-zero digits after the 5, the number is rounded up. For example, 2.551 rounded to 2 significant digits is 2.6.

5) If the digit immediately to the right of the nth digit is 5 and there are no subsequent non-zero digits, then the number is rounded whichever way leaves the nth digit even. For example, 2.55 rounded to 2 significant digits is 2.6, but 2.45 rounded to 2 significant digits is 2.4.



You will be given an expression as a String that will contain two numbers that are separated by a '+'. Each number will contain a non-empty integral part, as well as an optional fractional part which, if present, will be preceded by a decimal point. The numbers may contain leading zeros, and will always be positive. Examples of valid numbers are 0.456, 004.0, and 0. However, .025 is invalid because it has no integral part, and 45. is invalid because a fractional part must follow the decimal point.



Your result should be in the same format as described above. However, there should only be a leading zero if the integral part of the number is 0. For example, 0.00450 is a valid result, while 0459.2 is not.
 

Definition

    
Class:SignificanceArithmetic
Method:evaluate
Parameters:String
Returns:String
Method signature:String evaluate(String expression)
(be sure your method is public)
    
 

Notes

-Numbers in expression may contain leading zeros.
 

Constraints

-expression will contain two numbers that are separated by a '+'.
-Each number in expression will be a real number containing between 1 and 6 digits, inclusive (not including the decimal point).
-Each number in expression will be formatted exactly as described in the problem statement.
 

Examples

0)
    
"2+2"
Returns: "4"
Two has one significant digit, so the result will as well.
1)
    
"1.234+0.000"
Returns: "1.234"
All digits here are significant.
2)
    
"1.234+0.006"
Returns: "1.240"
Again, all digits here are significant. Therefore the result will contain the same number of significant digits, and rounding rule #1 is used.
3)
    
"12.57+2.6"
Returns: "15.2"
12.45 + 2.6 = 15.17. Rounding rule #2 is used.
4)
    
"5.50005+0"
Returns: "6"
The only significant digit here is the one immediately to the left of the decimal point, and rounding rule #4 is used.
5)
    
"2.5+2"
Returns: "4"
Again, the only significant digit here is the one immediately to the left of the decimal point. Here, rounding rule #5 is used.
6)
    
"0.00+000"
Returns: "0"
7)
    
"983.17+76.8"
Returns: "1060.0"

Problem url:

http://www.topcoder.com/stat?c=problem_statement&pm=8056

Problem stats url:

http://www.topcoder.com/tc?module=ProblemDetail&rd=10775&pm=8056

Writer:

eleusive

Testers:

PabloGilberto , Olexiy , lovro , ivan_metelsky

Problem categories:

Search, Simple Math, Simulation