What is the best technique for checking whether a floating point or double value is the same as exactly zero?

What will be an ideal response?


Due to the way floating point and double values are stored, it is not possible to perform accurate checking with the same as operator. The best technique to use for checking whether a floating point value is the same as zero (or any other value) is to obtain the absolute value of the difference of the number and what we are checking for, such as zero. If the difference is very small, such as 0.00001, we may assume the value is zero (or very close). For example, to check if the value of the variable “x” is zero, the check could be performed as shown here:
```
double x;
// set the value in x
if( fabs(x – 0.0) < 0.00001) // checking to see if x is 0.0
{
// assume x is 0.0
}

double y;
// set the value in y
if( fabs(y – 25.0) < 0.00001) // checking to see if y is 25.0
{
// assume y is 25.0
}
```

Computer Science & Information Technology

You might also like to view...

Why is the nohighlight command extremely useful when using the Vim text editor?

What will be an ideal response?

Computer Science & Information Technology

How does software configuration management differ for Web and Mobile Apps?

What will be an ideal response?

Computer Science & Information Technology

Where does a form header appear?

A. At the top of each page B. At the left of each page C. Once, at the left of the form D. Once, at the top of the form

Computer Science & Information Technology

A computer has lost network connectivity. The network technician determines the network cable and the cable drop are both good. Which of the following would the technician use to test the computer's NIC?

A. Cable certifier B. Throughput tester C. Loopback plug D. Multimeter

Computer Science & Information Technology