It seems that most of the solutions given won't work for the input 10.1
. Here's what I have achieved:
Code:
#include <iostream>
#include <string>
#include <cmath> // std::abs
void isZero(double x){
double total = std::abs (x) - std::abs((int)x);
//std::cout << "Decimal Number: " << total << "\n";
if(((total * 10 + 0.5) / 10.0) < 0.10){
std::cout << x << " True\n";
}else{
std::cout << x << " False\n";
}
}
int main(){
double positive[] = {
96.487587, 96.561569, 97.893280, 97.967270,
98.041245, 98.115227, 98.855072, 98.929054,
99.003044, 99.890846, 99.964836, 10.0, 10.1,
10.2, 10.3, 10.4, 10.5, 10.6, 10.7, 10.8, 10.9,
};
double negative[] = {
-96.487587, -96.561569, -97.893280, -97.967270,
-98.041245, -98.115227, -98.855072, -98.929054,
-99.003044, -99.890846, -99.964836, -10.0, -10.1,
-10.2, -10.3, -10.4, -10.5, -10.6, -10.7, -10.8, -10.9
};
for(int i = 0; i < sizeof(positive)/sizeof(positive[0]); i++){
isZero(positive[i]);
}
for(int i = 0; i < sizeof(negative)/sizeof(negative[0]); i++){
isZero(negative[i]);
}
}
Output:
96.4876 False
96.5616 False
97.8933 False
97.9673 False
98.0412 True
98.1152 False
98.8551 False
98.9291 False
99.003 True
99.8908 False
99.9648 False
10 True
10.1 False
10.2 False
10.3 False
10.4 False
10.5 False
10.6 False
10.7 False
10.8 False
10.9 False
-96.4876 False
-96.5616 False
-97.8933 False
-97.9673 False
-98.0412 True
-98.1152 False
-98.8551 False
-98.9291 False
-99.003 True
-99.8908 False
-99.9648 False
-10 True
-10.1 False
-10.2 False
-10.3 False
-10.4 False
-10.5 False
-10.6 False
-10.7 False
-10.8 False
-10.9 False