Fixed point implementation of nan () c++

i am doing fixed point implementation in c++ and i am trying to define "notanumber" and support a function isnan( ) which returns true if the number is not-a-number and false otherwise.

like the code shown below

Test file

1
2
3
4
5
6
7
8
#include "fixed_point_header.h"
int main()
{
fp::fixed_point<long long int, 63> a=fp::fixed_point<long long int, 63>::positive_infinity(); // will assign positive infinity value to a from an function from header 
fp::fixed_point<long long int, 63> b=fp::fixed_point<long long int, 63>::negative_infinity(); // will assign positive infinity value to b from an function from header 
float nan=fp::fixed_point<long long int, 63>::isnan(a,b);
printf( "fixed point nan value  == %f\n", float (nan));
} 


In the header i want to do somewhat like the code shown below if positive and negative infinity values are added, the isnan function should return 1 else 0.

fixed point header.h

1
2
3
4
5
6
7
8
#include fixed_point_header
static fp::fixed_point<FP, I, F> isnan (fp::fixed_point<FP, I, F> x,fp::fixed_point<FP, I, F> y){
/*if ( x + y ) happens, ie. x and y are infinities
      {
 should return 1; }
       else {
 should return 0; }
      } */


can anyone please tell how to proceed with it?
Last edited on
http://en.cppreference.com/w/cpp/numeric/math/isnan
http://en.cppreference.com/w/cpp/numeric/math/isinf

I think what you're trying to do is in cmath, unless I misunderstand what you're trying to do.
1
2
3
4
return x == fp::fixed_point<FP, I, F>::positive_infinity() && 
       y == fp::fixed_point<FP, I, F>::negative_infinity() ||
       x == fp::fixed_point<FP, I, F>::negative_infinity() && 
       y == fp::fixed_point<FP, I, F>::positive_infinity();
@Peter87 i couldn't understand your answer

1
2
3
4
return x == fp::fixed_point<FP, I, F>::positive_infinity() && 
       y == fp::fixed_point<FP, I, F>::negative_infinity() ||
       x == fp::fixed_point<FP, I, F>::negative_infinity() && 
       y == fp::fixed_point<FP, I, F>::positive_infinity();


after which condition i should return these values, can you please explain a bit more detailed how it works?
Last edited on
The way I understand your first post is that you want isnan to take two fixed point numbers x and y. If x is positive infinity and y is negative infinity, or if x is negative infinity and y is positive infinity you want to return true (1) otherwise you want to return false (0). If you put what I wrote as the only thing inside isnan I thought it would do this.
Topic archived. No new replies allowed.