Assigning a Double to a long

This is a discussion on Assigning a Double to a long within the C Programming forums, part of the General Programming Boards category; Hi I am having a problem on a rounding function. When I assign a double to a long sometimes it ...

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    7

    Assigning a Double to a long

    Hi I am having a problem on a rounding function.
    When I assign a double to a long sometimes it changes the number

    so for example

    double d = 23.00
    long l;

    l = d;

    and then when I print l it is 22.

    Any ideas? I can post more of the code, I mean that the double number isn't just initialised and assigned and maybe what happens to it before affects what is happening?

    Thanks !

  2. #2
    Watch for flying houses. Nessarose's Avatar
    Join Date
    Sep 2004
    Posts
    46
    What does your code look like? It prints out fine for me:

    Code:
    double x = 23.00;
    long y = x;
    printf("%ld\n", y);

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    It's possible that, since 23.0 may not be representable as a double (I think it should be, but who knows...) it stores 22.999999 or something like that. Conversion to integral always means "round down", so 22.99999 gets rounded to 22.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,673
    22.99999999 is very possible.

    You can do normal rounding with "y = x + 0.5" of course.

    gg

  5. #5
    Registered User
    Join Date
    Sep 2004
    Posts
    7
    Thanks, the full code is actually a rounding function and this is it:
    Code:
     
    
    double roundd(nm_valor)
    double nm_valor;
    {
      double di_exponente= (double)0.00;
      double aux=0.00;
      unsigned long aux1 = 0; /* unsigned long aux1 = 0; */
      int signo = 1;
      
      if (nm_valor < 0.00)
      {
        signo = -1;
        nm_valor = -nm_valor;
      }  
            
       /* di_exponente = pow (10, (double) NM_DECIMALES);*/
    
        di_exponente = pow ((double)10, (double) NM_DECIMALES);
    
        aux = ((nm_valor * di_exponente) + 0.5);
      
        aux1 = aux/1;
        aux = aux1 / di_exponente;
        aux = aux * signo;
        
        return(aux);
    }
    And if the value that went in was x.5 the rounded value sometimes was x and sometimes x+1.

    Seems part of the problem is with the prints which also do their own rounding...

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Code:
    double roundd(nm_valor)
    double nm_valor;
    {
    Could it be that your syntax is just a little outdated?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    Registered User
    Join Date
    Sep 2004
    Posts
    7
    That is very possible but it is all I have to work with, they are a little stupid here in that sense *sigh*

    surely out of date syntax doesn't result in bad behaviour?

    although it does give problems...but this isn't one of those cases I don't think..
    Last edited by natalia; 09-23-2004 at 08:06 AM.

  8. #8
    Registered User
    Join Date
    Sep 2004
    Posts
    7
    So, just so I can see what it going on in different places (looks like the problem could be in another place because of a division) How can i see all of a double ? I mean how do I print it so that 2.99999999999 doesn't show as 3.00 ?

    Thanks

  9. #9
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Adjust the precision of printf/cout.

    printf("%.8f", val);
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  10. #10
    Registered User
    Join Date
    Sep 2004
    Posts
    7
    Thanks

    The problem is a division 55344.6 divided by 360 on paper gives 153.735 exactly
    but in C it returns 153.734999999999985

    Then carries the error because it rounds to 153.73 as oposed to 153.74 which is what 153.735 would round to.
    Is there a way to fix that?

  11. #11
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by natalia
    Thanks

    The problem is a division 55344.6 divided by 360 on paper gives 153.735 exactly
    but in C it returns 153.734999999999985

    Then carries the error because it rounds to 153.73 as oposed to 153.74 which is what 153.735 would round to.
    Is there a way to fix that?

    When dealing with numbers that cannot be represented exactly in the number system you are using, I don't think it is reasonable to expect exact results.

    55344.6 is not representable exactly as a binary floating point number (like float or double in C).

    It is sometimes possible to derive at an estimate of the upper bound for errors in mathematical calculations, and in some cases it is useful to do so.

    Regards,

    Dave

  12. #12
    Registered User
    Join Date
    Sep 2004
    Posts
    7
    Well it wasn't 55344.6 but 55344.6000000
    Surely that is explicit enough?

    I can understand that some divisions are not exact and cannot be expected to but isn't in a way 55344.6000000/360 the same as 4/2?
    I mean in the sense that by hand you can arrive to an precise result..

    It's not the same as 10.00/3.00 if you know what I mean

  13. #13
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Well it wasn't 55344.6 but 55344.6000000
    There is no difference between the two. Trailing 0s have no meaning in numerology.
    No, this is about the binary representation of the number. An IEEE floating point value is simply not capable of representing this value.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  14. #14
    Registered User
    Join Date
    Sep 2004
    Posts
    7


    doesn't make sense to me, I mean the fact that it is like that, complicates calculations unnecessarily..doesn't it? Or am I missing something? Our app doesn't even require amazing precision and it is already giving problems..

  15. #15
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    And how would you do it? Floating point numbers have been used for a few decades now. If they were so bad, they wouldn't be used anymore.

    Sorry, the only tip I can give you is to increase the precision. double instead of float, long double instead of double (though the latter has no effect in MSVC++).
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. expected primary expression
    By mju4t in forum C Programming
    Replies: 2
    Last Post: 03-27-2007, 06:59 PM
  2. need some help with last part of arrays
    By Lince in forum C Programming
    Replies: 3
    Last Post: 11-18-2006, 08:13 AM
  3. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  4. Unknown Math Issues.
    By Sir Andus in forum C++ Programming
    Replies: 1
    Last Post: 03-06-2006, 05:54 PM
  5. Quick, Partiotion, and Insertion Sort
    By silicon in forum C++ Programming
    Replies: 0
    Last Post: 05-18-2005, 08:47 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21