Convert dms to dd

Hello,

I'm using the Pacific C for MS-DOS, v7.51 compiler from HI-TECH software. A function I wrote previously on the Microsoft Visual C++ 6.0 Professional Edition does not output the correct results.

#include
#include

double dms(double); /* Convert deg.mmss to decimal degrees */

main()
{
}

double dms(double dd)
{
double dms = 0, mind = 0, secd = 0;
int hr = 0, min = 0, sec = 0;

dms = dd;
hr = (int) dms;
min = (int) ((dms - hr) * 100 + 1.e-6);
sec = (int) (dms * 10000 + 1.e-6) % 100;
mind = (double) min / 60;
secd = (double) sec / 3600;
dd = hr + mind + secd;

return dd;
}

The inclusion of "+ 1.e-6" was suggested to me because when I executed the function on the Microsoft compiler the results weren't always correct. I was told the "+ 1.e-6" would add some "junk" to the output. Now, when I execute the same code on the HI-TECH compiler, I do not get the correct results.

Examples:

Input Output Should Be

36.5212 36.86555556 36.87000000

45.0000 44.98555556 45.00000000

Thanks,

Mark

Comments

  • : Hello,
    :
    : I'm using the Pacific C for MS-DOS, v7.51 compiler from HI-TECH
    : software. A function I wrote previously on the Microsoft Visual C++
    : 6.0 Professional Edition does not output the correct results.
    :
    : #include
    : #include
    :
    : double dms(double); /* Convert deg.mmss to decimal degrees */
    :
    : main()
    : {
    : }
    :
    : double dms(double dd)
    : {
    : double dms = 0, mind = 0, secd = 0;
    : int hr = 0, min = 0, sec = 0;
    :
    : dms = dd;
    : hr = (int) dms;
    : min = (int) ((dms - hr) * 100 + 1.e-6);
    : sec = (int) (dms * 10000 + 1.e-6) % 100;
    : mind = (double) min / 60;
    : secd = (double) sec / 3600;
    : dd = hr + mind + secd;
    :
    : return dd;
    : }
    :
    : The inclusion of "+ 1.e-6" was suggested to me because when I
    : executed the function on the Microsoft compiler the results weren't
    : always correct. I was told the "+ 1.e-6" would add some "junk" to
    : the output. Now, when I execute the same code on the HI-TECH
    : compiler, I do not get the correct results.
    :
    : Examples:
    :
    : Input Output Should Be
    :
    : 36.5212 36.86555556 36.87000000
    :
    : 45.0000 44.98555556 45.00000000
    :
    : Thanks,
    :
    : Mark
    :


    I don't what to be an idiot, but, is it possible that these differences are just due to floating-point inaccuracies?...idk,

    [code]
    #include
    #include

    double dms(double); /* Convert deg.mmss to decimal degrees */

    main()
    {
    printf("%.8f->%.8f
    ",45.0, dms(45.0));
    printf("%.8f->%.8f
    ",36.5212, dms(36.5212));
    return 0;
    }
    double dms(double dd)
    {
    double dms = 0, mind = 0, secd = 0;
    int hr = 0, min = 0, sec = 0;

    dms = dd;
    hr = (int) dms;
    min = (int) ((dms - hr) * 100 + 1.e-6);
    sec = (int) (dms * 10000) % 100;[color=Red]//lookee[/color]
    mind = (double) min / 60;
    secd = (double) sec / 3600;
    dd = hr + mind + secd;

    return dd;
    }
    [/code]

    Compiles on the gcc gave me your "expected" results...I even included the 1.e-6..it was the same...0.000001 is too small of a number, it probably became zero on my machine...

    To be specific, i got:

    45.00000000->45.00000000
    36.52120000->36.87000000


    I think its probably just an inaccuracy thing
    {2]rIng
  • Thanks very much for replying to my post! Do you think maybe the floating-point inaccuracy has to do with the Pacific C compiler? I like the Pacific C compiler because it doesn't have all the overhead of the Microsoft compiler.

    : : Hello,
    : :
    : : I'm using the Pacific C for MS-DOS, v7.51 compiler from HI-TECH
    : : software. A function I wrote previously on the Microsoft Visual C++
    : : 6.0 Professional Edition does not output the correct results.
    : :
    : : #include
    : : #include
    : :
    : : double dms(double); /* Convert deg.mmss to decimal degrees */
    : :
    : : main()
    : : {
    : : }
    : :
    : : double dms(double dd)
    : : {
    : : double dms = 0, mind = 0, secd = 0;
    : : int hr = 0, min = 0, sec = 0;
    : :
    : : dms = dd;
    : : hr = (int) dms;
    : : min = (int) ((dms - hr) * 100 + 1.e-6);
    : : sec = (int) (dms * 10000 + 1.e-6) % 100;
    : : mind = (double) min / 60;
    : : secd = (double) sec / 3600;
    : : dd = hr + mind + secd;
    : :
    : : return dd;
    : : }
    : :
    : : The inclusion of "+ 1.e-6" was suggested to me because when I
    : : executed the function on the Microsoft compiler the results weren't
    : : always correct. I was told the "+ 1.e-6" would add some "junk" to
    : : the output. Now, when I execute the same code on the HI-TECH
    : : compiler, I do not get the correct results.
    : :
    : : Examples:
    : :
    : : Input Output Should Be
    : :
    : : 36.5212 36.86555556 36.87000000
    : :
    : : 45.0000 44.98555556 45.00000000
    : :
    : : Thanks,
    : :
    : : Mark
    : :
    :
    :
    : I don't what to be an idiot, but, is it possible that these
    : differences are just due to floating-point inaccuracies?...idk,
    :
    : [code]:
    : #include
    : #include
    :
    : double dms(double); /* Convert deg.mmss to decimal degrees */
    :
    : main()
    : {
    : printf("%.8f->%.8f
    ",45.0, dms(45.0));
    : printf("%.8f->%.8f
    ",36.5212, dms(36.5212));
    : return 0;
    : }
    : double dms(double dd)
    : {
    : double dms = 0, mind = 0, secd = 0;
    : int hr = 0, min = 0, sec = 0;
    :
    : dms = dd;
    : hr = (int) dms;
    : min = (int) ((dms - hr) * 100 + 1.e-6);
    : sec = (int) (dms * 10000) % 100;[color=Red]//lookee[/color]
    : mind = (double) min / 60;
    : secd = (double) sec / 3600;
    : dd = hr + mind + secd;
    :
    : return dd;
    : }
    : [/code]:
    :
    : Compiles on the gcc gave me your "expected" results...I even
    : included the 1.e-6..it was the same...0.000001 is too small of a
    : number, it probably became zero on my machine...
    :
    : To be specific, i got:
    :
    : 45.00000000->45.00000000
    : 36.52120000->36.87000000
    :
    :
    : I think its probably just an inaccuracy thing
    : {2]rIng

  • : Thanks very much for replying to my post! Do you think maybe the
    : floating-point inaccuracy has to do with the Pacific C compiler? I
    : like the Pacific C compiler because it doesn't have all the overhead
    : of the Microsoft compiler.


    It has nothing to do with the compiler. The reason your code will be very inaccurate in this case is because you are doing integer division, as pointed out.
  • To Lundin,

    Thanks for your input(no pun intended)! Do you have any suggestions for me? When I first tried to figure out how to convert dms to dd I thought about converting the number to a string and then removing the degrees, minutes and seconds, one substring at a time. I'm just a neophyte programmer so I didn't know how to do that. Instead, I wrote the code as you see it on the message board.

    : : Thanks very much for replying to my post! Do you think maybe the
    : : floating-point inaccuracy has to do with the Pacific C compiler? I
    : : like the Pacific C compiler because it doesn't have all the overhead
    : : of the Microsoft compiler.
    :
    :
    : It has nothing to do with the compiler. The reason your code will be
    : very inaccurate in this case is because you are doing integer
    : division, as pointed out.


  • Lundin, thanks for the post! This is very interesting!
    : http://c-faq.com/expr/truncation1.html

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories