Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 501719
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T06:13:07+00:00 2026-05-13T06:13:07+00:00

I have a numerical method that could return nan or inf if there was

  • 0

I have a numerical method that could return nan or inf if there was an error, and for testing purposed I’d like to temporarily force it to return nan or inf to ensure the situation is being handled correctly. Is there a reliable, compiler-independent way to create values of nan and inf in C?

After googling for about 10 minutes I’ve only been able to find compiler dependent solutions.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T06:13:07+00:00Added an answer on May 13, 2026 at 6:13 am

    You can test if your implementation has it:

    #include <math.h>
    #ifdef NAN
    /* NAN is supported */
    #endif
    #ifdef INFINITY
    /* INFINITY is supported */
    #endif
    

    The existence of INFINITY is guaranteed by C99 (or the latest draft at least), and “expands to a constant expression of type float representing positive or unsigned
    infinity, if available; else to a positive constant of type float that overflows at translation time.”

    NAN may or may not be defined, and “is defined if and only if the implementation supports quiet NaNs for the float type. It expands to a constant expression of type float representing a quiet NaN.”

    Note that if you’re comparing floating point values, and do:

    a = NAN;
    

    even then,

    a == NAN;
    

    is false. One way to check for NaN would be:

    #include <math.h>
    if (isnan(a)) { ... }
    

    You can also do: a != a to test if a is NaN.

    There is also isfinite(), isinf(), isnormal(), and signbit() macros in math.h in C99.

    C99 also has nan functions:

    #include <math.h>
    double nan(const char *tagp);
    float nanf(const char *tagp);
    long double nanl(const char *tagp);
    

    (Reference: n1256).

    Docs INFINITY
    Docs NAN

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 290k
  • Answers 290k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer On windows set the bind address by starting JBoss using:… May 13, 2026 at 5:49 pm
  • Editorial Team
    Editorial Team added an answer Try this: tr:hover td { background-color: #FFFAF0; color: #000; }… May 13, 2026 at 5:49 pm
  • Editorial Team
    Editorial Team added an answer “Strictly” regular expressions describe regular languages. But many features, such… May 13, 2026 at 5:49 pm

Related Questions

I have a timeseries class that, over the course of a day will hold
I have a string which was converted to a char array and stored in
I spend much of my time programming in R or MATLAB. These languages are
Say I want to have a method that takes any kind of number, is
I have the following scenario where I want to pass in string and a

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.