I found the result of nextafter(0., 1.) with data type double and float are not the same.
#include <math.h>
#include <stdio.h>
int main(){
double a;
float b;
a=nextafter(0., 1.);
b=nextafter(0., 1.);
printf("default %e\n",nextafter(0., 1.));
printf("double %e\n",a); //double 4.940656e-324
printf("float %e\n",b); //float 0.000000e+00
return 0;
}
Why the smallest float bigger than 0.0 is 0.000000e+00 ?
When you call
nextafter(0., 1.)and store the result in afloat, implicit type conversion happens. The function returns the smallest positive subnormaldouble, which is 4.940656 x 10⁻³²⁴. This value is then rounded tofloat, but it's too small to be represented as afloat, leading to zero.To get the correct
floatresult, you should usenextafterf(0.f, 1.f):This will give you the smallest positive subnormal
float.