Überprüfen Sie die Doppelvariable, ob sie eine Ganzzahl und kein Gleitkomma enthält

Überprüfen Sie die Doppelvariable, ob sie eine Ganzzahl und kein Gleitkomma enthält


Was ich meine ist folgendes:


  double d1 =555;
double d2=55.343

Ich möchte sagen können, dass d1 eine Ganzzahl ist, d2 jedoch nicht. Gibt es eine einfache Möglichkeit, dies in c/c++ zu tun?


Antworten:


Verwenden Sie std::modf :


double intpart;
modf(value, &intpart) == 0.0

Nicht in int umwandeln ! Die Zahl 1.0e+300 ist auch eine Ganzzahl.


Bearbeiten:Wie Pete Kirkham darauf hinweist, ist das Übergeben von 0 als zweites Argument nicht durch den Standard garantiert, um zu funktionieren, was die Verwendung einer Dummy-Variablen erfordert und den Code leider viel weniger elegant macht.


Einige Code-Antworten


  double d1 =555;
double d2=55.343
double intpart;
modf(value, &intpart) == 0.0
(trunc(x) == x) 
bool double_is_int(double trouble) {    double absolute = abs( trouble );
return absolute == floor(absolute);
}
#include<cmath>
#include<iostream>
int main () {
double d1 = 555;
double d2 = 55.343;
double int_part1;
double int_part2;
using namespace std;
cout <<
boolalpha;
cout <<
d1 <<
" " <<
modf ( d1, &int_part1 ) <<
endl;
cout <<
d1 <<
" " <<
( modf ( d1, &int_part1 ) == 0.0 ) <<
endl;
cout <<
d2 <<
" " <<
modf ( d2, &int_part2 ) <<
endl;
cout <<
d1 <<
" " <<
( modf ( d2, &int_part2 ) == 0.0 ) <<
endl;
cout <<
d2 <<
" " <<
modf ( d2, &int_part2 ) <<
endl;
cout <<
d1 <<
" " <<
( modf ( d2, &int_part2 ) == 0.0 ) <<
endl;
cout <<
d1 <<
" " <<
fmod ( d1, 1.0 ) <<
endl;
cout <<
d1 <<
" " <<
( fmod ( d1, 1.0 ) == 0 ) <<
endl;
cout <<
d2 <<
" " <<
fmod ( d2, 1.0 ) <<
endl;
cout <<
d2 <<
" " <<
( fmod ( d2, 1.0 ) == 0 ) <<
endl;
cout.flush();
modf ( d1, 0 );
// segfault }
int iHaveNoFraction(double d){
return d == trunc(d);
}
if (abs(d1 - (round(d1))) <
0.000000001) { printf "Integer\n";
/* Can not use "==" since we are concerned about precision */ }
if ((d1 - floor(d1) <
0.000000001) || (d1 - floor(d1) >
0.9999999999)) { /* Better store floor value in a temp variable to speed up */ printf "Integer\n";
/* Can not use "==" since we are concerned about precision */ }
if ((d1 - (int)d1) == 0)
// integer
#define _EPSILON_ 0.000001  bool close_to_int(double &d) {
double integer,fraction = modf(d, &integer);
if(fraction <
_EPSILON_)
{
d = integer;
return true;
}
if((1.0 - fraction) <
_EPSILON_)
{
d = integer + 1;
return true;
}
return false;
}
bool isInteger(double d, double delta) {    double absd = abs(d);
if( absd - floor(absd) >
0.5 )
return (ceil(absd) - absd) <
delta;
return (d - floor(absd)) <
delta;
}
#include <math.h>
#include <limits>
int main() { double x, y, n;
x = SOME_VAL;
y = modf( x, &n );
// splits a floating-point value into fractional and integer parts if ( abs(y) <
std::numeric_limits<double>::epsilon() ) {
// no floating part } }
bool isInteger( double value ) {
double flr = floor( value + 1e-5 );
double diff = value - flr;
return diff <
1e-5;
}
double d = 2.000000001;
int i = std::round(d);
std::fabs(d-i) <
10 * std::numeric_limits<double>::epsilon()
return floor(d)==ceil(d);
#include <iostream>
#include <cmath>
int main() {
double
number = 55.12;
if (!(number - std::nearbyint(number))) {
std::cout <<
"Is integer!";
} else {
std::cout <<
"Has decimal!";
}
return 0;
}
if (  ABS( ((int) d1) - (d1)) )<
0.000000001) cout <<"Integer" <<
endl;
else cout <<"Flaot" <<
endl;
#include<iostream>
using namespace std;
int main() {
void checkType(double x);
double d1 = 555;
double d2 = 55.343;
checkType(d1);
checkType(d2);
system("Pause");
return 0;
} void checkType(double x) {
if(x != (int)x)
{
cout<<
x <<
" is not an integer "<<
endl;
}
else
{
cout <<
x <<
" is an integer " <<
endl;
} };