#FEATURE by cg
authorClaus Gittinger <cg@exept.de>
Tue, 03 Dec 2019 19:49:53 +0100
changeset 25054 8e6c956c4b50
parent 25053 e19e11facb6d
child 25055 0c8fdbe05d9e
#FEATURE by cg class: ShortFloat added: #integerAndFractionParts
ShortFloat.st
--- a/ShortFloat.st	Tue Dec 03 19:45:05 2019 +0100
+++ b/ShortFloat.st	Tue Dec 03 19:49:53 2019 +0100
@@ -2423,6 +2423,49 @@
     "
 !
 
+integerAndFractionParts
+    "return the integer and the fraction part of the receiver as a pair
+     of floats (i.e. the result of the modf function)
+     Adding the parts gives the original value"
+
+    |integerPart fractionPart|
+
+%{
+#ifdef __SCHTEAM__
+    ERROR("unimplemented");
+#else
+    float fVal, iPart, fPart;
+    OBJ i, f;
+
+    fVal = __shortFloatVal(self);
+# ifdef NO_MODFF
+    {
+        double dFrac, dTrunc;
+
+        dFrac = modf((double)fVal, &dTrunc) ;
+        fPart = (float)dFrac;
+        iPart = (float)dTrunc;   
+    }
+# else
+    fPart = modff(fVal, &iPart);
+# endif
+    __qMKSFLOAT(i, iPart);
+    __qMKSFLOAT(f, fPart);
+    integerPart = i;
+    fractionPart = f;
+#endif
+%}.
+    integerPart isNil ifTrue:[^ super integerAndFractionParts].
+    ^ { integerPart . fractionPart }
+
+    "
+     0.5 asShortFloat integerAndFractionParts   
+     -0.5 asShortFloat integerAndFractionParts  
+     12345.6789 asShortFloat integerAndFractionParts      
+     -12345.6789 asShortFloat integerAndFractionParts     
+    "
+!
+
 rounded
     "return the receiver rounded to the nearest integer"