repost: type conversion problem
Hi,
here is a repost of my message:
I have a problem with following code:
void calculateamount( long* l_amount )
{
double amount;
// amount gets calculated here
// the result is: 19.67
amount *= 100;
*l_amount = amount;
// then l_amount = 1966 and not 1967. WHY ????
// how can i prevent it?
}
I use VC6, SP2. U can find an example project on http://www.dedenet.de/d_l_test.zip
Re: repost: type conversion problem
When you convert a float to an int (or long) then the value is truncated and not rounded.
To round the value try:
*l_amount = (long)(amount + 0.5);
Re: repost: type conversion problem
OOPs, sorry I misread your problem...
Re: repost: type conversion problem
I know that, but the value cannot be truncated because it's 1967 and not 196.7 or something else. i just want to convert
(double)1967 to (ulong)1967.
take a look at that example project, there u can see it.
do u have another solution or do u agree to me that there is a bug in vc6, sp2
Re: repost: type conversion problem
I tested the problem that you show and I get the correct answer, assuming amount = 19.67.
Are you sure its exactly 19.67 and not 19.66xxxx?
Re: repost: type conversion problem
I took a look at you example project, very odd behavior! I'll see what I can find out.
Re: repost: type conversion problem
have u downloaded that example project i made? there u can see that it is exact 19.67
Re: repost: type conversion problem
Hi again Karsten,
I downloaded your project and tested it and it's not a truncation error like I thought yesterday. It has probably something to do with the rounding error that occurs when trying to represent either 3.2 or 16.47 in binary form. Not all decimal numbers can be represented exactly in a limited precision binary form, so they have to be rounded. Though VC6 debugger surely seems to be rounding it back correctly.
This is no bug in VC6, but rather a known limitation of the float value representation.
/Michael Grundberg
M.Sc. Computer Science
Visionova IT-System, Sweden
[email protected]