ShortFloat.st
changeset 12915 d80685e8f20e
parent 12629 2683b8a14a8c
child 13358 562da8ee5e75
equal deleted inserted replaced
12914:802070da5c65 12915:d80685e8f20e
   320     "
   320     "
   321 
   321 
   322     "Modified: / 7.1.1998 / 16:17:59 / cg"
   322     "Modified: / 7.1.1998 / 16:17:59 / cg"
   323 ! !
   323 ! !
   324 
   324 
       
   325 !ShortFloat class methodsFor:'accessing'!
       
   326 
       
   327 defaultPrintFormat
       
   328     "this is fixed in ShortFloat"
       
   329 
       
   330     ^ '.6'
       
   331 ! !
       
   332 
   325 !ShortFloat class methodsFor:'binary storage'!
   333 !ShortFloat class methodsFor:'binary storage'!
   326 
   334 
   327 readBinaryIEEESingleFrom:aStream
   335 readBinaryIEEESingleFrom:aStream
   328     "read a float value from the binary stream, aStream,
   336     "read a float value from the binary stream, aStream,
   329      interpreting the next bytes as an IEEE formatted 4-byte float"
   337      interpreting the next bytes as an IEEE formatted 4-byte float"
   938      * actually only needed on sparc: since thisContext is
   946      * actually only needed on sparc: since thisContext is
   939      * in a global register, which gets destroyed by printf,
   947      * in a global register, which gets destroyed by printf,
   940      * manually save it here - very stupid ...
   948      * manually save it here - very stupid ...
   941      */
   949      */
   942     __BEGIN_PROTECT_REGISTERS__
   950     __BEGIN_PROTECT_REGISTERS__
   943 
   951     len = snprintf(buffer, sizeof(buffer), "%.6g", (float)__shortFloatVal(self));
   944 #ifdef SYSV
       
   945     len = snprintf(buffer, sizeof(buffer), "%.6lg", (double)__shortFloatVal(self));
       
   946 #else
       
   947     len = snprintf(buffer, sizeof(buffer), "%.6G", (double)__shortFloatVal(self));
       
   948 #endif
       
   949 
       
   950     __END_PROTECT_REGISTERS__
   952     __END_PROTECT_REGISTERS__
   951 
   953 
   952     if (len >= 0 && len <= sizeof(buffer)-3) {
   954     if (len >= 0 && len <= sizeof(buffer)-3) {
   953 	/*
   955         /*
   954 	 * kludge to make integral float f prints as "f.0" (not as "f" as printf does)
   956          * kludge to make integral float f prints as "f.0" (not as "f" as printf does)
   955 	 * (i.e. look if string contains '.' or 'e' and append '.0' if not)
   957          * (i.e. look if string contains '.' or 'e' and append '.0' if not)
   956 	 */
   958          */
   957 	for (cp = buffer; *cp; cp++) {
   959         for (cp = buffer; *cp; cp++) {
   958 	    if ((*cp == '.') || (*cp == 'E') || (*cp == 'e')) break;
   960             if ((*cp == '.') || (*cp == 'E') || (*cp == 'e')) break;
   959 	}
   961         }
   960 	if (!*cp && (cp[-1] >= '0') && (cp[-1] <= '9')) {
   962         if (!*cp && (cp[-1] >= '0') && (cp[-1] <= '9')) {
   961 	    if (__isCharacter(@global(DecimalPointCharacterForPrinting))) {
   963             if (__isCharacter(@global(DecimalPointCharacterForPrinting))) {
   962 		*cp++ = __intVal(__characterVal(@global(DecimalPointCharacterForPrinting)));
   964                 *cp++ = __intVal(__characterVal(@global(DecimalPointCharacterForPrinting)));
   963 	    } else {
   965             } else {
   964 		*cp++ = '.';
   966                 *cp++ = '.';
   965 	    }
   967             }
   966 	    *cp++ = '0';
   968             *cp++ = '0';
   967 	    *cp = '\0';
   969             *cp = '\0';
   968 	} else {
   970         } else {
   969 	    if (cp && (*cp == '.')) {
   971             if (cp && (*cp == '.')) {
   970 		if (__isCharacter(@global(DecimalPointCharacterForPrinting))) {
   972                 if (__isCharacter(@global(DecimalPointCharacterForPrinting))) {
   971 		    *cp = __intVal(__characterVal(@global(DecimalPointCharacterForPrinting)));
   973                     *cp = __intVal(__characterVal(@global(DecimalPointCharacterForPrinting)));
   972 		}
   974                 }
   973 	    }
   975             }
   974 	}
   976         }
   975 
   977 
   976 	s = __MKSTRING(buffer);
   978         s = __MKSTRING(buffer);
   977 	if (s != nil) {
   979         if (s != nil) {
   978 	    RETURN (s);
   980             RETURN (s);
   979 	}
   981         }
   980     }
   982     }
   981 %}.
   983 %}.
   982     ^ self asFloat printString
   984     ^ self asFloat printString
   983 
   985 
   984     "
   986     "
   985 	1.234 asShortFloat printString.
   987         1.234 asShortFloat printString.
   986 	1.0 asShortFloat printString.
   988         1.0 asShortFloat printString.
   987 	1e10 asShortFloat printString.
   989         1e10 asShortFloat printString.
   988 	1.2e3 asShortFloat printString.
   990         1.2e3 asShortFloat printString.
   989 	1.2e30 asShortFloat printString.
   991         1.2e30 asShortFloat printString.
   990 	(1.0 uncheckedDivide:0) asShortFloat printString.
   992         (1.0 uncheckedDivide:0) asShortFloat printString.
   991 	(0.0 uncheckedDivide:0) asShortFloat printString.
   993         (0.0 uncheckedDivide:0) asShortFloat printString.
   992 
   994         self pi printString.
   993 	DecimalPointCharacterForPrinting := $,.
   995 
   994 	1.234 asShortFloat printString.
   996         DecimalPointCharacterForPrinting := $,.
   995 	1.0 asShortFloat printString.
   997         1.234 asShortFloat printString.
   996 	1e10 asShortFloat printString.
   998         1.0 asShortFloat printString.
   997 	1.2e3 asShortFloat printString.
   999         1e10 asShortFloat printString.
   998 	1.2e30 asShortFloat printString.
  1000         1.2e3 asShortFloat printString.
   999 	(1.0 uncheckedDivide:0) asShortFloat printString.
  1001         1.2e30 asShortFloat printString.
  1000 	(0.0 uncheckedDivide:0) asShortFloat printString.
  1002         (1.0 uncheckedDivide:0) asShortFloat printString.
  1001 	DecimalPointCharacterForPrinting := $.
  1003         (0.0 uncheckedDivide:0) asShortFloat printString.
       
  1004         DecimalPointCharacterForPrinting := $.
  1002     "
  1005     "
  1003 !
  1006 !
  1004 
  1007 
  1005 printfPrintString:formatString
  1008 printfPrintString:formatString
  1006     "non-standard: return a printed representation of the receiver
  1009     "non-standard: return a printed representation of the receiver
  1719 ! !
  1722 ! !
  1720 
  1723 
  1721 !ShortFloat class methodsFor:'documentation'!
  1724 !ShortFloat class methodsFor:'documentation'!
  1722 
  1725 
  1723 version
  1726 version
  1724     ^ '$Header: /cvs/stx/stx/libbasic/ShortFloat.st,v 1.103 2009-12-18 14:41:46 cg Exp $'
  1727     ^ '$Header: /cvs/stx/stx/libbasic/ShortFloat.st,v 1.104 2010-05-05 13:56:55 stefan Exp $'
  1725 !
  1728 !
  1726 
  1729 
  1727 version_CVS
  1730 version_CVS
  1728     ^ '$Header: /cvs/stx/stx/libbasic/ShortFloat.st,v 1.103 2009-12-18 14:41:46 cg Exp $'
  1731     ^ '$Header: /cvs/stx/stx/libbasic/ShortFloat.st,v 1.104 2010-05-05 13:56:55 stefan Exp $'
  1729 ! !
  1732 ! !