A deficiency in VC + + and its improving method

Source: Internet
Author: User
Tags float number numeric numeric value

Using the C language program, it is sometimes necessary to convert a numeric value (such as a float-type value) to a numeric value of another type (such as int), at which point we will use the type cast operator, such as (int), (float), and so on. However, in VC + +, when the value of a float is converted to an int type, there are sometimes large errors. I ran into it when I was working, and when I ftemp a float variable with a value of 1140.00 to the int type using the following method,

itemp=(int)ftemp;

At this point, the value of the INT type variable ITEMP is 1139, error 1 (in this article, I call this error "1 Error"). Such a big error, we are not acceptable.

After a thorough test study, I found that: in VC + +, when a float variable initialization (from the screen input a value, or a constant value assigned to it) after the above method to convert to type int, the result is a fractional part of the left, the integer part of the reservation, error less than 1, no "1 Error" And when a float variable is initialized, after some operation, and then converted to type int, there may be "1 error", that is to say, the result not only to remove the decimal part, the whole number of parts may also have a change. For example, we convert the data in metric units to centimeters, use the float variable F to store data in meters, use the int variable i to store the data in centimeters, and use the following statement to implement the data conversion.

i=(int)(f*100);

When f=11.40 (m), i=1139 (cm);

When f=11.41 (m), i=1140 (cm);

When f=12.32 (m), i=1231 (cm);

When f=12.33 (m), i=1232 (cm);

And so on, there are "1 errors" in the conversion of many data.

However, most data conversions are error-free, such as when f=11.39 (m), i=1139 (cm);

When f=12.31 (m), i=1231 (cm).

If you use the following methods to implement data conversion, "1 Error" exists.

float ftemp;
ftemp=f*100;
i=(int)ftemp;

Here, the ftemp is a local variable (defined within the function) or a global variable (defined outside the function). Change the f*100 to f*100.0, "1 Error" also exists. But if you change the ftemp into a class attribute variable (defined in the Class), "1 Error" does not exist.

I also found that the "1 error" phenomenon is symmetric to positive negative numbers. That is, if there is a "1 error", for a positive number, (int) is less than 1 after the conversion, for negative numbers, (int) after the conversion of 1. As in the example above, when f=-11.40 (m), i=-1139 (cm) when f=-11.41 (m), i=-1140 (cm) when f=-12.32 (m), i=-1231 (cm) when f=-12.33 (m), i=-1232 (cm). Moreover, the conversion error is not greater than 1.

For the above analysis, I give a correction of "1 Error" Method for reference. I designed a function that converts a float number to an int, replacing the (int) operator. The list of functions is as follows:

int Float_to_int (float f)
{
int i;
float ferror;
i= (int) F;
ferror=f-(float) i;
if (Fabs (ferror) >0.99) //has "1 error", correction
if (f>0)
i++;
Else
i--;
return (i);
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.