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

Answers (1)

Jan
Jan on 12 Mar 2022
Edited: Jan on 12 Mar 2022
format long g
dms = degrees2dms(35.55)
dms = 1×3
35 32 59.9999999999898
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 Comments

Thanks for warn about limited precision of doubles effect.
I had to rewrite degrees2dms myself.
I think matlab comp. has to handle this.
have a nice day.
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.
Thanks your intention. But I sorry about unsolution on this subject by matlab..
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.
@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.

Sign in to comment.

Products

Release

R2021b

Asked:

on 12 Mar 2022

Commented:

Jan
on 18 Mar 2022

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!