CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Apr 1999
    Location
    Germany
    Posts
    53

    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




  2. #2
    Join Date
    Apr 1999
    Posts
    90

    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);


  3. #3
    Join Date
    Apr 1999
    Posts
    90

    Re: repost: type conversion problem

    OOPs, sorry I misread your problem...


  4. #4
    Join Date
    Apr 1999
    Location
    Germany
    Posts
    53

    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


  5. #5
    Join Date
    Apr 1999
    Posts
    90

    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?


  6. #6
    Join Date
    Apr 1999
    Posts
    90

    Re: repost: type conversion problem

    I took a look at you example project, very odd behavior! I'll see what I can find out.


  7. #7
    Join Date
    Apr 1999
    Location
    Germany
    Posts
    53

    Re: repost: type conversion problem

    have u downloaded that example project i made? there u can see that it is exact 19.67


  8. #8
    Join Date
    Apr 1999
    Posts
    8

    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]


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured