简体   繁体   中英

How do I set precision while using type double / float in C++

In my previous question Comparing a double and int, without casting or conversion , I found out how the the difference between two doubles was tripping the comparison.

I came accross the method setprecision(), which will help display all the numbers after decimal.

So, the difference of 6.15 and 3.15 was found to be : 3.00000000000000044408920985006

Now, when it gets compared with 3, it returns a result saying it is greater than 3.

How do I force it to take only a limited number of digits?

When I used 6.1 and 3.1, the difference was : 2.99999999999999955591079014994

How should I make the precision so that we know that it is actually equal to 3, and not less than.

Hopefully you should be knowing that floating/double cannot be exactly represented in binary and truncation happens because of the recurring decimal. Your comparison with a float/double with an integer will always fail.

Even your using setprecision will not work because its a method of iomanip to set the precision of the display and not the value being stored.

The portable way of comparing doubles is not to use the '==' operator but to do something like

bool Compare(double a,double b) {
    std::fabs(a - b) < std::numeric_limits<double>::epsilon();
}

You can use this to compare double with float and or integer. You can also write a similar compare function for float

bool Compare(float a,float b) {
    std::fabs(a - b) < std::numeric_limits<float>::epsilon();
}

In a comment on your other question, you were already pointed towards this great paper on floating-point numbers. It's well worth a read.

With reference to your particular question, a standard way is to define a tolerance with which comparison between doubles is to be made. For example if you have two doubles a and b , and wish to determine whether a is larger than b within a tolerance of eps (another double), you might do something like:

if (a - b > eps) {
  // a is greater than b
} else {
  // a is not greater than b
}

Alternatively, if you want to know that a is equal to b within the tolerance specified by eps , you might do something of this sort:

if (std::abs(a - b) <= eps) {
  // a and b are equal within the set tolerance
} else {
  // a and b are not equal within the set tolerance
}

As pointed out by others, C++ comes with some helpful functions out of the box for performing these sorts of comparisons. Look at std::abs , std::numeric_limits , and this nice post on SO.

Here's a comparison function that determines if two numbers are within one LSB of each other.

bool Compare(double a, double b)
{
    return (a <= std::nextafter(b, abs(1.1*b))) && (b <= std::nextafter(a, abs(1.1*a)));
}

std::nextafter is new to C++11, but versions are available in earlier compilers. See Generate next largest or smallest representable floating point number without bit twiddling

setprecision lets you select how many digits you spit out to a stream. It does not decide the number of digits to be considered. For rounding purposes use one of the rounding functions from <cmath> .

The no of digits of precision in float and double actually depends on the size of them respectively.That is why float generally has less precision than double. You can use

    std::cout<<std::setprecision(desired no);

    float a=(desired no);

Now you have sucessfully set the precision of float.The same can be done to other appropriate data type including double as well. Warning do not set precision greater than what a data type has to offer.Double has 15 to 18 digits of precision while float has only 6 to 9 digits of precision.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM