Precision problem while solving Cody Problems
Mostrar comentarios más antiguos
While solving Cody problems, I have often encountered that my answer is not accepted because of the precision doesn't match the required answer. Matlab always rounds the double format to 4 digits after decimal. My logic seems correct on paper, but fails while applied as a code due to the precision problem.
Here's an example


Can somebody help me how to change this or how I can output more than 4 digits?
I know I can use vpa, but cody doesn't accept vpa and also it is symbolic.
6 comentarios
Walter Roberson
el 18 de Oct. de 2020
MATLAB does not round to 4 digits unless you are displaying and you have format short in effect.
Internally MATLAB uses IEEE 754 double precision, which is about 15 1/2 to 16 1/2 digits.
Walter Roberson
el 18 de Oct. de 2020
Really the test suite should not have been coded in terms of isequal() and should have been coded to accept tolerance instead. I will bring this to the attention of some cody people.
Dyuman Joshi
el 18 de Oct. de 2020
Editada: Dyuman Joshi
el 30 de Abr. de 2021
"Matlab always rounds the double format to 4 digits after decimal."
I doubt that. How did you check this? Please show us the exact code that you used to show how MATLAb has performed rounding on the stored data value.
"...why does it output answer upto 4 digits only?"
I doubt that it does. Much more likely is that your algorithm accumulates floating point error in a different way to the provided/expected answer, and thus fails the test (even though it is equivalent given the precision of the data type).
Note that the square brackets are totally superflous: square brackets are a concatentation operator (not a "list" operator, which MATLAB does not have) and should be removed completely:
>> polyval(5:-1:0,0.1)
ans = 0.12345
Walter Roberson
el 18 de Oct. de 2020
MATLAB defaults to format short because for most work, about 4 digits is as much accuracy as can be justified by the precision of the inputs, and much of the time people do not care about the additional digits and the digits just make it more difficult to see the information they are looking for. Most of the time, it is more valuable to use the saved space to output more entries per line instead of fewer more precise entries. In the case where people do need the higher precision then format longg command can be used or the system preference can be modified. I personally often need higher precision but most people do not.
Dyuman Joshi
el 19 de Oct. de 2020
Respuesta aceptada
Más respuestas (0)
Categorías
Más información sobre Number Theory en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!