sqrt(-10)结果-nan是什么意思,

我想输出sqrt(-10)的值,结果给我个-nan,请问-nan是什么意思?

#include<stdio.h>
#include<math.h>
int main()
{
  printf("%f\n", sqrt(-10));
  return 0;
}

img

nan的意思是not a number
对负数开方,结果是个虚数,不是实数

负数不能开根号呗
NaN(Not a Number,非数)是计算机科学中数值数据类型的一类值,表示未定义或不可表示的值

仅供参考:

// https://docs.microsoft.com/en-us/cpp/c-runtime-library/format-specification-syntax-printf-and-wprintf-functions?view=vs-2019
// https://docs.microsoft.com/zh-cn/cpp/porting/visual-cpp-change-history-2003-2015?view=vs-2019
//
// Starting in Visual Studio 2015, if the argument that corresponds to a floating-point conversion specifier (a, A, e, E, f, F, g, G) is infinite,
// indefinite, or NaN, the formatted output conforms to the C99 standard. This table lists the formatted output:
//   Value           Output
//   --------------  ---------
//   infinity        inf
//   Quiet NaN       nan
//   Signaling NaN   nan(snan)
//   Indefinite NaN  nan(ind)
// Any of these values may be prefixed by a sign. If a floating-point type conversion specifier character is a capital letter,
// then the output is also formatted in capital letters. For example, if the format specifier is %F instead of %f, an infinity
// is formatted as INF instead of inf. The scanf functions can also parse these strings, so these values can make a round trip
// through printf and scanf functions.
//
// Before Visual Studio 2015, the CRT used a different, non-standard format for output of infinite, indefinite, and NaN values:
//   Value                           Output
//   ------------------------------  -------------------------
//   + infinity                      1.#INF random-digits
//   - infinity                      -1.#INF random-digits
//   Indefinite (same as quiet NaN)  digit .#IND random-digits
//   NaN                             digit .#NAN random-digits
// Any of these may have been prefixed by a sign, and may have been formatted slightly differently depending on field width and precision,
// sometimes with unusual effects. For example, printf("%.2f\n", INFINITY) would print 1.#J because the #INF would be "rounded" to 2 digits of precision.
#include <stdio.h>
#include <float.h>
int main() {
    int r;
    double d1=-1.0,d2=-2.0;
    printf("input two double (d1 d2):");
    r=scanf("%lf%lf",&d1,&d2);
    printf("scanf return:%d d1:%lg d2:%lg\n",r,d1,d2);
    printf("_finite(d1):%d\n",_finite(d1));
    printf("_fpclass(d1):0x%04X\n",_fpclass(d1));
    return 0;
}
// input two double (d1 d2):nan 22
// scanf return:2 d1:nan d2:22
// _finite(d1):0
// _fpclass(d1):0x0002
//
// input two double (d1 d2):nan(snan) 22
// scanf return:2 d1:nan(snan) d2:22
// _finite(d1):0
// _fpclass(d1):0x0002
//
// input two double (d1 d2):nan(ind) 22
// scanf return:2 d1:-nan(ind) d2:22
// _finite(d1):0
// _fpclass(d1):0x0002
//
// input two double (d1 d2):-inf 22
// scanf return:2 d1:-inf d2:22
// _finite(d1):0
// _fpclass(d1):0x0004



可以查看手册:c++-nan() 中的内容