degrees2dms convertion anomally.
이전 댓글 표시
Hi,
When I use degrees2dms, ı saw a calculation anomally. 0.55 degrees is 33 min 00 second not 32 min 60 second.
Have nice and healty days.....
>> dms=degrees2dms(35.55)
dms = 35.0000 32.0000 60.0000
>> deg=dms2degrees(dms) Reverse calculation is true but it received 60 second.
deg = 35.5500
답변 (1개)
format long g
dms = degrees2dms(35.55)
This is a typical effect of the limited precision of doubles. Most decimal numbers cannot be represented exactly in binary format. See also: FAQ: Why is 0.3 - 0.2 - 0.1 not equal to zero?
댓글 수: 5
Gkn Cng
2022년 3월 13일
Jan
2022년 3월 13일
There is no way to handle this. If 35.55 cannot be represented exactly in binary format, there is no way to decide, if a rounding is wanted or nor:
fprintf('%.16f', 35.55)
% >> 35.5499999999999972
If a numerical function is susceptible for such deviations, it is called "instable" and the results are fragile.
Gkn Cng
2022년 3월 17일
Steven Lord
2022년 3월 17일
This is not a MATLAB issue. This is a consequence of the fact that double precision has a finite number of bits with which to represent numbers. In fact, you can see this "unsolution" even if you don't have a computer. Please try this little experiment. Find something to write with and something to write on (ideally compatible things; pencil and paper not pencil and whiteboard.)
Step 1: Using long division (like you learned in school) divide 1 by 3. Call the result x. You are allowed to write as many decimal places of the result as you want, but only those you explicitly write can be used in step 2. No using 0.3 repeating to get "an infinite" number of places.
Step 2: Multiply x by 3. Call the result y.
In exact arithmetic we know (1/3)*3 is exactly 1. But x is not one third. It is slightly smaller than one third because you rounded off one third to fit it into x. If you've written one more decimal place in step 1 you'd have an x that's closer to one third than the x you did use in step 2. Therefore y will not be 1, it will be slightly smaller than 1.
Jan
2022년 3월 18일
@Steven Lord: Thanks for this handy example of the effects of using math with a limited number of digits.
@Gokhan Cengiz: There is no reliable way to solve this. It would introduce other artifacts, if Matlab applies some rounding to guess, what the user meant. I've mentiones the example 0.1+0.2 ~= 0.3 . This does not happen in Matlab only, but in all langauges using IEEE 754 doubles: C, Fortran, Python, Rust and even assembler. This is not an "unsolution" but the world of numerical maths. Generations of computer scientists have learned how to cope with this effect.
카테고리
도움말 센터 및 File Exchange에서 Lengths and Angles에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!