long -> double -> long

Discussion in 'C Programming' started by Steven Woody, Aug 28, 2007.

  1. Steven Woody

    Steven Woody Guest

    long i = nnn;
    long j;
    double d;
    d = i;
    j = ( long )d;

    in this case, i == j ?

    Steven Woody, Aug 28, 2007
    1. Advertisements

  2. Steven Woody

    Jack Klein Guest

    The cast is completely unnecessary, and completely useless. The
    conversion is performed automatically on assignment, and cast does not
    change that. If the value of the double is outside the range of
    values representable in an unsigned long, the behavior is undefined,
    with or without the cast.
    You're welcome.

    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://c-faq.com/
    comp.lang.c++ http://www.parashift.com/c++-faq-lite/
    Jack Klein, Aug 28, 2007
    1. Advertisements

  3. Not necessarily. double pretty much has to be at least 64 bits,
    including the sign and exponent; you end up with about 52 bits
    of mantisa as the minimum. If the nnn that you are storing
    is more than the radix to the power of (1 more than #bits in mantisa)
    (e.g., 2^(1+52), then nnn cannot be stored exactly unless
    the last (64-(1+#bits in mantisa)) happen to be 0.

    This does come up in practice; on SGI and Sun 64 bit machines
    programs compiled in 64 bit mode have 64 bit doubles and 64 bit longs.
    ((1L<<53)+1L) is too large to be stored in a double in such a system.
    Walter Roberson, Aug 28, 2007
  4. i==j yes it is true.
    harpreetsingh911, Aug 28, 2007
  5. Normally, yes. On some systems all integers representable by a long will be
    representable exactly by a double, and so i will always equal j. Change the
    double to a float and put it a very high value, and assuming four bytes for
    each, you will see that j is now usually approximate.
    Malcolm McLean, Aug 28, 2007
  6. Steven Woody

    Ian Collins Guest

    Given a sufficiently high value in that case, j must be approximate.
    Ian Collins, Aug 28, 2007
  7. you have nnn to i by using long data type. the output of both i and j
    will be 0 because it is not poible to output character through long.
    harpreetsingh911, Aug 28, 2007
  8. Steven Woody

    Steven Woody Guest

    thanks for all your inputs. i now understan, if nnn is so large that
    it can not be presented in a double with zero exponent, it becomes
    unexactly. since my system has a 16bit integer and 32bit double, so i
    believe this will not happend and i in above code always equals to j.

    thanks again.
    Steven Woody, Aug 28, 2007
    Walter Roberson, Aug 28, 2007
  10. Steven Woody

    Old Wolf Guest

    It does have some use: to document that the writer
    intended to possibly lose precision in the value.

    (Not that I approve of such use, but others do).
    Old Wolf, Aug 28, 2007
  11. Steven Woody

    Steven Woody Guest

    it seems it conflits with above description. by above, you seem to
    compare the bits numbers of the integer substracting ( 1 + bits of
    leading 1's ) with the bits of mantissa.
    Steven Woody, Aug 29, 2007
  12. Not quite. Most (if not all) floating point systems have the implied
    decimal point (technically a radix point since it is not decimal) at
    the far left so no integers greater than 1 have a zero exponent (and
    on my system, neither does 1).

    Its more a of function bit representation. If the number of
    significant bits in your long (sbl) exceeds the number of significant
    bits a double can hold (sbd), then the rightmost sbl-sbd digits of the
    long will not be present in the double. If any of these bits are 1,
    they will be lost in the first conversion and cannot be recovered in
    the second.

    By way of example, consider a double with a 5-bit mantissa and the
    implied decimal point at the far right. If the long is (in binary)
    11111, then the double will be 11111 with an exponent of 0 and the
    conversion back produces the same value. If the long is 111111, the
    double will be 11111 with an exponent of 1 and the conversion back
    will produce 111110. However, if the long started as 1111100 (which
    is a greater value), the conversion back works because all the
    "missing" bits are zero.

    Please don't quote signatures (the part following the "-- "

    BTW, is you shift key broken? Just as code is easier to read when it
    follows conventions, so is the accompanying text. When asking for
    help, it behooves the requestor to make things as easy as possible for
    the those tempted to respond.

    Remove del for email
    Barry Schwarz, Aug 30, 2007
  13. Not if a sufficient number of low order bits in the binary
    representation of the initial value are zero.

    Remove del for email
    Barry Schwarz, Aug 30, 2007
  14. It is perfectly acceptable to assign a char to a long. But since the
    original poster didn't do that, why would you bring it up?

    Remove del for email
    Barry Schwarz, Aug 30, 2007
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.