CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Apr 2005
    Posts
    30

    Why this error happen?

    Code:
    float a=127.2344,c;
    int b;
    
    b = abs(a); <====1
    c = a - b; <====2
    1 - Return 127
    2 - Return 0.234375 and the correct is 0.2344 - ERROR.

    Why this problem happen? I really need the correct value. óÒ


    Tks very much ^^

  2. #2
    Join Date
    May 2005
    Location
    Oregon
    Posts
    3,725

    Lightbulb Re: Why this error happen?

    Quote Originally Posted by phoenixbr
    Code:
    float a=127.2344,c;
    int b;
    
    b = abs(a); <====1
    c = a - b; <====2
    1 - Return 127
    2 - Return 0.234375 and the correct is 0.2344 - ERROR.

    Why this problem happen? I really need the correct value. óÒ


    Tks very much ^^
    here is your answer

    Code:
    double a=127.2344,c;
    int b;
    b = abs(a); 
    cout<<b;
    c = a - b; 
    cout<<endl;
    cout<<c;
    due to decleration of float you was getting some thing
    because compiler will treat it as double

  3. #3
    Join Date
    Apr 2005
    Posts
    30

    Re: Why this error happen?

    Tks very much ^^

  4. #4
    Join Date
    May 2005
    Location
    Oregon
    Posts
    3,725

    Lightbulb Re: Why this error happen?

    Quote Originally Posted by phoenixbr
    Tks very much ^^
    you welcome

  5. #5
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

  6. #6
    Join Date
    Jul 2005
    Posts
    2

    Re: Why this error happen?

    Quote Originally Posted by phoenixbr
    Code:
    float a=127.2344,c;
    int b;
    
    b = abs(a); <====1
    c = a - b; <====2
    1 - Return 127
    2 - Return 0.234375 and the correct is 0.2344 - ERROR.

    Why this problem happen? I really need the correct value. óÒ


    Tks very much ^^
    There are two implicit type conversion occured;
    and the complier will transform the type of expression to assigned object.

    Good Luck!

  7. #7
    Join Date
    Feb 2005
    Location
    Normandy in France
    Posts
    4,590

    Re: Why this error happen?

    Quote Originally Posted by phoenixbr
    Code:
    float a=127.2344,c;
    int b;
    
    b = abs(a); <====1
    c = a - b; <====2
    1. It converts from float to integer, then it computes the absolute value of the integer.
      To avoid strange rounding rules, it may be preferable to use fabs which uses a double argument.
      Code:
      b = fabs(a);
    2. It is a precision error, because 127.2344 cannot be correctly represented on a float number (float are binary numbers, not decimal numbers, so 1/5 for example cannot be correctly represented).
      Using double values greatly increase the precision.

      Note, that BC++ has a library for BCD (Binary coded decimals) floats, but use them only if you really need them, because it is not portable.

  8. #8
    Join Date
    Jun 2002
    Location
    Moscow, Russia.
    Posts
    2,176

    Re: Why this error happen?

    You can use floor and fabs functions - it wouldn't require conversions:
    Code:
    double a=127.2344;
    double c=fabs(a)-floor(fabs(a));
    "Programs must be written for people to read, and only incidentally for machines to execute."

  9. #9
    Join Date
    Oct 2000
    Location
    London, England
    Posts
    4,773

    Re: Why this error happen?

    You could also use boost::rational if you really want decimal fractions represented exactly.

    An IEEE float gives 23 significant bits of data (22 bits but there is one assumed 1-bit) so that's about 8 million. Therefore you get just under 7 significant figures in decimal.

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