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
}
```
You might also like to view...
A username is most likely assigned by the network administrator and would likely include a part of the user's name
Indicate whether the statement is true or false
Given the following pseudocode, what value of GRADE will be output if 60 is input?
``` Start Read GRADENUM CASENTRY GRADENUM CASE 61 ? GRADENUM ? 80 GRADE = “A” CASE 59 ? GRADENUM ? 60 GRADE = “B” CASE 50 ? GRADENUM < 60 GRADE = “C” CASE other GRADE = “No Grade” ENDCASE Write GRADE Stop ``` a) A b) B c) C d) No Grade