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 7418655
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T07:55:30+00:00 2026-05-29T07:55:30+00:00

What part of the C++ spec, or the IEEE float spec, states that a

  • 0

What part of the C++ spec, or the IEEE float spec, states that a NaN value should convert to true as opposed to false?

If I look at the C++ standard section 4.12 Boolean Conversions it says:

A zero value, null pointer value, or null member pointer value is
converted to false; any other value is converted to true.

Now IEEE floats say that NaN compares false to any other value. So whether NaN is true or false depends on how you do your comparision (below). Thus I presume there must be an explicit mention.

value == 0 ? false : true
value != 0 ? true : false

Now, what about conversion to an integer. The short program below shows that a variable NAN converted to an integer results in the minimum integer, whereas a constant gets converted to 0 (using GCC). This seems odd.

#include <iostream>
#include <cmath>

void write( double r, int i, bool b )
{
    std::cout << r << " == " <<  i << " == " << (b ? "True" : "False") << std::endl;
}

int main()
{
    double value = NAN;
    write( value, value, value );
    write( NAN, NAN, NAN );
}

Output:

nan == -2147483648 == True
nan == 0 == True

The conversion of a NaN to zero but bool conversion as True seems troubling. I also not that something like MatLab will convert a NaN to 0 using a function like int16.

So, what are the specifics of the relevant standards that state how NaN converts to boolean and integer values?

I’m tagging C as well, since while it may not define the boolean conversion, it probably defines an integral conversion and use in a conditional and I suspect C++ will follow the same rules

  • 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-29T07:55:31+00:00Added an answer on May 29, 2026 at 7:55 am

    In both C and C++, the behaviour is undefined when converting NAN to an integer type (other than bool):

    C99 6.3.1.4/1: When a finite value of real floating type is converted to an integer type other than _Bool, the fractional part is discarded (i.e., the value is truncated toward zero). If the value of the integral part cannot be represented by the integer type, the behavior is undefined.

    C++11 4.9/1: A prvalue of a floating point type can be converted to a prvalue of an integer type. The conversion truncates; that is, the fractional part is discarded. The behavior is undefined if the truncated value cannot be represented in the destination type. [ Note: If the destination type is bool, see 4.12. —end note ]

    In both languages, converting NAN to bool (or _Bool) gives true (or 1):

    C99 6.3.1.2/1: When any scalar value is converted to _Bool, the result is 0 if the value compares equal to 0; otherwise, the result is 1.

    C++11 4.12/1: A zero value, null pointer value, or null member pointer value is converted to false; any other value is converted to true.

    NAN is not a zero value, and doesn’t compare equal to zero.

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

Sidebar

Related Questions

The R 5 RS spec states that as part of the requirements for a
Part of the GUI I'm building using tkinter has a pop-up window that says
Part of my web application is a background script that polls from a beanstalkd
Part of my app caches web pages for offline viewing. To do that, I
Part of my table design is to include a IsDeleted BIT column that is
Part of a complex query that our app is running contains the lines: ...(inner
I am communicating with a machine over serial. Part of the protocol communication spec
Part 8.3.7 of the Modelica Spec describes the function assert and gives two examples,
As part of the WebSocket spec , all client-sent frames must have the payload
Apparently, infinity and NaN are not a part of JSON specification, so this PHP

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.