# Is there a way to know if a variable is NaN, +infinity, -infinity?

**URL:** <https://fortran-lang.discourse.group/t/is-there-a-way-to-know-if-a-variable-is-nan-infinity-infinity/2655>\
**Category:** Uncategorized\
**Created:** [January 26, 2022, 8:18am UTC](https://fortran-lang.discourse.group/t/is-there-a-way-to-know-if-a-variable-is-nan-infinity-infinity/2655 "2022-01-26T08:18:02Z")\
**Posts on this page:** 1\
**Showing post:** 12

<div class="post-metadata">

**Author:** ![zaikunzhang](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/zaikunzhang/32/785_2.png) [@zaikunzhang](https://fortran-lang.discourse.group/u/zaikunzhang)\
**Post date:** [January 27, 2022, 2:06am UTC](https://fortran-lang.discourse.group/t/is-there-a-way-to-know-if-a-variable-is-nan-infinity-infinity/2655/12 "2022-01-27T02:06:31Z")

</div>

> [@CRquantum](#):
>
> If using IEEE\_ARITHMETIC intrinsic module may lead to discrepancy due to -Ofast flag.  
> Then perhaps we can just use below to check NaN, +inf, -inf,
> 
> About if x is NaN,  
> like @vmagnin suggested, we can define a function to check if x = x or not.
> 
> About if x is +inf or -inf, can we use intrinsic HUGE function like below?
> 
> ```auto
> if x > huge(x), it means x is +inf
> if x< -huge(x), it means is -inf
> 
> ```

If you want some replacement that works (for the time being), you may try my [GitHub repo](https://github.com/zaikunzhang/infnan) mentioned above and in [the other thread](https://fortran-lang.discourse.group/t/checking-inf-nan-when-compilers-are-invoked-with-aggressive-optimization-flags/1851) quoted before. They have been tested with 9 compilers (all that I could find) with aggressive optimization flags. I will be very happy to see where they do not work, and if possible, some suggested alternative implementation will be highly appreciated. Many thanks!

---

_[View the full topic](https://fortran-lang.discourse.group/t/is-there-a-way-to-know-if-a-variable-is-nan-infinity-infinity/2655)._
