Problem 371. Decimal Comparison
Background
A utility of particular interest to Cody and other MATLAB ventures is comparing the equality of two numbers. In computing, numbers can be stored in a number of ways, but they all share the trait of being stored as a binary number. As such, the equals operator in MATLAB is checking the bits of one stored number against the bits of another stored number. In this problem, I ask that we move away from this binary representation and comparison of numbers back into the decimal domain.
Problem
Write a function that takes a character input that represents a number that we wish to compare against, n1, and a double input that represents the other number, n2. Output true if the numbers are equal to the precision of n1.
Example
n1 = '12.3456'
n2 = 12.3456
output = true
n1 = '12.34567432'
n2 = 12.3456
output = false
Solution Stats
Problem Comments
-
4 Comments
Show
1 older comment
Jan
on 4 Mar 2012
It is not getting clear why '12.3456' and 12.3456789 should be accepted as equal.
Andrew Dobrovolc
on 14 Aug 2017
the second test in the test suite is incorrect, it should be a negative assertion.
Siddhant Shenoy
on 26 Mar 2019
Yes, I completely agree with the other comments. The second test case is incorrect. '12.3456' is not equal to 12.3456789 which when rounded off to 4 decimal places becomes 12.3457
The 50 solvers yet (as of 26/03/19) are undeserving of their title as 'Solvers'.
Dyuman Joshi
on 24 Feb 2021
The test case has been corrected.
Solution Comments
Show commentsProblem Recent Solvers234
Suggested Problems
-
407 Solvers
-
String substitution, sub problem to cryptoMath
230 Solvers
-
The sum of the numbers in the vector
596 Solvers
-
678 Solvers
-
576 Solvers
More from this Author56
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!