Velocity Reviews > Re: Bad conversion

Michael Karas
Guest
Posts: n/a

 11-12-2010
In article <ib5h1p\$460\$(E-Mail Removed)>, 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

Niklas Holsti
Guest
Posts: n/a

 11-12-2010
> In article <ib5h1p\$460\$(E-Mail Removed)>, 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
. @ .

Keith Thompson
Guest
Posts: n/a

 11-12-2010
Michael Karas <(E-Mail Removed)> writes:
> In article <ib5h1p\$460\$(E-Mail Removed)>, 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.velocityreviews.com/forums/(E-Mail Removed) <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"

J. J. Farrell
Guest
Posts: n/a

 11-13-2010
Michael Karas wrote:
> In article <ib5h1p\$460\$(E-Mail Removed)>, 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.