[Qt 4] abfragen, ob float-Variable Ganzzahl enthält
-
Hy,
wie kann ich ermitteln, ob in einer float Variable eine Ganzzahl gespeichert ist?
Bsp.:
float f=2.5; if (/* hat float Ganzzahl gespeichert?*/) -> false float f=4; if (/* hat float Ganzzahl gespeichert?*/) -> trueDanke schonmal
-
inline bool is_integer( const float &value ) { return value == static_cast< unsigned long >( value ); }greetz, Swordfish
-
if (fmod(f,1)==0)oder
floor(f)==foder
ceil(f)==f
-
hi,
das geht auch:bool Ganzzahl (float z){return z==floor(z);}vg tesu
-
Die "static cast auf long" Variante ist nicht geeignet um zu prüfen ob ein float/double einen Integerwert darstellt, sondern kann nur dann sinnvoll verwendet werden wenn der float/double Wert im Wertebereich eines longs ist.
Was er nicht muss.