1

lets say you have a std::complex<double> array[N];

how would you set all -inf and inf values to 0 in a for loop?

std::isinf won't work for me the compiler predates C++11

pyCthon
  • 11,746
  • 20
  • 73
  • 135
  • See http://stackoverflow.com/questions/2249110/how-do-i-make-a-portable-isnan-isinf-function – robert Sep 09 '12 at 00:05
  • @robert thank's i'll try the solutions there but i'm not sure they will work with std::complex , maybe if i just use the real part of the array – pyCthon Sep 09 '12 at 00:09

2 Answers2

1

Try this: std::numeric_limits<double>::infinity() will provide a value that tells you whether a number is an infinity or not.

Here is an example code for a vector of double values; you can change the repl_inf to use complex<double> instead.

#include <iostream>
#include <algorithm>
#include <vector>
#include <limits>

using namespace std;

int repl_inf (double v) { return (v== std::numeric_limits<double>::infinity() || -v== std::numeric_limits<double>::infinity()) ? 0.0 : v; }

int main() {
    vector<double> v;
    v.push_back(1);
    v.push_back(1.0/0.0);
    v.push_back(2);
    v.push_back(-1.0/0.0);
    transform (v.begin(), v.end(), v.begin(), repl_inf);
    for (int i = 0 ; i != v.size() ; i++) {
        cout << v[i] << endl;
    }
    return 0;
}

On ideone: link.

For complex<double>:

bool isInf(double v) {
    return v== std::numeric_limits<double>::infinity()
       || -v== std::numeric_limits<double>::infinity();
}
bool isInf(complex<double> c) {
    return isInf(c.real) || isInf(c.imag);
}
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • 1
    This needs a negative infinity test. –  Sep 09 '12 at 00:11
  • it's not working for me... i tried `for ( int i = 0; i< N; i++){ if (real(phi[i])== std::numeric_limits::infinity() || -real(phi[i])== std::numeric_limits::infinity()){ phi[i] = 0; }` where phi is std::complex , failed with out the real() as well still printed out inf and -inf – pyCthon Sep 09 '12 at 00:41
  • @pyCthon You need to define a function that tests individual components of `complex` for infinity. I edited the answer to show how it can be done. – Sergey Kalinichenko Sep 09 '12 at 00:45
1
std::replace( array, array + N, std::complex( std::numeric_limits<double>::infinity(), 0.0), std::complex(0.0) );
std::replace( array, array + N, std::complex( -std::numeric_limits<double>::infinity(), 0.0), std::complex(0.0) );

or

bool isinf( std::complex<double> d )
{
  return std::abs( d.real() ) == std::numeric_limits<double>::infinity()
}

std::replace_if( array, array + N, isinf, 0.0 );
Drew Dormann
  • 59,987
  • 13
  • 123
  • 180
  • the first doesn't work as is and the seconds doesn't work either, its `std::complex` not `vector` or just `double` – pyCthon Sep 09 '12 at 00:36
  • @pyCthon what infinity are you referring to in your question? Are you looking for `std::complex( inf, 0 )`? – Drew Dormann Sep 09 '12 at 00:43
  • @pyCthon Edited, based on what I think you're asking. – Drew Dormann Sep 09 '12 at 00:45
  • @pyCthon Are you certain that you're dealing with values like `std::complex(inf, -inf)`? An "imaginary negative infinity" does have a meaning, but it's a very unusual thing to come across. – Drew Dormann Sep 09 '12 at 00:49
  • characteristic function before use in FFT because -inf or inf gives me errors , and my matlab equivalent code doesn't have an inf or -inf in it , the real part is the same thou – pyCthon Sep 09 '12 at 00:58