Re: Bad conversion

Discussion in 'C Programming' started by Michael Karas, Nov 12, 2010.

  1. In article <ib5h1p$460$>, christian@_nospam.com
    says...
    >
    > Hello,
    >
    > I have :
    >
    > float nCap = atof(sCours) * atof(sVolume);
    > where sCours = "29.12" and sVolume= "63810000"
    >
    > then I do :
    >
    > char sCap[20];
    > sprintf(sCap, "%d", (int)nCap);
    >
    > But I get a negative value for sCap : "-1964672256"
    > as nCap = 2.3302950e+009
    >
    > Is there a workaround ?
    > (I tried with long instead of int without success...)
    >
    > Thanks.


    (int)nCap is not a float to integer conversion. It means instead take
    the memory stored representation of nCap and treat those bits as a
    stored int instead of a stored float.

    --

    Michael Karas
    Carousel Design Solutions
    http://www.carousel-design.com
     
    Michael Karas, Nov 12, 2010
    #1
    1. Advertising

  2. > In article <ib5h1p$460$>, christian@_nospam.com
    > says...
    >> Hello,
    >>
    >> I have :
    >>
    >> float nCap = atof(sCours) * atof(sVolume);
    >> where sCours = "29.12" and sVolume= "63810000"
    >>
    >> then I do :
    >>
    >> char sCap[20];
    >> sprintf(sCap, "%d", (int)nCap);
    >>
    >> But I get a negative value for sCap : "-1964672256"


    See below for the probable reason.

    >> as nCap = 2.3302950e+009


    The product of 29.12 and 63810000 is 1858147200, or 1.858147e+09, not
    the number you give above for nCap. So there must be something else
    wrong in your program, or it is different from what you wrote above.

    Michael Karas wrote:
    >
    > (int)nCap is not a float to integer conversion. It means instead take
    > the memory stored representation of nCap and treat those bits as a
    > stored int instead of a stored float.


    No, this is an arithmetic conversion in which the value of nCap is
    truncated towards zero to an "int". The problem is that the value
    2.3302950e+009 is too large for a 32-bit "int" so overflow occurs and
    the behaviour is undefined. Since "long" doesn't work for the OP,
    either, "long" is probably also 32 bits on the OP's system. "long long"
    works for me, with (long long)2.3302950e+009 yielding 2330295040. But
    the last two digits (..40) are of course doubtful.

    --
    Niklas Holsti
    Tidorum Ltd
    niklas holsti tidorum fi
    . @ .
     
    Niklas Holsti, Nov 12, 2010
    #2
    1. Advertising

  3. Michael Karas <> writes:
    > In article <ib5h1p$460$>, christian@_nospam.com
    > says...
    >> I have :
    >>
    >> float nCap = atof(sCours) * atof(sVolume);
    >> where sCours = "29.12" and sVolume= "63810000"
    >>
    >> then I do :
    >>
    >> char sCap[20];
    >> sprintf(sCap, "%d", (int)nCap);
    >>
    >> But I get a negative value for sCap : "-1964672256"
    >> as nCap = 2.3302950e+009
    >>
    >> Is there a workaround ?
    >> (I tried with long instead of int without success...)

    >
    > (int)nCap is not a float to integer conversion. It means instead take
    > the memory stored representation of nCap and treat those bits as a
    > stored int instead of a stored float.


    You are mistaken. (int)nCap is a float to integer conversion; it
    converts the value, not the representation.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Nov 12, 2010
    #3
  4. Michael Karas wrote:
    > In article <ib5h1p$460$>, christian@_nospam.com
    > says...
    >> Hello,
    >>
    >> I have :
    >>
    >> float nCap = atof(sCours) * atof(sVolume);
    >> where sCours = "29.12" and sVolume= "63810000"
    >>
    >> then I do :
    >>
    >> char sCap[20];
    >> sprintf(sCap, "%d", (int)nCap);
    >>
    >> But I get a negative value for sCap : "-1964672256"
    >> as nCap = 2.3302950e+009
    >>
    >> Is there a workaround ?
    >> (I tried with long instead of int without success...)
    >>
    >> Thanks.

    >
    > (int)nCap is not a float to integer conversion. It means instead take
    > the memory stored representation of nCap and treat those bits as a
    > stored int instead of a stored float.


    That is as wrong as wrong could be.
     
    J. J. Farrell, Nov 13, 2010
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    932
  2. Eric Anderson

    Bad Transform or Bad Engine?

    Eric Anderson, Oct 4, 2005, in forum: XML
    Replies:
    1
    Views:
    397
    Peter Flynn
    Oct 5, 2005
  3. =?Utf-8?B?V2lsbGlhbSBTdWxsaXZhbg==?=

    vs2005 publish website doing bad things, bad things

    =?Utf-8?B?V2lsbGlhbSBTdWxsaXZhbg==?=, Oct 25, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    626
    =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=
    Oct 25, 2006
  4. Gene

    Re: Bad code or bad compiler?

    Gene, Jun 1, 2008, in forum: C Programming
    Replies:
    3
    Views:
    289
    Barry Schwarz
    Jun 1, 2008
  5. rantingrick
    Replies:
    44
    Views:
    1,304
    Peter Pearson
    Jul 13, 2010
Loading...

Share This Page