CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 21 of 21
  1. #16
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,244

    Re: .11 * 100 trouble in C++

    [ Moved thread ]

  2. #17
    Join Date
    Aug 2006
    Posts
    515

    Re: .11 * 100 trouble in C++

    Quote Originally Posted by kyoryu

    At any rate, casting from a float to an int is something you need to be careful with. You need to think about what you want the output to be for non-integer results. As an example, do you want 10.5 to be converted to 10, or 11?
    Just to clarify, what I want to do is this: Give are set of float values essentially with 2 decimal places. The goal is eliminate the decimal point and get the integer value, which means simply multiply be 100.

    12.34 -> 1234

    Now I am going to eliminate float i think and store th original value in doubles.

  3. #18
    Join Date
    Dec 2005
    Posts
    254

    Re: .11 * 100 trouble in C++

    On second thought:
    First solution (rely on rounding):

    (int)(.11 * 100 + .005) should always produce 11
    (int)(.9 * 10 + .05) should always produce 9
    (int)(.359 * 1000 + .0005) should always produce 359
    It would be less complicated to do it this way:

    (int)(.11 * 100 + .5)
    (int)(.9 * 10 + .5)
    (int)(.359 * 1000 + .5)

  4. #19
    Join Date
    Apr 2007
    Posts
    43

    Re: .11 * 100 trouble in C++

    Quote Originally Posted by zspirit
    Just to clarify, what I want to do is this: Give are set of float values essentially with 2 decimal places. The goal is eliminate the decimal point and get the integer value, which means simply multiply be 100.

    12.34 -> 1234

    Now I am going to eliminate float i think and store th original value in doubles.
    How are you reading the information in in the first place? Given that you always want to store the information as an int, it may be easier to never actually use a float.

    How reasonable this is is dependent on how you're getting the data.

    Though, whether you store as float or double, you'll be better off if you always use the same data type for all caclulations, and then convert to int at the end of it all.

    See the code I posted for an example of this.

  5. #20
    Join Date
    Aug 2006
    Posts
    515

    Re: .11 * 100 trouble in C++

    Here is the solution!!!

    From the original post:
    Code:
    // case 2
    float fVal = .11;
    short int test = (short int) (float) (fVal * 100); // now this right!
    The trick is to typecast it first to float and than to short int!!! ahhh

    if we don't typecast to float, the result is treated as double and with high precision its value becomes .1099999, multiply this by 100 is 10.9, typecast this to int gives 10

    typecasting 10.9 to int would simply discard the .9 part but typecasting it to float first will still work with .9 part because it is float and its value is read as 11.


  6. #21
    Join Date
    Feb 2007
    Posts
    23

    Re: .11 * 100 trouble in C++

    Quote Originally Posted by zspirit
    Here is the solution!!!

    From the original post:
    Code:
    // case 2
    float fVal = .11;
    short int test = (short int) (float) (fVal * 100); // now this right!
    The trick is to typecast it first to float and than to short int!!! ahhh

    if we don't typecast to float, the result is treated as double and with high precision its value becomes .1099999, multiply this by 100 is 10.9, typecast this to int gives 10

    typecasting 10.9 to int would simply discard the .9 part but typecasting it to float first will still work with .9 part because it is float and its value is read as 11.

    It don't work

    try declaring fVal like double directly

    Sorry, my english is not good

Page 2 of 2 FirstFirst 12

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