vendredi 4 juin 2021

how to check indeterminate bool values (bool equivalent of double's NAN and isnormal)

I'm trying to keep track of indeterminate values. When I'm working with floats or doubles, I have a solution that works :

#include <iostream>
#include <cmath>
int main()
{
 double inp;double out;double state[2]={NAN,NAN};
 for(int n=0;n<5;n++)
 {
  inp=(double)n;
  out=state[1]+inp;
  state[1]=state[0];
  state[0]=inp;
  std::cout << std::boolalpha << "valid : " << std::isnormal(out) << "  value=" << out << '\n';
 }
 return 1;
}

It outputs the expected result : 

valid : false  value=nan
valid : false  value=nan
valid : true  value=2
valid : true  value=4
valid : true  value=6

Now I need its equivalent for bool. So basically what would I use for "XXX" (equivalent of NAN) and "YYY" (equivalent of isnormal) below? (this is just a template, so it won't compile...I'm using c++11).

#include <iostream>
int main()
{
 bool inp;bool out;bool state[2]={XXX,XXX};
 for(int n=0;n<5;n++)
 {
  inp=(bool)(n%2);
  out=state[1]^inp;
  state[1]=state[0];
  state[0]=inp;
  std::cout << std::boolalpha << "valid : " << std::YYY(out) << "  value=" << out << '\n';
 }
 return 1;
}

Aucun commentaire:

Enregistrer un commentaire