author | Claus Gittinger <cg@exept.de> |
Thu, 07 Jun 2018 16:23:29 +0200 | |
changeset 23054 | 2f647561edc5 |
parent 22922 | ca17023b590e |
child 23475 | a7369668089d |
permissions | -rw-r--r-- |
22785 | 1 |
"{ Encoding: utf8 }" |
2 |
||
1197 | 3 |
" |
4 |
COPYRIGHT (c) 1996 by Claus Gittinger |
|
3158 | 5 |
All Rights Reserved |
1197 | 6 |
|
7 |
This software is furnished under a license and may be used |
|
8 |
only in accordance with the terms of that license and with the |
|
9 |
inclusion of the above copyright notice. This software may not |
|
10 |
be provided or otherwise made available to, or used by, any |
|
11 |
other person. No title to or ownership of the software is |
|
12 |
hereby transferred. |
|
13 |
" |
|
5411 | 14 |
"{ Package: 'stx:libbasic' }" |
15 |
||
17382 | 16 |
"{ NameSpace: Smalltalk }" |
17 |
||
1197 | 18 |
LimitedPrecisionReal variableByteSubclass:#ShortFloat |
19 |
instanceVariableNames:'' |
|
21872 | 20 |
classVariableNames:'DefaultPrintFormat DefaultPrintfFormat Pi E Epsilon Ln10 NaN |
21 |
PositiveInfinity NegativeInfinity' |
|
1197 | 22 |
poolDictionaries:'' |
23 |
category:'Magnitude-Numbers' |
|
24 |
! |
|
25 |
||
2394 | 26 |
!ShortFloat primitiveDefinitions! |
27 |
%{ |
|
28 |
||
14744 | 29 |
#include <stdio.h> |
3471 | 30 |
#include <errno.h> |
31 |
||
32 |
#ifndef __OPTIMIZE__ |
|
33 |
# define __OPTIMIZE__ |
|
34 |
#endif |
|
4825 | 35 |
|
36 |
#define __USE_ISOC9X 1 |
|
8980
abee9fcb0385
cleanup isfinite() and isnan() - usage (HPUX)
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
37 |
#define __USE_ISOC99 1 |
3471 | 38 |
#include <math.h> |
39 |
||
16670 | 40 |
#ifndef INT32 |
41 |
# define INT32 int |
|
42 |
#endif |
|
43 |
||
3471 | 44 |
/* |
45 |
* on some systems errno is a macro ... check for it here |
|
46 |
*/ |
|
47 |
#ifndef errno |
|
48 |
extern errno; |
|
49 |
#endif |
|
50 |
||
19860
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19617
diff
changeset
|
51 |
#if !defined (__win32__) |
14673
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
52 |
# include <locale.h> |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
53 |
#endif |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
54 |
|
19860
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19617
diff
changeset
|
55 |
#if defined (__aix__) |
2394 | 56 |
# include <float.h> |
57 |
#endif |
|
4825 | 58 |
|
19860
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19617
diff
changeset
|
59 |
#if defined(__irix__) |
2395 | 60 |
# include <nan.h> |
61 |
#endif |
|
4825 | 62 |
|
19860
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19617
diff
changeset
|
63 |
#if defined(__linux__) |
4825 | 64 |
# ifndef NAN |
5822
4e56f8aadda5
Get nan.h from bits/nan.h for Linux
Stefan Vogel <sv@exept.de>
parents:
5718
diff
changeset
|
65 |
# include <bits/nan.h> |
4825 | 66 |
# endif |
2397 | 67 |
#endif |
4825 | 68 |
|
19860
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19617
diff
changeset
|
69 |
#if defined(__solaris__) || defined(__sunos__) |
2398 | 70 |
# include <nan.h> |
71 |
#endif |
|
2394 | 72 |
|
19860
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19617
diff
changeset
|
73 |
#ifdef __win32__ |
3198
4f930c1c96e7
care for systems which do not define finite() / isnan() - sigh
Claus Gittinger <cg@exept.de>
parents:
3194
diff
changeset
|
74 |
/* |
4f930c1c96e7
care for systems which do not define finite() / isnan() - sigh
Claus Gittinger <cg@exept.de>
parents:
3194
diff
changeset
|
75 |
* no finite(x) ? |
4f930c1c96e7
care for systems which do not define finite() / isnan() - sigh
Claus Gittinger <cg@exept.de>
parents:
3194
diff
changeset
|
76 |
* no isnan(x) ? |
4f930c1c96e7
care for systems which do not define finite() / isnan() - sigh
Claus Gittinger <cg@exept.de>
parents:
3194
diff
changeset
|
77 |
*/ |
7419 | 78 |
# ifndef isnan |
79 |
# define isnan(x) \ |
|
19570
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
80 |
((((unsigned int *)(&x))[0] == 0x00000000) && \ |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
81 |
(((unsigned int *)(&x))[1] == 0xFFF80000)) |
7419 | 82 |
# endif |
83 |
||
84 |
# ifndef isPositiveInfinity |
|
85 |
# define isPositiveInfinity(x) \ |
|
19570
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
86 |
((((unsigned int *)(&x))[0] == 0x00000000) && \ |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
87 |
(((unsigned int *)(&x))[1] == 0x7FF00000)) |
3198
4f930c1c96e7
care for systems which do not define finite() / isnan() - sigh
Claus Gittinger <cg@exept.de>
parents:
3194
diff
changeset
|
88 |
# endif |
7419 | 89 |
|
90 |
# ifndef isNegativeInfinity |
|
14931 | 91 |
# define isNegativeInfinity(x) \ |
19570
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
92 |
((((unsigned int *)(&x))[0] == 0x00000000) && \ |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
93 |
(((unsigned int *)(&x))[1] == 0xFFF00000)) |
3198
4f930c1c96e7
care for systems which do not define finite() / isnan() - sigh
Claus Gittinger <cg@exept.de>
parents:
3194
diff
changeset
|
94 |
# endif |
7419 | 95 |
|
96 |
# ifndef isinf |
|
97 |
# define isinf(x) \ |
|
19570
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
98 |
((((unsigned int *)(&x))[0] == 0x00000000) && \ |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
99 |
((((unsigned int *)(&x))[1] & 0x7FF00000) == 0x7FF00000)) |
7419 | 100 |
# endif |
101 |
||
8980
abee9fcb0385
cleanup isfinite() and isnan() - usage (HPUX)
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
102 |
# ifndef isfinite |
abee9fcb0385
cleanup isfinite() and isnan() - usage (HPUX)
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
103 |
# define isfinite(x) (!isinf(x) && !isnan(x)) |
7419 | 104 |
# endif |
105 |
||
9121
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
106 |
# ifndef isnanf |
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
107 |
# define isnanf(x) \ |
19570
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
108 |
(((unsigned int *)(&x))[0] == 0xFFC00000) |
9121
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
109 |
# endif |
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
110 |
|
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
111 |
# ifndef isPositiveInfinityf |
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
112 |
# define isPositiveInfinityf(x) \ |
19570
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
113 |
(((unsigned int *)(&x))[0] == 0x7F800000) |
9121
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
114 |
# endif |
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
115 |
|
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
116 |
# ifndef isNegativeInfinityf |
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
117 |
# define isNegativeInfinityf(x) \ |
19570
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
118 |
(((unsigned int *)(&x))[0] == 0xFF800000) |
9121
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
119 |
# endif |
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
120 |
|
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
121 |
# ifndef isinff |
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
122 |
# define isinff(x) \ |
19570
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
123 |
((((unsigned int *)(&x))[0] & 0x7FFFFFFF) == 0x7F800000) |
9121
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
124 |
# endif |
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
125 |
|
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
126 |
# ifndef isfinitef |
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
127 |
# define isfinitef(x) (!isinff(x) && !isnanf(x)) |
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
128 |
# endif |
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
129 |
|
7419 | 130 |
# define NO_ASINH |
131 |
# define NO_ACOSH |
|
132 |
# define NO_ATANH |
|
22919 | 133 |
|
14727
10825e6940d3
no modff, no fmodf with bcc
Claus Gittinger <cg@exept.de>
parents:
14719
diff
changeset
|
134 |
# ifdef __BORLANDC__ |
10825e6940d3
no modff, no fmodf with bcc
Claus Gittinger <cg@exept.de>
parents:
14719
diff
changeset
|
135 |
# define NO_FMODF |
10825e6940d3
no modff, no fmodf with bcc
Claus Gittinger <cg@exept.de>
parents:
14719
diff
changeset
|
136 |
# define NO_MODFF |
21834
1fe471f18a2e
oops - no log10f under win32
Claus Gittinger <cg@exept.de>
parents:
21829
diff
changeset
|
137 |
# define NO_LOG10F |
21976 | 138 |
# define NO_LOGF |
22919 | 139 |
# define NO_ABSF |
14727
10825e6940d3
no modff, no fmodf with bcc
Claus Gittinger <cg@exept.de>
parents:
14719
diff
changeset
|
140 |
# endif |
14931 | 141 |
|
142 |
# ifdef __MINGW__ |
|
22491 | 143 |
# ifndef _STRING_H_INCLUDED_ |
144 |
# include <string.h> |
|
145 |
# define _STRING_H_INCLUDED_ |
|
146 |
# endif |
|
14931 | 147 |
# endif |
148 |
||
19860
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19617
diff
changeset
|
149 |
#endif /* __win32__ */ |
3198
4f930c1c96e7
care for systems which do not define finite() / isnan() - sigh
Claus Gittinger <cg@exept.de>
parents:
3194
diff
changeset
|
150 |
|
19860
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19617
diff
changeset
|
151 |
#ifdef __solaris__ |
8984 | 152 |
# ifndef isfinite |
153 |
# define isfinite(f) finite((double)(f)) |
|
154 |
# endif |
|
155 |
#endif |
|
156 |
||
19860
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19617
diff
changeset
|
157 |
#ifdef __realIX__ |
8980
abee9fcb0385
cleanup isfinite() and isnan() - usage (HPUX)
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
158 |
# ifndef isfinite |
abee9fcb0385
cleanup isfinite() and isnan() - usage (HPUX)
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
159 |
# define isfinite(x) 1 |
3414 | 160 |
# endif |
8706 | 161 |
#endif /* realIX */ |
162 |
||
9896 | 163 |
#ifndef isfinitef |
9121
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
164 |
# define isfinitef(x) isfinite(x) |
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
165 |
#endif |
9896 | 166 |
#ifndef isnanf |
9121
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
167 |
# define isnanf(x) isnan(x) |
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
168 |
#endif |
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
169 |
|
2394 | 170 |
%} |
171 |
! ! |
|
172 |
||
1880 | 173 |
!ShortFloat class methodsFor:'documentation'! |
1197 | 174 |
|
175 |
copyright |
|
176 |
" |
|
177 |
COPYRIGHT (c) 1996 by Claus Gittinger |
|
3158 | 178 |
All Rights Reserved |
1197 | 179 |
|
180 |
This software is furnished under a license and may be used |
|
181 |
only in accordance with the terms of that license and with the |
|
182 |
inclusion of the above copyright notice. This software may not |
|
183 |
be provided or otherwise made available to, or used by, any |
|
184 |
other person. No title to or ownership of the software is |
|
185 |
hereby transferred. |
|
186 |
" |
|
187 |
||
188 |
||
189 |
! |
|
190 |
||
191 |
documentation |
|
192 |
" |
|
11945 | 193 |
ShortFloats represent rational numbers with limited precision. |
22785 | 194 |
They use the C-compiler's 'float' format, which is usually the IEEE single float format. |
7380 | 195 |
|
196 |
In contrast to Floats (which use the C-compilers 64bit 'double' format), |
|
197 |
ShortFloats give you 32 bit floats. |
|
1197 | 198 |
|
199 |
Notice, that ST/X Floats are what Doubles are in ST-80 and ShortFloats are |
|
14719 | 200 |
ST-80's Floats respectively. The reason was to make ST/X's floats compatible |
22785 | 201 |
to bothe visualWorks and other smalltalks, which use C-doubles for the Float class |
202 |
(i.e. VisualST and V'Age). |
|
14719 | 203 |
Thus, STX's Float precision is not worse than that of other ST's. |
204 |
||
1197 | 205 |
WARNING: |
22785 | 206 |
The layout of shortFloat instances is known by the runtime system and the compiler; |
207 |
you may not add instance variables here. |
|
208 |
Also, subclassing is complicated by the fact, that the VM creates floats/shortFloats, |
|
209 |
and does some of its float-checks by an identity compare with the ShortFloat-class. |
|
210 |
(i.e. your subclasses instances may not be recognized as float-like objects, |
|
211 |
thus mixed mode arithmetic will always coerce them, effectively slowing things down). |
|
212 |
This may be changed, to use a flag bit in the class. |
|
1197 | 213 |
|
7380 | 214 |
Mixed mode arithmetic: |
22785 | 215 |
shortFloat op shortFloat -> shortFloat |
216 |
shortFloat op fix -> shortFloat |
|
217 |
shortFloat op fraction -> shortFloat |
|
218 |
shortFloat op integer -> shortFloat |
|
219 |
shortFloat op longFloat -> longFloat |
|
220 |
shortFloat op float -> float |
|
221 |
shortFloat op complex -> complex |
|
1295 | 222 |
|
7405 | 223 |
Representation: |
22785 | 224 |
32bit single precision IEEE floats |
225 |
23 bit mantissa, |
|
226 |
8 bit exponent, |
|
227 |
6 decimal digits (approx) |
|
7405 | 228 |
|
229 |
Range and Precision of Storage Formats: see LimitedPrecisionReal >> documentation |
|
230 |
||
1295 | 231 |
[author:] |
22785 | 232 |
Claus Gittinger |
1556 | 233 |
|
234 |
[see also:] |
|
22785 | 235 |
Number |
236 |
Float LongFloat Fraction FixedPoint Integer Complex |
|
237 |
FloatArray DoubleArray |
|
1197 | 238 |
" |
239 |
! ! |
|
240 |
||
1880 | 241 |
!ShortFloat class methodsFor:'instance creation'! |
1207 | 242 |
|
243 |
basicNew |
|
244 |
"return a new shortFloat - here we return 0.0 |
|
245 |
- shortFloats are usually NOT created this way ... |
|
246 |
Its implemented here to allow things like binary store & load |
|
247 |
of shortFloats. (but even this support will go away eventually, its not |
|
248 |
a good idea to store the bits of a float - the reader might have a |
|
11945 | 249 |
totally different representation - so floats will eventually be |
1207 | 250 |
binary stored in a device independent format." |
251 |
||
252 |
%{ /* NOCONTEXT */ |
|
253 |
OBJ newFloat; |
|
254 |
||
1695
465e1eba8e8e
removed useless context-arg of all qMK macros - needs full recompile.
Claus Gittinger <cg@exept.de>
parents:
1670
diff
changeset
|
255 |
__qMKSFLOAT(newFloat, 0.0); |
1207 | 256 |
RETURN (newFloat); |
257 |
%} |
|
3158 | 258 |
! |
259 |
||
4297 | 260 |
fastFromString:aString at:startIndex |
11945 | 261 |
"return the next ShortFloat from the string starting at startIndex. |
4297 | 262 |
No spaces are skipped. |
263 |
||
264 |
This is a specially tuned entry (using a low-level C-call), which |
|
265 |
returns garbage if the argument string is not a valid float number. |
|
11945 | 266 |
It has been added to allow higher speed string decomposition into numbers, |
267 |
especially for mass-data." |
|
268 |
||
269 |
%{ /* NOCONTEXT */ |
|
12486
f1ef886832f1
change __isString() to __isStringLike() in primitive code
Stefan Vogel <sv@exept.de>
parents:
11945
diff
changeset
|
270 |
if (__isStringLike(aString) && __isSmallInteger(startIndex)) { |
14673
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
271 |
char *cp = (char *)(__stringVal(aString)); |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
272 |
int idx = __intVal(startIndex) - 1; |
22921 | 273 |
double atof(const char *); |
4297 | 274 |
|
14673
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
275 |
if ((unsigned)idx < __stringSize(aString)) { |
22921 | 276 |
double val = atof(cp + idx); |
14673
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
277 |
RETURN (__MKSFLOAT(val)); |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
278 |
} |
4297 | 279 |
} |
280 |
%}. |
|
281 |
self primitiveFailed. |
|
282 |
||
283 |
" |
|
11945 | 284 |
ShortFloat fastFromString:'123.45' at:1 |
285 |
ShortFloat fastFromString:'123.45' at:2 |
|
286 |
ShortFloat fastFromString:'123.45E4' at:1 |
|
287 |
ShortFloat fastFromString:'hello123.45E4' at:6 |
|
288 |
ShortFloat fastFromString:'12345' at:1 |
|
289 |
ShortFloat fastFromString:'12345' at:2 |
|
290 |
ShortFloat fastFromString:'12345' at:3 |
|
291 |
ShortFloat fastFromString:'12345' at:4 |
|
292 |
ShortFloat fastFromString:'12345' at:5 |
|
293 |
ShortFloat fastFromString:'12345' at:6 |
|
294 |
ShortFloat fastFromString:'12345' at:0 |
|
295 |
ShortFloat fastFromString:'hello123.45E4' at:1 |
|
4297 | 296 |
|
297 |
Time millisecondsToRun:[ |
|
14673
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
298 |
100000 timesRepeat:[ |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
299 |
ShortFloat readFrom:'123.45' |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
300 |
] |
4297 | 301 |
] |
302 |
" |
|
303 |
||
304 |
" |
|
305 |
Time millisecondsToRun:[ |
|
14673
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
306 |
100000 timesRepeat:[ |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
307 |
ShortFloat fastFromString:'123.45' at:1 |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
308 |
] |
4297 | 309 |
] |
310 |
" |
|
311 |
! |
|
312 |
||
15015 | 313 |
fromIEEE32Bit: anInteger |
314 |
"creates a float, given the four native float bytes as an integer" |
|
315 |
||
316 |
%{ /* NOCONTEXT */ |
|
317 |
||
318 |
REGISTER union { |
|
19570
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
319 |
unsigned int i; |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
320 |
float f; |
15015 | 321 |
} r; |
322 |
||
323 |
r.i = __unsignedLongIntVal( anInteger ); |
|
15019 | 324 |
RETURN( __MKSFLOAT(r.f) ); |
15015 | 325 |
%} |
17220 | 326 |
|
327 |
" |
|
19570
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
328 |
ShortFloat fromIEEE32Bit:(#[64 73 15 219] asInteger) |
17220 | 329 |
" |
1207 | 330 |
! ! |
331 |
||
12915
d80685e8f20e
#printString prints with maximum precision (15 for Float and 19 for LongFloat)
Stefan Vogel <sv@exept.de>
parents:
12629
diff
changeset
|
332 |
!ShortFloat class methodsFor:'accessing'! |
d80685e8f20e
#printString prints with maximum precision (15 for Float and 19 for LongFloat)
Stefan Vogel <sv@exept.de>
parents:
12629
diff
changeset
|
333 |
|
d80685e8f20e
#printString prints with maximum precision (15 for Float and 19 for LongFloat)
Stefan Vogel <sv@exept.de>
parents:
12629
diff
changeset
|
334 |
defaultPrintFormat |
16670 | 335 |
^ DefaultPrintFormat |
336 |
! |
|
12915
d80685e8f20e
#printString prints with maximum precision (15 for Float and 19 for LongFloat)
Stefan Vogel <sv@exept.de>
parents:
12629
diff
changeset
|
337 |
|
16670 | 338 |
defaultPrintFormat:aString |
339 |
DefaultPrintFormat := aString. |
|
340 |
! |
|
341 |
||
342 |
epsilon |
|
21799 | 343 |
"return the maximum relative spacing of instances of mySelf |
344 |
(i.e. the value-delta of the least significant bit)" |
|
345 |
||
19570
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
346 |
Epsilon isNil ifTrue:[ |
22853 | 347 |
Epsilon := self computeEpsilon. |
19570
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
348 |
]. |
16670 | 349 |
^ Epsilon |
21799 | 350 |
|
21891 | 351 |
"Modified (comment): / 21-06-2017 / 13:56:47 / cg" |
22853 | 352 |
"Modified (comment): / 10-05-2018 / 01:09:13 / stefan" |
12915
d80685e8f20e
#printString prints with maximum precision (15 for Float and 19 for LongFloat)
Stefan Vogel <sv@exept.de>
parents:
12629
diff
changeset
|
353 |
! ! |
d80685e8f20e
#printString prints with maximum precision (15 for Float and 19 for LongFloat)
Stefan Vogel <sv@exept.de>
parents:
12629
diff
changeset
|
354 |
|
7356 | 355 |
!ShortFloat class methodsFor:'binary storage'! |
356 |
||
357 |
readBinaryIEEESingleFrom:aStream |
|
358 |
"read a float value from the binary stream, aStream, |
|
14924 | 359 |
interpreting the next bytes as an IEEE formatted 4-byte float. |
360 |
The bytes are read in the native byte order (i.e.lsb on intel)" |
|
1197 | 361 |
|
7356 | 362 |
|f| |
1197 | 363 |
|
21878 | 364 |
f := self basicNew. |
15012 | 365 |
self readBinaryIEEESingleFrom:aStream into:f MSB:(UninterpretedBytes isBigEndian). |
366 |
^ f |
|
367 |
||
368 |
"not part of libboss, as this is also used by others (TIFFReader)" |
|
369 |
||
370 |
"Created: / 16-04-1996 / 21:00:35 / cg" |
|
21878 | 371 |
"Modified: / 21-06-2017 / 10:38:31 / cg" |
15012 | 372 |
! |
373 |
||
374 |
readBinaryIEEESingleFrom:aStream MSB:msbFirst |
|
375 |
"read a float value from the binary stream, aStream, |
|
376 |
interpreting the next bytes as an IEEE formatted 4-byte float. |
|
15014 | 377 |
The bytes are read in the soecified byte order" |
15012 | 378 |
|
379 |
|f| |
|
380 |
||
21878 | 381 |
f := self basicNew. |
15012 | 382 |
self readBinaryIEEESingleFrom:aStream into:f MSB:msbFirst. |
7356 | 383 |
^ f |
1197 | 384 |
|
9636 | 385 |
"not part of libboss, as this is also used by others (TIFFReader)" |
386 |
||
387 |
"Created: / 16-04-1996 / 21:00:35 / cg" |
|
21878 | 388 |
"Modified: / 21-06-2017 / 10:38:35 / cg" |
1197 | 389 |
! |
390 |
||
19617 | 391 |
readBinaryIEEESingleFrom:aStream into:aBasicNewShortFloat |
7356 | 392 |
"read a float value from the binary stream, aStream, |
14922 | 393 |
interpreting the next bytes as an IEEE formatted 4-byte float. |
14924 | 394 |
The bytes are read in the native byte order (i.e.lsb on intel)" |
14922 | 395 |
|
19617 | 396 |
^ self readBinaryIEEESingleFrom:aStream into:aBasicNewShortFloat MSB:(UninterpretedBytes isBigEndian) |
14922 | 397 |
|
398 |
"Modified: / 23-08-2006 / 16:01:52 / cg" |
|
399 |
! |
|
400 |
||
19617 | 401 |
readBinaryIEEESingleFrom:aStream into:aBasicNewShortFloat MSB:msb |
14922 | 402 |
"read a float value from the binary stream, aStream, |
403 |
interpreting the next bytes as an IEEE formatted 4-byte float. |
|
404 |
If msb is true, the stream bytes are most-significant-first." |
|
7356 | 405 |
|
21878 | 406 |
aBasicNewShortFloat class == self ifFalse:[self error:'not a ShortFloat']. |
6574 | 407 |
|
7356 | 408 |
" |
409 |
this implementation is wrong: does not work on non-IEEE machines |
|
410 |
(to date all machines where ST/X is running on use |
|
411 |
IEEE float format. Need more here, when porting ST/X to 370's) |
|
412 |
" |
|
413 |
self isIEEEFormat ifFalse:[self error:'unsupported operation']. |
|
414 |
||
14922 | 415 |
(UninterpretedBytes isBigEndian == msb) ifFalse:[ |
21976 | 416 |
"swap the bytes" |
417 |
4 to:1 by:-1 do:[:i | |
|
418 |
aBasicNewShortFloat basicAt:i put:(aStream next) |
|
419 |
]. |
|
420 |
^ self |
|
7356 | 421 |
]. |
422 |
1 to:4 do:[:i | |
|
21976 | 423 |
aBasicNewShortFloat basicAt:i put:aStream next |
7356 | 424 |
] |
9636 | 425 |
|
426 |
"not part of libboss, as this is also used by others (TIFFReader)" |
|
427 |
||
21878 | 428 |
"Modified: / 21-06-2017 / 10:36:28 / cg" |
6574 | 429 |
! |
430 |
||
7356 | 431 |
storeBinaryIEEESingle:aFloat on:aStream |
432 |
"store aFloat as an IEEE formatted 4-byte float |
|
14924 | 433 |
onto the binary stream, aStream. |
434 |
The bytes are written in the native byte order (i.e.lsb on intel)" |
|
435 |
||
436 |
self storeBinaryIEEESingle:aFloat on:aStream MSB:(UninterpretedBytes isBigEndian). |
|
437 |
! |
|
438 |
||
439 |
storeBinaryIEEESingle:aFloat on:aStream MSB:msb |
|
440 |
"store aFloat as an IEEE formatted 4-byte float |
|
441 |
onto the binary stream, aStream. |
|
442 |
If msb is true, the stream bytes are written most-significant-first." |
|
7356 | 443 |
|
8418
37ccc74bbf9e
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
7746
diff
changeset
|
444 |
|float| |
37ccc74bbf9e
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
7746
diff
changeset
|
445 |
|
37ccc74bbf9e
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
7746
diff
changeset
|
446 |
float := aFloat asShortFloat. |
7356 | 447 |
|
448 |
" |
|
449 |
this implementation is wrong: does not work on non-IEEE machines |
|
450 |
(to date all machines where ST/X is running on use |
|
451 |
IEEE float format. Need more here, when porting ST/X to 370's) |
|
452 |
" |
|
453 |
self isIEEEFormat ifFalse:[self error:'unsupported operation']. |
|
6574 | 454 |
|
14924 | 455 |
(UninterpretedBytes isBigEndian == msb) ifFalse:[ |
14931 | 456 |
"swap the bytes" |
457 |
4 to:1 by:-1 do:[:i | |
|
458 |
aStream nextPut:(float basicAt:i). |
|
459 |
]. |
|
460 |
^ self |
|
7356 | 461 |
]. |
462 |
1 to:4 do:[:i | |
|
14931 | 463 |
aStream nextPut:(float basicAt:i). |
7356 | 464 |
] |
9636 | 465 |
|
466 |
"not part of libboss, as this is also used by others (TIFFReader)" |
|
467 |
||
468 |
"Modified: / 23-08-2006 / 16:01:55 / cg" |
|
7356 | 469 |
! ! |
470 |
||
16670 | 471 |
!ShortFloat class methodsFor:'class initialization'! |
472 |
||
473 |
initialize |
|
19570
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
474 |
DefaultPrintFormat := '.7'. "/ print 7 valid digits |
21868 | 475 |
DefaultPrintfFormat := '%7f'. |
16670 | 476 |
|
477 |
" |
|
478 |
self initialize |
|
479 |
" |
|
480 |
! ! |
|
481 |
||
21872 | 482 |
!ShortFloat class methodsFor:'coercing & converting'! |
483 |
||
484 |
coerce:aNumber |
|
485 |
"convert the argument aNumber into an instance of the receiver's class and return it." |
|
486 |
||
487 |
^ aNumber asShortFloat. |
|
488 |
! ! |
|
489 |
||
7356 | 490 |
!ShortFloat class methodsFor:'constants'! |
6574 | 491 |
|
21858 | 492 |
NaN |
493 |
"return a shortFloat which represents not-a-Number (i.e. an invalid number)" |
|
494 |
||
495 |
NaN isNil ifTrue:[ |
|
21868 | 496 |
NaN := super NaN |
21858 | 497 |
]. |
498 |
^ NaN |
|
499 |
||
500 |
"Created: / 20-06-2017 / 13:44:12 / cg" |
|
501 |
! |
|
502 |
||
7392 | 503 |
e |
504 |
"return the constant e as ShortFloat" |
|
505 |
||
19570
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
506 |
E isNil ifTrue:[ |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
507 |
E := Float e asShortFloat |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
508 |
]. |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
509 |
^ E |
7392 | 510 |
! |
511 |
||
18128 | 512 |
emax |
513 |
"Answer the maximum exponent for this representation." |
|
514 |
||
515 |
^127 |
|
516 |
! |
|
517 |
||
18130 | 518 |
emin |
519 |
"Answer the minimum exponent for this representation." |
|
520 |
||
521 |
^-126 |
|
522 |
! |
|
523 |
||
21858 | 524 |
infinity |
525 |
"return a shortFloat which represents positive infinity" |
|
526 |
||
527 |
PositiveInfinity isNil ifTrue:[ |
|
21868 | 528 |
PositiveInfinity := Float infinity asShortFloat |
21858 | 529 |
]. |
530 |
^ PositiveInfinity |
|
531 |
||
532 |
"Created: / 20-06-2017 / 13:44:45 / cg" |
|
533 |
! |
|
534 |
||
21829 | 535 |
ln10 |
536 |
"return the natural logarithm of 10 as a shortFloat" |
|
537 |
||
538 |
Ln10 isNil ifTrue:[ |
|
21834
1fe471f18a2e
oops - no log10f under win32
Claus Gittinger <cg@exept.de>
parents:
21829
diff
changeset
|
539 |
Ln10 := Float ln10 asShortFloat |
21829 | 540 |
]. |
541 |
^ Ln10 |
|
542 |
||
543 |
" |
|
544 |
self ln10 |
|
545 |
" |
|
546 |
||
547 |
"Created: / 16-06-2017 / 11:09:37 / cg" |
|
548 |
! |
|
549 |
||
21858 | 550 |
negativeInfinity |
551 |
"return a shortFloat which represents negative infinity" |
|
552 |
||
553 |
NegativeInfinity isNil ifTrue:[ |
|
21868 | 554 |
NegativeInfinity := Float negativeInfinity asShortFloat |
21858 | 555 |
]. |
556 |
^ NegativeInfinity |
|
557 |
||
558 |
"Created: / 20-06-2017 / 13:45:08 / cg" |
|
559 |
! |
|
560 |
||
7392 | 561 |
pi |
562 |
"return the constant pi as ShortFloat" |
|
563 |
||
19570
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
564 |
Pi isNil ifTrue:[ |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
565 |
Pi := Float pi asShortFloat |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
566 |
]. |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
567 |
^ Pi |
7392 | 568 |
! |
569 |
||
1197 | 570 |
unity |
1260 | 571 |
"return the neutral element for multiplication (1.0) as ShortFloat" |
1197 | 572 |
|
573 |
^ 1.0 asShortFloat |
|
574 |
||
1260 | 575 |
"Modified: 23.4.1996 / 09:26:51 / cg" |
1197 | 576 |
! |
577 |
||
578 |
zero |
|
1260 | 579 |
"return the neutral element for addition (0.0) as ShortFloat" |
1197 | 580 |
|
581 |
^ 0.0 asShortFloat |
|
582 |
||
1260 | 583 |
"Modified: 23.4.1996 / 09:26:45 / cg" |
1197 | 584 |
! ! |
585 |
||
21829 | 586 |
!ShortFloat class methodsFor:'queries'! |
21149 | 587 |
|
21829 | 588 |
defaultPrintPrecision |
589 |
"return the number of decimal digits printed by default" |
|
590 |
||
591 |
^ 5 |
|
592 |
||
593 |
" |
|
21834
1fe471f18a2e
oops - no log10f under win32
Claus Gittinger <cg@exept.de>
parents:
21829
diff
changeset
|
594 |
ShortFloat defaultPrintPrecision |
1fe471f18a2e
oops - no log10f under win32
Claus Gittinger <cg@exept.de>
parents:
21829
diff
changeset
|
595 |
Float defaultPrintPrecision |
1fe471f18a2e
oops - no log10f under win32
Claus Gittinger <cg@exept.de>
parents:
21829
diff
changeset
|
596 |
LongFloat defaultPrintPrecision |
21829 | 597 |
" |
598 |
||
599 |
"Created: / 17-06-2017 / 02:59:31 / cg" |
|
600 |
! |
|
1197 | 601 |
|
6898 | 602 |
exponentCharacter |
603 |
^ $e |
|
604 |
! |
|
605 |
||
1197 | 606 |
isBuiltInClass |
1264 | 607 |
"return true if this class is known by the run-time-system. |
608 |
Here, true is returned for myself, false for subclasses." |
|
1197 | 609 |
|
610 |
^ self == ShortFloat |
|
611 |
||
1264 | 612 |
"Modified: 23.4.1996 / 16:00:23 / cg" |
3427
ba90490c94b6
added query for IEE format (for future use)
Claus Gittinger <cg@exept.de>
parents:
3414
diff
changeset
|
613 |
! |
ba90490c94b6
added query for IEE format (for future use)
Claus Gittinger <cg@exept.de>
parents:
3414
diff
changeset
|
614 |
|
7356 | 615 |
numBitsInExponent |
616 |
"answer the number of bits in the exponent |
|
617 |
This is an IEEE float, where 8 bits are available: |
|
11945 | 618 |
seeeeeee emmmmmmm mmmmmmmm mmmmmmmm |
7356 | 619 |
" |
620 |
||
621 |
^ 8 |
|
622 |
! |
|
623 |
||
624 |
numBitsInMantissa |
|
625 |
"answer the number of bits in the mantissa. |
|
626 |
This is an IEEE float, where 23 bits (the hidden one is not counted here) are available: |
|
11945 | 627 |
seeeeeee emmmmmmm mmmmmmmm mmmmmmmm |
7356 | 628 |
" |
629 |
||
630 |
^ 23 |
|
631 |
! |
|
632 |
||
633 |
precision |
|
634 |
"answer the precision of a ShortFloat (in bits) |
|
11945 | 635 |
This is an IEEE float, where only the fraction from the normalized mantissa is stored |
9149 | 636 |
and so there is a hidden bit and the mantissa is actually represented by 24 binary digits |
637 |
(although only 23 are needed in the binary representation)" |
|
7356 | 638 |
|
639 |
^ 24 |
|
19325 | 640 |
|
641 |
" |
|
642 |
self numBitsInMantissa + 1 |
|
643 |
self precision |
|
644 |
" |
|
7356 | 645 |
! |
646 |
||
647 |
radix |
|
648 |
"answer the radix of a ShortFloats exponent |
|
649 |
This is an IEEE float, which is represented as binary" |
|
650 |
||
9149 | 651 |
^ 2 "must be careful here, whenever ST/X is used on a VAX or a 370" |
1197 | 652 |
! ! |
653 |
||
654 |
!ShortFloat methodsFor:'arithmetic'! |
|
655 |
||
656 |
* aNumber |
|
11730 | 657 |
"return the product of the receiver and the argument." |
1197 | 658 |
|
659 |
%{ /* NOCONTEXT */ |
|
660 |
||
661 |
OBJ newFloat; |
|
662 |
float result; |
|
663 |
double dResult; |
|
664 |
||
665 |
if (__isSmallInteger(aNumber)) { |
|
11945 | 666 |
result = __shortFloatVal(self) * (float)(__intVal(aNumber)); |
1197 | 667 |
retResult: |
11945 | 668 |
__qMKSFLOAT(newFloat, result); |
669 |
RETURN ( newFloat ); |
|
670 |
} |
|
7356 | 671 |
if (__isShortFloat(aNumber)) { |
11945 | 672 |
result = __shortFloatVal(self) * __shortFloatVal(aNumber); |
673 |
goto retResult; |
|
674 |
} |
|
7356 | 675 |
if (__isFloatLike(aNumber)) { |
11945 | 676 |
dResult = (double) __shortFloatVal(self)* __floatVal(aNumber); |
677 |
__qMKFLOAT(newFloat, dResult); |
|
678 |
RETURN ( newFloat ); |
|
1197 | 679 |
} |
680 |
%}. |
|
681 |
^ aNumber productFromShortFloat:self |
|
682 |
! |
|
683 |
||
684 |
+ aNumber |
|
685 |
"return the sum of the receiver and the argument, aNumber" |
|
686 |
||
687 |
%{ /* NOCONTEXT */ |
|
688 |
||
689 |
OBJ newFloat; |
|
690 |
float result; |
|
691 |
double dResult; |
|
692 |
||
693 |
if (__isSmallInteger(aNumber)) { |
|
11945 | 694 |
result = __shortFloatVal(self) + (float)(__intVal(aNumber)); |
1197 | 695 |
retResult: |
11945 | 696 |
__qMKSFLOAT(newFloat, result); |
697 |
RETURN ( newFloat ); |
|
698 |
} |
|
7356 | 699 |
if (__isShortFloat(aNumber)) { |
11945 | 700 |
result = __shortFloatVal(self) + __shortFloatVal(aNumber); |
701 |
goto retResult; |
|
702 |
} |
|
7356 | 703 |
if (__isFloatLike(aNumber)) { |
11945 | 704 |
dResult = (double) __shortFloatVal(self) + __floatVal(aNumber); |
705 |
__qMKFLOAT(newFloat, dResult); |
|
706 |
RETURN ( newFloat ); |
|
1197 | 707 |
} |
708 |
%}. |
|
709 |
^ aNumber sumFromShortFloat:self |
|
710 |
! |
|
711 |
||
712 |
- aNumber |
|
713 |
"return the difference of the receiver and the argument, aNumber" |
|
714 |
||
715 |
%{ /* NOCONTEXT */ |
|
716 |
||
717 |
OBJ newFloat; |
|
718 |
float result; |
|
719 |
double dResult; |
|
720 |
||
721 |
if (__isSmallInteger(aNumber)) { |
|
11945 | 722 |
result = __shortFloatVal(self) - (float)(__intVal(aNumber)); |
1197 | 723 |
retResult: |
11945 | 724 |
__qMKSFLOAT(newFloat, result); |
725 |
RETURN ( newFloat ); |
|
726 |
} |
|
7356 | 727 |
if (__isShortFloat(aNumber)) { |
11945 | 728 |
result = __shortFloatVal(self) - __shortFloatVal(aNumber); |
729 |
goto retResult; |
|
730 |
} |
|
7356 | 731 |
if (__isFloatLike(aNumber)) { |
11945 | 732 |
dResult = (double) __shortFloatVal(self) - __floatVal(aNumber); |
733 |
__qMKFLOAT(newFloat, dResult); |
|
734 |
RETURN ( newFloat ); |
|
1197 | 735 |
} |
736 |
%}. |
|
737 |
^ aNumber differenceFromShortFloat:self |
|
738 |
! |
|
739 |
||
740 |
/ aNumber |
|
741 |
"return the quotient of the receiver and the argument, aNumber" |
|
742 |
||
743 |
%{ /* NOCONTEXT */ |
|
744 |
||
745 |
OBJ newFloat; |
|
746 |
float result, val; |
|
747 |
double dResult, dVal; |
|
748 |
||
749 |
if (__isSmallInteger(aNumber)) { |
|
11945 | 750 |
if (aNumber != __mkSmallInteger(0)) { |
751 |
result = __shortFloatVal(self) / (float)(__intVal(aNumber)); |
|
1197 | 752 |
retResult: |
11945 | 753 |
__qMKSFLOAT(newFloat, result); |
754 |
RETURN ( newFloat ); |
|
755 |
} |
|
756 |
} |
|
7356 | 757 |
if (__isShortFloat(aNumber)) { |
11945 | 758 |
val = __shortFloatVal(aNumber); |
759 |
if (val != 0.0) { |
|
760 |
result = __shortFloatVal(self) / val; |
|
761 |
goto retResult; |
|
762 |
} |
|
763 |
} |
|
7356 | 764 |
if (__isFloatLike(aNumber)) { |
11945 | 765 |
dVal = __floatVal(aNumber); |
766 |
if (dVal != 0.0) { |
|
767 |
dResult = (double) __shortFloatVal(self) / dVal; |
|
768 |
__qMKFLOAT(newFloat, dResult); |
|
769 |
RETURN ( newFloat ); |
|
770 |
} |
|
1197 | 771 |
} |
772 |
%}. |
|
773 |
((aNumber == 0) or:[aNumber = 0.0]) ifTrue:[ |
|
11945 | 774 |
" |
775 |
No, you shalt not divide by zero |
|
776 |
" |
|
777 |
^ ZeroDivide raiseRequestWith:thisContext. |
|
1197 | 778 |
]. |
4166
5730ad6b744b
oops - from coercion in quotient / uncheckedQuotient
Claus Gittinger <cg@exept.de>
parents:
4100
diff
changeset
|
779 |
^ aNumber quotientFromShortFloat:self |
1197 | 780 |
! |
781 |
||
14710 | 782 |
abs |
783 |
"return the absolute value of the receiver |
|
784 |
reimplemented here for speed" |
|
785 |
||
786 |
%{ /* NOCONTEXT */ |
|
787 |
||
788 |
OBJ newFloat; |
|
789 |
float val = __shortFloatVal(self); |
|
790 |
||
791 |
if (val < 0.0) { |
|
792 |
__qMKSFLOAT(newFloat, -val); |
|
793 |
RETURN ( newFloat ); |
|
794 |
} |
|
795 |
RETURN (self); |
|
796 |
%}. |
|
797 |
||
798 |
" |
|
799 |
3.0 asShortFloat abs |
|
800 |
-3.0 asShortFloat abs |
|
801 |
" |
|
802 |
! |
|
803 |
||
804 |
negated |
|
805 |
"return myself negated" |
|
806 |
||
807 |
%{ /* NOCONTEXT */ |
|
808 |
OBJ newFloat; |
|
809 |
float rslt = - __shortFloatVal(self); |
|
810 |
||
811 |
__qMKSFLOAT(newFloat, rslt); |
|
812 |
RETURN ( newFloat ); |
|
813 |
%}. |
|
814 |
^ 0.0 - self |
|
815 |
||
816 |
! |
|
817 |
||
14690 | 818 |
rem: aNumber |
819 |
"return the floating point remainder of the receiver and the argument, aNumber" |
|
820 |
||
821 |
%{ /* NOCONTEXT */ |
|
822 |
||
823 |
/* |
|
824 |
* notice: |
|
825 |
* the following inline code handles some common cases, |
|
826 |
* and exists as an optimization, to speed up those cases. |
|
827 |
* |
|
828 |
* Conceptionally, (and for most other argument types), |
|
829 |
* mixed arithmetic is implemented by double dispatching |
|
830 |
* (see the message send at the bottom) |
|
831 |
*/ |
|
832 |
OBJ newFloat; |
|
833 |
float result, val; |
|
834 |
double dResult, dVal; |
|
835 |
||
836 |
if (__isSmallInteger(aNumber)) { |
|
837 |
if (aNumber != __mkSmallInteger(0)) { |
|
838 |
val = (float)__intVal(aNumber); |
|
839 |
computeResult: |
|
14709
9ba9a2c9e132
ouch: fmodf is not avail with borland
Claus Gittinger <cg@exept.de>
parents:
14690
diff
changeset
|
840 |
#ifdef NO_FMODF |
9ba9a2c9e132
ouch: fmodf is not avail with borland
Claus Gittinger <cg@exept.de>
parents:
14690
diff
changeset
|
841 |
dResult = fmod((double)__shortFloatVal(self), (double)val) ; |
9ba9a2c9e132
ouch: fmodf is not avail with borland
Claus Gittinger <cg@exept.de>
parents:
14690
diff
changeset
|
842 |
result = (float)dResult; |
9ba9a2c9e132
ouch: fmodf is not avail with borland
Claus Gittinger <cg@exept.de>
parents:
14690
diff
changeset
|
843 |
#else |
14690 | 844 |
result = fmodf(__shortFloatVal(self), val) ; |
14709
9ba9a2c9e132
ouch: fmodf is not avail with borland
Claus Gittinger <cg@exept.de>
parents:
14690
diff
changeset
|
845 |
#endif |
14690 | 846 |
__qMKSFLOAT(newFloat, result); |
847 |
RETURN ( newFloat ); |
|
848 |
} |
|
849 |
} else if (__isFloatLike(aNumber)) { |
|
850 |
dVal = __floatVal(aNumber); |
|
851 |
if (dVal != 0.0) { |
|
852 |
dResult = fmod((double)(__shortFloatVal(self)), dVal) ; |
|
853 |
__qMKFLOAT(newFloat, dResult); |
|
854 |
RETURN ( newFloat ); |
|
855 |
} |
|
856 |
} else if (__isShortFloat(aNumber)) { |
|
857 |
val = __shortFloatVal(aNumber); |
|
858 |
if (val != 0.0) { |
|
859 |
goto computeResult; |
|
860 |
} |
|
861 |
} |
|
862 |
%}. |
|
863 |
((aNumber == 0) or:[aNumber = 0.0]) ifTrue:[ |
|
864 |
" |
|
865 |
No, you shalt not divide by zero |
|
866 |
" |
|
867 |
^ ZeroDivide raiseRequestWith:thisContext. |
|
868 |
]. |
|
869 |
^ aNumber remainderFromShortFloat:self |
|
870 |
! |
|
871 |
||
3147 | 872 |
uncheckedDivide:aNumber |
7356 | 873 |
"return the quotient of the receiver and the argument, aNumber. |
7471 | 874 |
Do not check for divide by zero (return NaN or Infinity). |
7356 | 875 |
This operation is provided for emulators of other languages/semantics, |
876 |
where no exception is raised for these results (i.e. Java). |
|
18841 | 877 |
It is only defined if the argument's type is the same as the receiver's." |
3147 | 878 |
|
879 |
%{ /* NOCONTEXT */ |
|
880 |
||
881 |
OBJ newFloat; |
|
882 |
float result, val; |
|
883 |
double dResult, dVal; |
|
884 |
||
885 |
if (__isSmallInteger(aNumber)) { |
|
19570
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
886 |
result = __shortFloatVal(self) / (float)(__intVal(aNumber)); |
3147 | 887 |
retResult: |
19570
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
888 |
__qMKSFLOAT(newFloat, result); |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
889 |
RETURN ( newFloat ); |
11945 | 890 |
} |
7356 | 891 |
if (__isShortFloat(aNumber)) { |
19570
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
892 |
val = __shortFloatVal(aNumber); |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
893 |
result = __shortFloatVal(self) / val; |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
894 |
goto retResult; |
11945 | 895 |
} |
7356 | 896 |
if (__isFloatLike(aNumber)) { |
19570
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
897 |
dVal = __floatVal(aNumber); |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
898 |
dResult = (double) __shortFloatVal(self) / dVal; |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
899 |
__qMKFLOAT(newFloat, dResult); |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
900 |
RETURN ( newFloat ); |
3147 | 901 |
} |
902 |
%}. |
|
4166
5730ad6b744b
oops - from coercion in quotient / uncheckedQuotient
Claus Gittinger <cg@exept.de>
parents:
4100
diff
changeset
|
903 |
^ aNumber quotientFromShortFloat:self |
3147 | 904 |
|
905 |
" |
|
906 |
0.0 asShortFloat uncheckedDivide:0 |
|
907 |
1.0 asShortFloat uncheckedDivide:0.0 |
|
908 |
" |
|
1197 | 909 |
! ! |
910 |
||
5257 | 911 |
!ShortFloat methodsFor:'coercing & converting'! |
1197 | 912 |
|
913 |
asFloat |
|
11238 | 914 |
"return a Float with same value as the receiver. |
915 |
Redefined for performance (machine can do it faster)" |
|
1197 | 916 |
|
917 |
%{ /* NOCONTEXT */ |
|
918 |
||
919 |
OBJ newFloat; |
|
920 |
double dVal = (double)__shortFloatVal(self); |
|
921 |
||
1695
465e1eba8e8e
removed useless context-arg of all qMK macros - needs full recompile.
Claus Gittinger <cg@exept.de>
parents:
1670
diff
changeset
|
922 |
__qMKFLOAT(newFloat, dVal); |
1197 | 923 |
RETURN ( newFloat ); |
924 |
%} |
|
925 |
||
926 |
" |
|
11945 | 927 |
1.0 asShortFloat asFloat |
1197 | 928 |
" |
929 |
! |
|
930 |
||
931 |
asInteger |
|
932 |
"return an integer with same value - might truncate" |
|
933 |
||
934 |
%{ /* NOCONTEXT */ |
|
1670 | 935 |
float fVal; |
1197 | 936 |
|
1670 | 937 |
fVal = __shortFloatVal(self); |
19860
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19617
diff
changeset
|
938 |
#ifdef __win32__ |
11945 | 939 |
if (! isnanf(fVal)) |
7419 | 940 |
#endif |
941 |
{ |
|
11945 | 942 |
if ((fVal >= (float)_MIN_INT) && (fVal <= (float)_MAX_INT)) { |
943 |
RETURN ( __mkSmallInteger( (INT)fVal) ); |
|
944 |
} |
|
1197 | 945 |
} |
946 |
%}. |
|
947 |
^ super asInteger |
|
948 |
||
949 |
" |
|
950 |
12345.0 asShortFloat asInteger |
|
951 |
1e15 asShortFloat asInteger |
|
952 |
" |
|
953 |
! |
|
954 |
||
7437
834ea4d6cdbc
oops - printString of NaN/INF where wrong;
Claus Gittinger <cg@exept.de>
parents:
7419
diff
changeset
|
955 |
asLongFloat |
21891 | 956 |
"return a LongFloat with same value as the receiver" |
957 |
||
7437
834ea4d6cdbc
oops - printString of NaN/INF where wrong;
Claus Gittinger <cg@exept.de>
parents:
7419
diff
changeset
|
958 |
^ LongFloat fromShortFloat:self |
21891 | 959 |
|
960 |
"Modified (comment): / 21-06-2017 / 13:59:41 / cg" |
|
7437
834ea4d6cdbc
oops - printString of NaN/INF where wrong;
Claus Gittinger <cg@exept.de>
parents:
7419
diff
changeset
|
961 |
! |
834ea4d6cdbc
oops - printString of NaN/INF where wrong;
Claus Gittinger <cg@exept.de>
parents:
7419
diff
changeset
|
962 |
|
1197 | 963 |
asShortFloat |
15022 | 964 |
"return a ShortFloat with same value as the receiver - that's me" |
1197 | 965 |
|
966 |
^ self |
|
1200 | 967 |
! |
968 |
||
7356 | 969 |
coerce:aNumber |
18841 | 970 |
"convert the argument aNumber into an instance of the receiver's class and return it." |
7356 | 971 |
|
972 |
^ aNumber asShortFloat |
|
973 |
! |
|
974 |
||
1200 | 975 |
generality |
976 |
"return the generality value - see ArithmeticValue>>retry:coercing:" |
|
977 |
||
978 |
^ 70 |
|
979 |
||
980 |
||
1197 | 981 |
! ! |
982 |
||
983 |
!ShortFloat methodsFor:'comparing'! |
|
984 |
||
985 |
< aNumber |
|
986 |
"return true, if the argument is greater" |
|
987 |
||
988 |
%{ /* NOCONTEXT */ |
|
989 |
||
990 |
if (__isSmallInteger(aNumber)) { |
|
11945 | 991 |
RETURN ( (__shortFloatVal(self) < (float)(__intVal(aNumber))) ? true : false ); |
1197 | 992 |
} |
7356 | 993 |
if (aNumber != nil) { |
11945 | 994 |
if (__qIsFloatLike(aNumber)) { |
995 |
RETURN ( (double)(__shortFloatVal(self) < __floatVal(aNumber)) ? true : false ); |
|
996 |
} |
|
997 |
if (__qIsShortFloat(aNumber)) { |
|
998 |
RETURN ( (__shortFloatVal(self) < __shortFloatVal(aNumber)) ? true : false ); |
|
999 |
} |
|
1197 | 1000 |
} |
1001 |
%}. |
|
1002 |
^ aNumber lessFromShortFloat:self |
|
1003 |
||
1200 | 1004 |
" |
1005 |
1.0 asShortFloat > (1/3) |
|
1006 |
" |
|
1197 | 1007 |
! |
1008 |
||
1009 |
<= aNumber |
|
1010 |
"return true, if the argument is greater or equal" |
|
1011 |
||
1012 |
%{ /* NOCONTEXT */ |
|
1013 |
||
1014 |
if (__isSmallInteger(aNumber)) { |
|
11945 | 1015 |
RETURN ( (__shortFloatVal(self) <= (float)(__intVal(aNumber))) ? true : false ); |
1197 | 1016 |
} |
7356 | 1017 |
if (aNumber != nil) { |
11945 | 1018 |
if (__qIsFloatLike(aNumber)) { |
1019 |
RETURN ( (double)(__shortFloatVal(self) <= __floatVal(aNumber)) ? true : false ); |
|
1020 |
} |
|
1021 |
if (__qIsShortFloat(aNumber)) { |
|
1022 |
RETURN ( (__shortFloatVal(self) <= __shortFloatVal(aNumber)) ? true : false ); |
|
1023 |
} |
|
1024 |
} |
|
1197 | 1025 |
%}. |
1026 |
^ self retry:#<= coercing:aNumber |
|
1027 |
! |
|
1028 |
||
1029 |
= aNumber |
|
7356 | 1030 |
"return true, if the argument represents the same numeric value |
1031 |
as the receiver, false otherwise" |
|
1197 | 1032 |
|
1033 |
%{ /* NOCONTEXT */ |
|
1034 |
||
1035 |
if (__isSmallInteger(aNumber)) { |
|
11945 | 1036 |
RETURN ( (__shortFloatVal(self) == (float)(__intVal(aNumber))) ? true : false ); |
7356 | 1037 |
} |
1038 |
if (aNumber == nil) { |
|
11945 | 1039 |
RETURN (false); |
1197 | 1040 |
} |
7356 | 1041 |
if (__qIsFloatLike(aNumber)) { |
11945 | 1042 |
RETURN ( (double)(__shortFloatVal(self) == __floatVal(aNumber)) ? true : false ); |
1197 | 1043 |
} |
7356 | 1044 |
if (__qIsShortFloat(aNumber)) { |
11945 | 1045 |
RETURN ( (__shortFloatVal(self) == __shortFloatVal(aNumber)) ? true : false ); |
1197 | 1046 |
} |
1047 |
%}. |
|
7356 | 1048 |
^ aNumber equalFromShortFloat:self |
1197 | 1049 |
! |
1050 |
||
1051 |
> aNumber |
|
1052 |
"return true, if the argument is less" |
|
1053 |
||
1054 |
%{ /* NOCONTEXT */ |
|
1055 |
||
1056 |
if (__isSmallInteger(aNumber)) { |
|
11945 | 1057 |
RETURN ( (__shortFloatVal(self) > (float)(__intVal(aNumber))) ? true : false ); |
1197 | 1058 |
} |
7356 | 1059 |
if (aNumber != nil) { |
11945 | 1060 |
if (__qIsFloatLike(aNumber)) { |
1061 |
RETURN ( (double)(__shortFloatVal(self) > __floatVal(aNumber)) ? true : false ); |
|
1062 |
} |
|
1063 |
if (__qIsShortFloat(aNumber)) { |
|
1064 |
RETURN ( (__shortFloatVal(self) > __shortFloatVal(aNumber)) ? true : false ); |
|
1065 |
} |
|
1197 | 1066 |
} |
1067 |
%}. |
|
1068 |
^ self retry:#> coercing:aNumber |
|
1069 |
! |
|
1070 |
||
1071 |
>= aNumber |
|
1072 |
"return true, if the argument is less or equal" |
|
1073 |
||
1074 |
%{ /* NOCONTEXT */ |
|
1075 |
||
1076 |
if (__isSmallInteger(aNumber)) { |
|
11945 | 1077 |
RETURN ( (__shortFloatVal(self) >= (float)(__intVal(aNumber))) ? true : false ); |
1197 | 1078 |
} |
7356 | 1079 |
if (aNumber != nil) { |
11945 | 1080 |
if (__qIsFloatLike(aNumber)) { |
1081 |
RETURN ( (double)(__shortFloatVal(self) >= __floatVal(aNumber)) ? true : false ); |
|
1082 |
} |
|
1083 |
if (__qIsShortFloat(aNumber)) { |
|
1084 |
RETURN ( (__shortFloatVal(self) >= __shortFloatVal(aNumber)) ? true : false ); |
|
1085 |
} |
|
1197 | 1086 |
} |
1087 |
%}. |
|
1088 |
^ self retry:#>= coercing:aNumber |
|
1089 |
! |
|
1090 |
||
1091 |
hash |
|
1092 |
"return a number for hashing; redefined, since floats compare |
|
1093 |
by numeric value (i.e. 3.0 = 3), therefore 3.0 hash must be the same |
|
4594
eb09f567a3bc
float, shortFloat and fraction all hash alike
Claus Gittinger <cg@exept.de>
parents:
4593
diff
changeset
|
1094 |
as 3 hash." |
1197 | 1095 |
|
1096 |
|i| |
|
1097 |
||
1098 |
(self >= SmallInteger minVal and:[self <= SmallInteger maxVal]) ifTrue:[ |
|
5120
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
4825
diff
changeset
|
1099 |
i := self asInteger. |
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
4825
diff
changeset
|
1100 |
self = i ifTrue:[ |
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
4825
diff
changeset
|
1101 |
^ i hash |
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
4825
diff
changeset
|
1102 |
]. |
1197 | 1103 |
]. |
1104 |
||
4594
eb09f567a3bc
float, shortFloat and fraction all hash alike
Claus Gittinger <cg@exept.de>
parents:
4593
diff
changeset
|
1105 |
^ self asFloat hash |
1197 | 1106 |
|
1107 |
" |
|
11945 | 1108 |
1.2345 hash |
1109 |
1.2345 asShortFloat hash |
|
1110 |
1.0 hash |
|
1111 |
1.0 asShortFloat hash |
|
1112 |
0.5 asShortFloat hash |
|
1113 |
0.25 asShortFloat hash |
|
1114 |
0.5 hash |
|
1115 |
0.25 hash |
|
1197 | 1116 |
" |
1117 |
! |
|
1118 |
||
19570
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1119 |
isAlmostEqualTo:aNumber nEpsilon:nE |
16670 | 1120 |
"return true, if the argument, aNumber represents almost the same numeric value |
1121 |
as the receiver, false otherwise. |
|
1122 |
||
1123 |
nE is the number of minimal float distances, that the numbers may differ and |
|
1124 |
still be considered equal. |
|
1125 |
||
19570
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1126 |
For background information why floats need this |
16670 | 1127 |
read: http://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/ |
1128 |
" |
|
1129 |
||
21149 | 1130 |
Epsilon isNil ifTrue:[ |
22853 | 1131 |
Epsilon := self class computeEpsilon. |
21149 | 1132 |
]. |
1133 |
||
16670 | 1134 |
%{ /* NOCONTEXT */ |
1135 |
||
1136 |
/* |
|
1137 |
* notice: |
|
1138 |
* the following inline code handles some common cases, |
|
1139 |
* and exists as an optimization, to speed up those cases. |
|
1140 |
* |
|
1141 |
* Conceptionally, (and for most other argument types), |
|
1142 |
* mixed arithmetic is implemented by double dispatching |
|
1143 |
* (see the message send at the bottom) |
|
1144 |
*/ |
|
1145 |
||
1146 |
INT32 ulpDiff; |
|
1147 |
union { |
|
22853 | 1148 |
float f; |
1149 |
INT32 i; |
|
16670 | 1150 |
} myself, otherFloat; |
1151 |
int nEpsilon; |
|
1152 |
float scaledEpsilon; |
|
1153 |
||
1154 |
if (!__isSmallInteger(nE)) { |
|
22853 | 1155 |
goto tryHarder; |
16670 | 1156 |
} |
1157 |
||
1158 |
nEpsilon = __intVal(nE); |
|
1159 |
scaledEpsilon = nEpsilon *__shortFloatVal(@global(Epsilon)); |
|
1160 |
||
1161 |
if (__isSmallInteger(aNumber)) { |
|
22853 | 1162 |
otherFloat.f = (float)(__intVal(aNumber)); |
16670 | 1163 |
} else if (aNumber == nil) { |
22853 | 1164 |
RETURN(false) |
16670 | 1165 |
} else if (__qIsFloatLike(aNumber)) { |
22853 | 1166 |
otherFloat.f = (float)(__floatVal(aNumber)); |
16670 | 1167 |
} else if (__qIsShortFloat(aNumber)) { |
22853 | 1168 |
otherFloat.f = __shortFloatVal(aNumber); |
1169 |
// } else if (__qIsLongFloat(aNumber)) { |
|
1170 |
// otherFloat.f = (float)(__longFloatVal(aNumber)); |
|
16670 | 1171 |
} else { |
22853 | 1172 |
goto tryHarder; |
16670 | 1173 |
} |
1174 |
||
1175 |
myself.f = __shortFloatVal(self); |
|
1176 |
||
1177 |
// Check if the numbers are really close -- needed |
|
1178 |
// when comparing numbers near zero (ULP method below fails for numbers near 0!). |
|
22919 | 1179 |
# ifdef NO_ABSF |
22920 | 1180 |
if (fabs((double)(myself.f - otherFloat.f)) <= scaledEpsilon) { |
22919 | 1181 |
RETURN(true); |
1182 |
} |
|
1183 |
# else |
|
22853 | 1184 |
// fprintf(stderr, "%.10f %.10f\n", fabsf(myself.f - otherFloat.f), scaledEpsilon); |
1185 |
if (fabsf(myself.f - otherFloat.f) <= scaledEpsilon) { |
|
1186 |
RETURN(true); |
|
16670 | 1187 |
} |
22919 | 1188 |
#endif |
16670 | 1189 |
|
1190 |
// if the signs differ, the numbers are different |
|
1191 |
if ((myself.f >= 0) != (otherFloat.f >= 0)) { |
|
22853 | 1192 |
RETURN(false); |
16670 | 1193 |
} |
1194 |
||
1195 |
// compute the difference of the 'units in the last place" ULP |
|
1196 |
// (if ulpDiff == 1, two floats are adjecant) |
|
1197 |
ulpDiff = myself.i - otherFloat.i; |
|
1198 |
if (ulpDiff < 0) ulpDiff = -ulpDiff; |
|
1199 |
if (ulpDiff <= nEpsilon) { |
|
22853 | 1200 |
RETURN(true); |
16670 | 1201 |
} else { |
22853 | 1202 |
RETURN(false) |
16670 | 1203 |
} |
1204 |
||
1205 |
tryHarder:; |
|
1206 |
%}. |
|
1207 |
^ aNumber isAlmostEqualToFromShortFloat:self nEpsilon:nE |
|
1208 |
||
1209 |
" |
|
22853 | 1210 |
67329.234 asShortFloat isAlmostEqualTo:67329.23401 asShortFloat nEpsilon:1 |
1211 |
1.0 asShortFloat isAlmostEqualTo:1.0000001 asShortFloat nEpsilon:1 |
|
1212 |
1.0 asShortFloat isAlmostEqualTo:1.0000001 nEpsilon:1 |
|
1213 |
1.0 asShortFloat isAlmostEqualTo:-1.0 nEpsilon:1 |
|
1214 |
1.0 asShortFloat isAlmostEqualTo:1 nEpsilon:1 |
|
22922 | 1215 |
0.0 asShortFloat isAlmostEqualTo:0.0000001 asShortFloat nEpsilon:1 |
1216 |
0.0 asShortFloat isAlmostEqualTo:0.000001 asShortFloat nEpsilon:1 |
|
22853 | 1217 |
0.0 asShortFloat isAlmostEqualTo:self epsilon nEpsilon:1 |
1218 |
0.0 asShortFloat - 1.192093e-07 asShortFloat |
|
16670 | 1219 |
" |
22853 | 1220 |
|
1221 |
"Modified: / 10-05-2018 / 00:47:22 / stefan" |
|
16670 | 1222 |
! |
1223 |
||
1197 | 1224 |
~= aNumber |
1225 |
"return true, if the arguments value are not equal" |
|
1226 |
||
1227 |
%{ /* NOCONTEXT */ |
|
1228 |
||
7356 | 1229 |
if (aNumber != nil) { |
11945 | 1230 |
if (__isSmallInteger(aNumber)) { |
1231 |
RETURN ( (__shortFloatVal(self) != (float)(__intVal(aNumber))) ? true : false ); |
|
1232 |
} |
|
1233 |
if (__qIsFloatLike(aNumber)) { |
|
1234 |
RETURN ( (double)(__shortFloatVal(self) != __floatVal(aNumber)) ? true : false ); |
|
1235 |
} |
|
1236 |
if (__qIsShortFloat(aNumber)) { |
|
1237 |
RETURN ( (__shortFloatVal(self) != __shortFloatVal(aNumber)) ? true : false ); |
|
1238 |
} |
|
7356 | 1239 |
} else { |
11945 | 1240 |
RETURN ( true ); |
1197 | 1241 |
} |
1242 |
%}. |
|
7356 | 1243 |
^ super ~= aNumber |
1197 | 1244 |
! ! |
1245 |
||
13359
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1246 |
!ShortFloat methodsFor:'mathematical functions'! |
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1247 |
|
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1248 |
fastInverseSqrt |
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1249 |
"return a rough but fast approximation of (1 / self sqrt). |
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1250 |
The error is some 1%, which is ok for many 3D computations or physics simulations. |
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1251 |
Do not use this for now: it is non-portable and probably not speeding things up |
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1252 |
much, unless inlined into the sender code. |
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1253 |
The code is here as a reminder and might be later used as a hint for the inliner |
13360
1aac241c9981
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13359
diff
changeset
|
1254 |
(to speed up 3D computations, for example). |
1aac241c9981
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13359
diff
changeset
|
1255 |
see: http://betterexplained.com/articles/understanding-quakes-fast-inverse-square-root/" |
13359
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1256 |
|
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1257 |
%{ /* NOCONTEXT */ |
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1258 |
float x, rslt; |
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1259 |
OBJ newFloat; |
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1260 |
|
13361 | 1261 |
if (sizeof(float) == 4) { |
14673
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1262 |
x = __shortFloatVal(self); |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1263 |
{ |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1264 |
float xhalf = 0.5f * x; |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1265 |
int i = *(int*)&x; // store floating-point bits in integer |
13359
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1266 |
|
14673
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1267 |
i = 0x5f3759d5 - (i >> 1); // initial guess for Newton's method |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1268 |
x = *(float*)&i; // convert new bits into float |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1269 |
x = x*(1.5f - xhalf*x*x); // One round of Newton's method |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1270 |
__qMKSFLOAT(newFloat, x); |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1271 |
RETURN ( newFloat ); |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1272 |
} |
13359
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1273 |
} |
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1274 |
%}. |
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1275 |
^ 1 / self sqrt |
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1276 |
|
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1277 |
" |
14673
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1278 |
10.0 asShortFloat fastInverseSqrt |
13359
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1279 |
(1 / 10.0 asShortFloat sqrt) |
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1280 |
" |
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1281 |
|
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1282 |
" |
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1283 |
|a b t0 t1 t2| |
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1284 |
|
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1285 |
a := 345 asShortFloat. |
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1286 |
t0 := Time millisecondsToRun:[ |
14673
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1287 |
1000000 timesRepeat:[ |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1288 |
] |
13359
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1289 |
]. |
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1290 |
t1 := Time millisecondsToRun:[ |
14673
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1291 |
1000000 timesRepeat:[ |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1292 |
a fastInverseSqrt |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1293 |
] |
13359
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1294 |
]. |
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1295 |
t2 := Time millisecondsToRun:[ |
14673
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1296 |
1000000 timesRepeat:[ |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1297 |
(1 / a sqrt) |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1298 |
] |
13359
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1299 |
]. |
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1300 |
Transcript show:'empty: '; showCR:t0. |
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1301 |
Transcript show:'fast: '; showCR:t1. |
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1302 |
Transcript show:'regular: '; showCR:t2. |
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1303 |
" |
21829 | 1304 |
! |
1305 |
||
21956 | 1306 |
ln |
1307 |
"return the natural logarithm of the receiver. |
|
1308 |
Raises an exception, if the receiver is less or equal to zero." |
|
1309 |
||
1310 |
%{ /* NOCONTEXT */ |
|
1311 |
#ifndef __SCHTEAM__ |
|
1312 |
float val, rslt; |
|
1313 |
OBJ newFloat; |
|
1314 |
||
1315 |
val = __shortFloatVal(self); |
|
1316 |
||
21976 | 1317 |
/* |
1318 |
* to suppress the warnBox opened by win32 |
|
21956 | 1319 |
* to avoid returning -INF from some unix math libs (__osx__) |
1320 |
*/ |
|
1321 |
if (val > 0.0) { |
|
21976 | 1322 |
__threadErrno = 0; |
21956 | 1323 |
# ifdef NO_LOGF |
21976 | 1324 |
{ |
1325 |
double dRslt = log((double)val); |
|
1326 |
rslt = (float)dRslt; |
|
1327 |
} |
|
21956 | 1328 |
# else |
21976 | 1329 |
rslt = logf(val); |
21956 | 1330 |
# endif |
21976 | 1331 |
if (! isnanf(rslt)) /* Currently all our systems support isnan() */ |
1332 |
{ |
|
1333 |
if (__threadErrno == 0) { |
|
1334 |
__qMKSFLOAT(newFloat, rslt); |
|
1335 |
RETURN ( newFloat ); |
|
1336 |
} |
|
1337 |
} |
|
21956 | 1338 |
} |
1339 |
#endif |
|
1340 |
%}. |
|
1341 |
" |
|
1342 |
an invalid value for logarithm |
|
21972 | 1343 |
if you need -INF for a zero receiver, try Number trapInfinity:[...] |
21956 | 1344 |
" |
1345 |
^ self class |
|
21976 | 1346 |
raise:(self = 0 ifTrue:[#infiniteResultSignal] ifFalse:[#domainErrorSignal]) |
1347 |
receiver:self |
|
1348 |
selector:#ln |
|
1349 |
arguments:#() |
|
1350 |
errorString:'bad receiver in log10 (not strictly positive)' |
|
21956 | 1351 |
|
1352 |
" |
|
21976 | 1353 |
0 asFloat ln |
21965 | 1354 |
Number trapInfinity:[ 0 asFloat ln ] |
1355 |
||
21956 | 1356 |
10 asFloat ln 2.30258509299405 |
1357 |
10 asShortFloat ln 2.302585 |
|
1358 |
10 asLongFloat ln 2.302585092994045684 |
|
1359 |
10 asQDouble ln 2.30258509299404568402 |
|
1360 |
||
1361 |
50 asFloat ln 3.91202300542815 |
|
1362 |
50 asShortFloat ln 3.912023 |
|
1363 |
50 asLongFloat ln 3.912023005428146059 |
|
1364 |
50 asQDouble ln 3.91202300542814605862 |
|
1365 |
" |
|
1366 |
||
1367 |
"Created: / 03-07-2017 / 15:20:07 / cg" |
|
1368 |
! |
|
1369 |
||
21829 | 1370 |
log10 |
1371 |
"return the base-10 logarithm of the receiver. |
|
1372 |
Raises an exception, if the receiver is less or equal to zero." |
|
1373 |
||
1374 |
%{ /* NOCONTEXT */ |
|
1375 |
#ifndef __SCHTEAM__ |
|
1376 |
float val, rslt; |
|
1377 |
OBJ newFloat; |
|
1378 |
||
1379 |
val = __shortFloatVal(self); |
|
1380 |
||
21976 | 1381 |
/* |
1382 |
* to suppress the warnBox opened by win32 |
|
21955 | 1383 |
* to avoid returning -INF from some unix math libs (__osx__) |
1384 |
*/ |
|
1385 |
if (val > 0.0) { |
|
21976 | 1386 |
__threadErrno = 0; |
21834
1fe471f18a2e
oops - no log10f under win32
Claus Gittinger <cg@exept.de>
parents:
21829
diff
changeset
|
1387 |
# ifdef NO_LOG10F |
21976 | 1388 |
{ |
1389 |
double dRslt = log10((double)val); |
|
1390 |
rslt = (float)dRslt; |
|
1391 |
} |
|
21834
1fe471f18a2e
oops - no log10f under win32
Claus Gittinger <cg@exept.de>
parents:
21829
diff
changeset
|
1392 |
# else |
21976 | 1393 |
rslt = log10f(val); |
21834
1fe471f18a2e
oops - no log10f under win32
Claus Gittinger <cg@exept.de>
parents:
21829
diff
changeset
|
1394 |
# endif |
21976 | 1395 |
if (! isnanf(rslt)) /* Currently all our systems support isnan() */ |
1396 |
{ |
|
1397 |
if (__threadErrno == 0) { |
|
1398 |
__qMKSFLOAT(newFloat, rslt); |
|
1399 |
RETURN ( newFloat ); |
|
1400 |
} |
|
1401 |
} |
|
21829 | 1402 |
} |
1403 |
#endif |
|
1404 |
%}. |
|
1405 |
" |
|
1406 |
an invalid value for logarithm |
|
21972 | 1407 |
if you need -INF for a zero receiver, try Number trapInfinity:[...] |
21829 | 1408 |
" |
1409 |
^ self class |
|
21976 | 1410 |
raise:(self = 0 ifTrue:[#infiniteResultSignal] ifFalse:[#domainErrorSignal]) |
1411 |
receiver:self |
|
1412 |
selector:#log10 |
|
1413 |
arguments:#() |
|
1414 |
errorString:'bad receiver in log10 (not strictly positive)' |
|
21829 | 1415 |
|
1416 |
" |
|
21976 | 1417 |
0 asShortFloat log10 |
21965 | 1418 |
Number trapInfinity:[ 0 asShortFloat log10 ] |
1419 |
||
21829 | 1420 |
10 asFloat log10 1.0 |
1421 |
10 asShortFloat log10 1.0 |
|
1422 |
10 asLongFloat log10 1.0 |
|
1423 |
10 asQDouble log10 1.00000000000000000000000000000000000000000 |
|
1424 |
||
1425 |
50 asFloat log10 1.69897000433602 |
|
1426 |
50 asShortFloat log10 1.69897 |
|
1427 |
50 asLongFloat log10 1.698970004336018805 |
|
1428 |
50 asQDouble log10 1.69897000433601880478626110527550697323181 |
|
1429 |
" |
|
1430 |
||
1431 |
"Created: / 16-06-2017 / 10:47:53 / cg" |
|
21972 | 1432 |
"Modified (comment): / 03-07-2017 / 15:59:05 / cg" |
13359
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1433 |
! ! |
17228556cabd
comment/format in: #fastInverseSqrt
Claus Gittinger <cg@exept.de>
parents:
13358
diff
changeset
|
1434 |
|
1197 | 1435 |
!ShortFloat methodsFor:'printing & storing'! |
1436 |
||
22303 | 1437 |
printOn:aStream |
1438 |
"append a printed representation of the receiver to |
|
1439 |
the argument, aStream. |
|
1440 |
||
1441 |
I use #printString instead of #printOn: as basic print mechanism." |
|
1442 |
||
1443 |
aStream nextPutAll:self printString |
|
1444 |
||
1445 |
"Created: / 10-10-2017 / 14:05:28 / cg" |
|
1446 |
! |
|
1447 |
||
1197 | 1448 |
printString |
3194
16073834a825
Add comment about #printOn: and #printString
Stefan Vogel <sv@exept.de>
parents:
3158
diff
changeset
|
1449 |
"return a printed representation of the receiver |
16073834a825
Add comment about #printOn: and #printString
Stefan Vogel <sv@exept.de>
parents:
3158
diff
changeset
|
1450 |
LimitedPrecisonReal and its subclasses use #printString instead of |
16073834a825
Add comment about #printOn: and #printString
Stefan Vogel <sv@exept.de>
parents:
3158
diff
changeset
|
1451 |
#printOn: as basic print mechanism." |
1197 | 1452 |
|
18139 | 1453 |
^ self printStringWithFormat:DefaultPrintFormat |
1454 |
||
1455 |
" |
|
19570
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1456 |
1.234 asShortFloat printString. |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1457 |
1.0 asShortFloat printString. |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1458 |
1e10 asShortFloat printString. |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1459 |
1.2e3 asShortFloat printString. |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1460 |
1.2e30 asShortFloat printString. |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1461 |
(1.0 uncheckedDivide:0) asShortFloat printString. |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1462 |
(0.0 uncheckedDivide:0) asShortFloat printString. |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1463 |
self pi printString. |
18139 | 1464 |
|
19570
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1465 |
self pi printString. |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1466 |
DefaultPrintFormat := '.3'. |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1467 |
self pi printString. |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1468 |
DefaultPrintFormat := '.7'. |
18139 | 1469 |
|
19570
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1470 |
DecimalPointCharacterForPrinting := $,. |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1471 |
1.234 asShortFloat printString. |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1472 |
1.0 asShortFloat printString. |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1473 |
1e10 asShortFloat printString. |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1474 |
1.2e3 asShortFloat printString. |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1475 |
1.2e30 asShortFloat printString. |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1476 |
(1.0 uncheckedDivide:0) asShortFloat printString. |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1477 |
(0.0 uncheckedDivide:0) asShortFloat printString. |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1478 |
DecimalPointCharacterForPrinting := $. |
18139 | 1479 |
" |
1480 |
! |
|
1481 |
||
1482 |
printStringWithFormat:format |
|
1483 |
"return a printed representation of the receiver; |
|
1484 |
fmt must be of the form: .nn, where nn is the number of digits. |
|
1485 |
To print 6 valid digits, use printStringWithFormat:'.6' |
|
1486 |
For Floats, the default used in printString, is 15 (because its a double); |
|
1487 |
for ShortFloats, it is 6 (because it is a float)" |
|
1488 |
||
1197 | 1489 |
%{ /* NOCONTEXT */ |
1490 |
||
1491 |
char buffer[64]; |
|
1492 |
REGISTER char *cp; |
|
8418
37ccc74bbf9e
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
7746
diff
changeset
|
1493 |
int len; |
1197 | 1494 |
OBJ s; |
16670 | 1495 |
char *fmt; |
1496 |
char fmtBuffer[20]; |
|
1497 |
||
18139 | 1498 |
if (__isStringLike(format)) { |
19570
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1499 |
fmt = (char *) __stringVal(format); |
16670 | 1500 |
} else { |
19570
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1501 |
/* |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1502 |
* in case we get called with garbage... |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1503 |
*/ |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1504 |
fmt = ".7"; |
16670 | 1505 |
} |
1506 |
||
1507 |
/* |
|
1508 |
* build a printf format string |
|
1509 |
*/ |
|
1510 |
fmtBuffer[0] = '%'; |
|
1511 |
strncpy(fmtBuffer+1, fmt, 10); |
|
1512 |
strcat(fmtBuffer, "g"); |
|
1197 | 1513 |
|
1514 |
/* |
|
1515 |
* actually only needed on sparc: since thisContext is |
|
1516 |
* in a global register, which gets destroyed by printf, |
|
1517 |
* manually save it here - very stupid ... |
|
1518 |
*/ |
|
1519 |
__BEGIN_PROTECT_REGISTERS__ |
|
16670 | 1520 |
len = snprintf(buffer, sizeof(buffer), fmtBuffer, (float)__shortFloatVal(self)); |
1197 | 1521 |
__END_PROTECT_REGISTERS__ |
1522 |
||
8418
37ccc74bbf9e
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
7746
diff
changeset
|
1523 |
if (len >= 0 && len <= sizeof(buffer)-3) { |
19570
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1524 |
/* |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1525 |
* kludge to make integral float f prints as "f.0" (not as "f" as printf does) |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1526 |
* (i.e. look if string contains '.' or 'e' and append '.0' if not) |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1527 |
*/ |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1528 |
for (cp = buffer; *cp; cp++) { |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1529 |
if ((*cp == '.') || (*cp == ',') || (*cp == 'E') || (*cp == 'e')) break; |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1530 |
} |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1531 |
if (!*cp && (cp[-1] >= '0') && (cp[-1] <= '9')) { |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1532 |
if (__isCharacter(@global(DecimalPointCharacterForPrinting))) { |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1533 |
*cp++ = __intVal(__characterVal(@global(DecimalPointCharacterForPrinting))); |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1534 |
} else { |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1535 |
*cp++ = '.'; |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1536 |
} |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1537 |
*cp++ = '0'; |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1538 |
*cp = '\0'; |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1539 |
} else { |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1540 |
if (cp && (*cp == '.')) { |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1541 |
if (__isCharacter(@global(DecimalPointCharacterForPrinting))) { |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1542 |
*cp = __intVal(__characterVal(@global(DecimalPointCharacterForPrinting))); |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1543 |
} |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1544 |
} |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1545 |
} |
1197 | 1546 |
|
19570
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1547 |
s = __MKSTRING(buffer); |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1548 |
if (s != nil) { |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1549 |
RETURN (s); |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1550 |
} |
1197 | 1551 |
} |
1552 |
%}. |
|
5432 | 1553 |
^ self asFloat printString |
8418
37ccc74bbf9e
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
7746
diff
changeset
|
1554 |
|
37ccc74bbf9e
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
7746
diff
changeset
|
1555 |
" |
19570
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1556 |
1.234 asShortFloat printString. |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1557 |
ShortFloat pi printString. |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1558 |
1.0 asShortFloat printString. |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1559 |
1e10 asShortFloat printString. |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1560 |
1.2e3 asShortFloat printString. |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1561 |
1.2e30 asShortFloat printString. |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1562 |
(1.0 uncheckedDivide:0) asShortFloat printString. |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1563 |
(0.0 uncheckedDivide:0) asShortFloat printString. |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1564 |
self pi printString. |
9121
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
1565 |
|
19570
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1566 |
self pi printString. |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1567 |
DefaultPrintFormat := '.3'. |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1568 |
self pi printString. |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1569 |
DefaultPrintFormat := '.7'. |
16670 | 1570 |
|
19570
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1571 |
DecimalPointCharacterForPrinting := $,. |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1572 |
1.234 asShortFloat printString. |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1573 |
1.0 asShortFloat printString. |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1574 |
1e10 asShortFloat printString. |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1575 |
1.2e3 asShortFloat printString. |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1576 |
1.2e30 asShortFloat printString. |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1577 |
(1.0 uncheckedDivide:0) asShortFloat printString. |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1578 |
(0.0 uncheckedDivide:0) asShortFloat printString. |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1579 |
DecimalPointCharacterForPrinting := $. |
8418
37ccc74bbf9e
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
7746
diff
changeset
|
1580 |
" |
5718 | 1581 |
! |
1582 |
||
1583 |
printfPrintString:formatString |
|
7746 | 1584 |
"non-standard: return a printed representation of the receiver |
5718 | 1585 |
as specified by formatString, which is defined by printf. |
21950 | 1586 |
|
1587 |
If you use this, be aware, that the format string must be correct and something like %f. |
|
5718 | 1588 |
Also, the resulting string may not be longer than 255 bytes - |
20433 | 1589 |
since that's the (static) size of the buffer. |
21906 | 1590 |
|
11924
db93de0d761d
changed #printfPrintString:
Claus Gittinger <cg@exept.de>
parents:
11730
diff
changeset
|
1591 |
This method is NONSTANDARD and may be removed without notice. |
21976 | 1592 |
WARNNG: this goes directly to the C-printf function and may therefore me inherently unsafe. |
11924
db93de0d761d
changed #printfPrintString:
Claus Gittinger <cg@exept.de>
parents:
11730
diff
changeset
|
1593 |
Please use the printf: method, which is safe as it is completely implemented in Smalltalk." |
5718 | 1594 |
|
1595 |
%{ /* STACK: 400 */ |
|
1596 |
char buffer[256]; |
|
1597 |
OBJ s; |
|
8418
37ccc74bbf9e
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
7746
diff
changeset
|
1598 |
int len; |
5718 | 1599 |
|
12486
f1ef886832f1
change __isString() to __isStringLike() in primitive code
Stefan Vogel <sv@exept.de>
parents:
11945
diff
changeset
|
1600 |
if (__isStringLike(formatString)) { |
21976 | 1601 |
/* |
1602 |
* actually only needed on sparc: since thisContext is |
|
1603 |
* in a global register, which gets destroyed by printf, |
|
1604 |
* manually save it here - very stupid ... |
|
1605 |
*/ |
|
1606 |
__BEGIN_PROTECT_REGISTERS__ |
|
5718 | 1607 |
|
21976 | 1608 |
len = snprintf(buffer, sizeof(buffer), __stringVal(formatString), __shortFloatVal(self)); |
8418
37ccc74bbf9e
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
7746
diff
changeset
|
1609 |
|
21976 | 1610 |
__END_PROTECT_REGISTERS__ |
8418
37ccc74bbf9e
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
7746
diff
changeset
|
1611 |
|
21976 | 1612 |
if (len < 0) goto fail; |
1613 |
if (len >= sizeof(buffer)) goto fail; |
|
8418
37ccc74bbf9e
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
7746
diff
changeset
|
1614 |
|
21976 | 1615 |
s = __MKSTRING_L(buffer, len); |
1616 |
if (s != nil) { |
|
1617 |
RETURN (s); |
|
1618 |
} |
|
5718 | 1619 |
} |
8418
37ccc74bbf9e
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
7746
diff
changeset
|
1620 |
fail: ; |
5718 | 1621 |
%}. |
21906 | 1622 |
^ super printfPrintString:formatString |
5718 | 1623 |
|
1624 |
"ShortFloat pi printfPrintString:'%%lg -> %lg'" |
|
1625 |
"ShortFloat pi printfPrintString:'%%lf -> %lf'" |
|
1626 |
"ShortFloat pi printfPrintString:'%%7.5lg -> %7.5lg'" |
|
1627 |
"ShortFloat pi printfPrintString:'%%G -> %G'" |
|
1628 |
"ShortFloat pi printfPrintString:'%%F -> %F'" |
|
1629 |
"ShortFloat pi printfPrintString:'%%7.5G -> %7.5G'" |
|
1630 |
"ShortFloat pi printfPrintString:'%%7.5F -> %7.5F'" |
|
21906 | 1631 |
|
21950 | 1632 |
"Modified (comment): / 03-07-2017 / 15:12:20 / cg" |
8422
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1633 |
! |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1634 |
|
22303 | 1635 |
storeOn:aStream |
1636 |
"append a printed representation of the receiver to |
|
1637 |
the argument, aStream. |
|
1638 |
||
1639 |
I use #storeString instead of #storeOn: as basic store mechanism." |
|
1640 |
||
1641 |
aStream nextPutAll:self storeString |
|
1642 |
||
1643 |
"Created: / 10-10-2017 / 14:06:52 / cg" |
|
1644 |
! |
|
1645 |
||
8422
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1646 |
storeString |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1647 |
"return a printed representation of the receiver; |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1648 |
all valid digits are printed. |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1649 |
LimitedPrecisonReal and its subclasses use #storeString instead of |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1650 |
#storeOn: as basic print mechanism." |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1651 |
|
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1652 |
%{ /* NOCONTEXT */ |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1653 |
|
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1654 |
char buffer[64]; |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1655 |
REGISTER char *cp; |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1656 |
OBJ s; |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1657 |
int len; |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1658 |
|
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1659 |
/* |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1660 |
* build a printf format string |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1661 |
*/ |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1662 |
|
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1663 |
__BEGIN_PROTECT_REGISTERS__ |
16670 | 1664 |
len = snprintf(buffer, sizeof(buffer), "%.8g", (float)__shortFloatVal(self)); |
8422
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1665 |
__END_PROTECT_REGISTERS__ |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1666 |
|
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1667 |
if (len >= 0 && len < sizeof(buffer)-3) { |
19570
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1668 |
/* |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1669 |
* kludge to make integral float f prints as "f.0" (not as "f" as printf does) |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1670 |
* (i.e. look if string contains '.' or 'e' and append '.0' if not) |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1671 |
*/ |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1672 |
for (cp = buffer; *cp; cp++) { |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1673 |
if ((*cp == '.') || (*cp == ',') || (*cp == 'E') || (*cp == 'e')) break; |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1674 |
} |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1675 |
if (!*cp && (cp[-1] >= '0') && (cp[-1] <= '9')) { |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1676 |
*cp++ = '.'; |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1677 |
*cp++ = '0'; |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1678 |
*cp = '\0'; |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1679 |
} |
8422
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1680 |
|
19570
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1681 |
s = __MKSTRING(buffer); |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1682 |
if (s != nil) { |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1683 |
RETURN (s); |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1684 |
} |
8422
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1685 |
} |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1686 |
%}. |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1687 |
" |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1688 |
memory allocation (for the new string) failed. |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1689 |
When we arrive here, there was no memory, even after a garbage collect. |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1690 |
This means, that the VM wanted to get some more memory from the |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1691 |
OS, which was not kind enough to give it. |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1692 |
Bad luck - you should increase the swap space on your machine. |
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1693 |
" |
20706
009b8269bd08
we have class based exceptions - use them
Claus Gittinger <cg@exept.de>
parents:
20433
diff
changeset
|
1694 |
^ AllocationFailure raise. |
8422
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1695 |
|
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1696 |
" |
19570
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1697 |
0.1 asShortFloat storeString |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1698 |
((Array new:10 withAll:0.1 asShortFloat) inject:0 into:[:v :sumSoFar| sumSoFar + v]) storeString |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1699 |
1.0 asShortFloat storeString |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1700 |
1.234 asShortFloat storeString |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1701 |
1e10 asShortFloat storeString |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1702 |
1.2e3 asShortFloat storeString |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1703 |
1.2e30 asShortFloat storeString |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1704 |
Float pi asShortFloat storeString |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1705 |
(1.0 uncheckedDivide:0) asShortFloat storeString |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1706 |
(0.0 uncheckedDivide:0) asShortFloat storeString |
8422
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1707 |
|
9121
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
1708 |
notice that the storeString is NOT affected by DecimalPointCharacterForPrinting: |
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
1709 |
|
19570
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1710 |
DecimalPointCharacterForPrinting := $,. |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1711 |
1.234 asShortFloat storeString. |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1712 |
1.0 asShortFloat storeString. |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1713 |
1e10 asShortFloat storeString. |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1714 |
1.2e3 asShortFloat storeString. |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1715 |
1.2e30 asShortFloat storeString. |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1716 |
(1.0 uncheckedDivide:0) asShortFloat storeString. |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1717 |
(0.0 uncheckedDivide:0) asShortFloat storeString. |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1718 |
DecimalPointCharacterForPrinting := $. |
8422
c206b4711b6e
Define #storeString to not use DecimalPointCharacter and to
Stefan Vogel <sv@exept.de>
parents:
8418
diff
changeset
|
1719 |
" |
1197 | 1720 |
! ! |
1721 |
||
12628 | 1722 |
!ShortFloat methodsFor:'private accessing'! |
1723 |
||
1724 |
basicAt:index |
|
1725 |
"return an internal byte of the float. |
|
1726 |
The value returned here depends on byte order, float representation etc. |
|
1727 |
Therefore, this method should be used strictly private. |
|
1728 |
||
1729 |
Notice: |
|
14673
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1730 |
the need to redefine this method here is due to the |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1731 |
inability of many machines to store floats in non-double aligned memory. |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1732 |
Therefore, on some machines, the first 4 bytes of a float are left unused, |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1733 |
and the actual float is stored at index 5 .. 12. |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1734 |
To hide this at one place, this method knows about that, and returns |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1735 |
values as if this filler wasnt present." |
12628 | 1736 |
|
1737 |
%{ /* NOCONTEXT */ |
|
1738 |
||
1739 |
register int indx; |
|
1740 |
unsigned char *cp; |
|
1741 |
||
1742 |
/* |
|
1743 |
* notice the missing test for self being a nonNilObject - |
|
1744 |
* this can be done since basicAt: is defined both in UndefinedObject |
|
1745 |
* and SmallInteger |
|
1746 |
*/ |
|
1747 |
if (__isSmallInteger(index)) { |
|
14673
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1748 |
indx = __intVal(index) - 1; |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1749 |
if (((unsigned)(indx)) < sizeof(float)) { |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1750 |
cp = (unsigned char *)(& (__ShortFloatInstPtr(self)->f_floatvalue)); |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1751 |
RETURN ( __mkSmallInteger(cp[indx] & 0xFF) ); |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1752 |
} |
12628 | 1753 |
} |
1754 |
%}. |
|
1755 |
^ self indexNotIntegerOrOutOfBounds:index |
|
1756 |
! |
|
1757 |
||
1758 |
basicAt:index put:value |
|
1759 |
"set an internal byte of the float. |
|
1760 |
The value to be stored here depends on byte order, float representation etc. |
|
1761 |
Therefore, this method should be used strictly private. |
|
1762 |
||
1763 |
Notice: |
|
14673
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1764 |
the need to redefine this method here is due to the |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1765 |
inability of many machines to store floats in non-double aligned memory. |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1766 |
Therefore, on some machines, the first 4 bytes of a float are left unused, |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1767 |
and the actual float is stored at index 5 .. 12. |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1768 |
To hide this at one place, this method knows about that, and returns |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1769 |
values as if this filler wasnt present." |
12628 | 1770 |
|
1771 |
%{ /* NOCONTEXT */ |
|
1772 |
register int indx, val; |
|
1773 |
unsigned char *cp; |
|
1774 |
||
1775 |
/* |
|
1776 |
* notice the missing test for self being a nonNilObject - |
|
1777 |
* this can be done since basicAt: is defined both in UndefinedObject |
|
1778 |
* and SmallInteger |
|
1779 |
*/ |
|
1780 |
if (__bothSmallInteger(index, value)) { |
|
14673
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1781 |
val = __intVal(value); |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1782 |
if ((val & ~0xFF) == 0 /* i.e. (val >= 0) && (val <= 255) */) { |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1783 |
indx = __intVal(index) - 1; |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1784 |
if (((unsigned)(indx)) < sizeof(float)) { |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1785 |
cp = (unsigned char *)(& (__ShortFloatInstPtr(self)->f_floatvalue)); |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1786 |
cp[indx] = val; |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1787 |
RETURN ( value ); |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1788 |
} |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1789 |
} |
12628 | 1790 |
} |
1791 |
%}. |
|
1792 |
value isInteger ifFalse:[ |
|
14673
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1793 |
" |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1794 |
the object to store should be an integer number |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1795 |
" |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1796 |
^ self elementNotInteger |
12628 | 1797 |
]. |
1798 |
(value between:0 and:255) ifFalse:[ |
|
14673
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1799 |
" |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1800 |
the object to store must be a bytes value |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1801 |
" |
d3c91be01e96
setlocale only once and for all in stxmain
Claus Gittinger <cg@exept.de>
parents:
14601
diff
changeset
|
1802 |
^ self elementBoundsError:value |
12628 | 1803 |
]. |
1804 |
^ self indexNotIntegerOrOutOfBounds:index |
|
1805 |
! ! |
|
1806 |
||
22259 | 1807 |
!ShortFloat methodsFor:'private-accessing'! |
1808 |
||
1809 |
byteAt:index |
|
1810 |
^ self basicAt:index |
|
1811 |
! |
|
1812 |
||
1813 |
byteAt:index put:newByte |
|
1814 |
self shouldNotImplement |
|
1815 |
! ! |
|
1816 |
||
16670 | 1817 |
!ShortFloat methodsFor:'queries'! |
1818 |
||
1819 |
nextFloat:count |
|
1820 |
"answer the next float count places after (or before if count is negative) myself" |
|
1821 |
||
1822 |
%{ |
|
1823 |
union u { |
|
19570
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1824 |
float d; |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1825 |
INT32 i; |
16670 | 1826 |
} this; |
1827 |
||
1828 |
if (__isSmallInteger(count)) { |
|
19570
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1829 |
this.d = __shortFloatVal(self); |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1830 |
if (isfinite(this.d)) |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1831 |
this.i += __intVal(count); |
16670 | 1832 |
|
19570
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
1833 |
RETURN(__MKSFLOAT(this.d)); |
16670 | 1834 |
} |
1835 |
%}. |
|
1836 |
self primitiveFailed:#badArgument |
|
1837 |
||
1838 |
" |
|
1839 |
(1.0 asShortFloat nextFloat:2) storeString |
|
1840 |
(67329.234 asShortFloat nextFloat:1) storeString |
|
1841 |
ShortFloat NaN nextFloat:100000 |
|
1842 |
ShortFloat infinity nextFloat:100000 |
|
1843 |
" |
|
1844 |
! ! |
|
1845 |
||
3897 | 1846 |
!ShortFloat methodsFor:'special access'! |
1847 |
||
1848 |
exponent |
|
21850 | 1849 |
"extract a normalized float's exponent. |
3897 | 1850 |
The returned value depends on the float-representation of |
1851 |
the underlying machine and is therefore highly unportable. |
|
1852 |
This is not for general use. |
|
1853 |
This assumes that the mantissa is normalized to |
|
21850 | 1854 |
0.5 .. 1.0 and the float's value is mantissa * 2^exp" |
3897 | 1855 |
|
1856 |
%{ /* NOCONTEXT */ |
|
8913 | 1857 |
int exp; |
3897 | 1858 |
|
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8706
diff
changeset
|
1859 |
#if defined(__i386__) && defined(__GNUC__) |
7356 | 1860 |
frexpf( __shortFloatVal(self), &exp); |
1861 |
#else |
|
1862 |
frexp( (double)(__shortFloatVal(self)), &exp); |
|
1863 |
#endif |
|
14744 | 1864 |
RETURN (__mkSmallInteger(exp)); |
3897 | 1865 |
%}. |
21850 | 1866 |
^ super exponent |
3897 | 1867 |
|
1868 |
" |
|
11945 | 1869 |
4.0 asShortFloat exponent |
1870 |
2.0 asShortFloat exponent |
|
1871 |
1.0 asShortFloat exponent |
|
1872 |
0.5 asShortFloat exponent |
|
1873 |
0.25 asShortFloat exponent |
|
1874 |
0.00000011111 asShortFloat exponent |
|
3897 | 1875 |
" |
21850 | 1876 |
|
1877 |
"Modified: / 20-06-2017 / 11:33:41 / cg" |
|
3897 | 1878 |
! |
1879 |
||
1880 |
mantissa |
|
1881 |
"extract a normalized floats mantissa. |
|
1882 |
The returned value depends on the float-representation of |
|
1883 |
the underlying machine and is therefore highly unportable. |
|
1884 |
This is not for general use. |
|
1885 |
This assumes that the mantissa is normalized to |
|
1886 |
0.5 .. 1.0 and the floats value is mantissa * 2^exp" |
|
1887 |
||
1888 |
%{ /* NOCONTEXT */ |
|
21850 | 1889 |
float myVal; |
8913 | 1890 |
int exp; |
3897 | 1891 |
|
21850 | 1892 |
myVal = __shortFloatVal(self); |
1893 |
// ouch: math libs seem to not care for NaN here; |
|
1894 |
#if 1 |
|
1895 |
// should we? |
|
21868 | 1896 |
if (!isnanf(myVal) && isfinitef(myVal)) |
21850 | 1897 |
#endif |
1898 |
{ |
|
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8706
diff
changeset
|
1899 |
#if defined(__i386__) && defined(__GNUC__) |
21868 | 1900 |
float frac = frexpf(myVal, &exp); |
1901 |
RETURN (__MKSFLOAT(frac)); |
|
7356 | 1902 |
#else |
21868 | 1903 |
double frac = frexp( (double)(myVal), &exp); |
1904 |
RETURN (__MKFLOAT(frac)); |
|
7356 | 1905 |
#endif |
21850 | 1906 |
} |
3897 | 1907 |
%}. |
20008 | 1908 |
^ super mantissa |
20706
009b8269bd08
we have class based exceptions - use them
Claus Gittinger <cg@exept.de>
parents:
20433
diff
changeset
|
1909 |
|
3897 | 1910 |
" |
11945 | 1911 |
1.0 asShortFloat exponent |
3897 | 1912 |
1.0 asShortFloat mantissa |
21850 | 1913 |
1e1000 asShortFloat mantissa |
3897 | 1914 |
|
11945 | 1915 |
0.5 asShortFloat exponent |
1916 |
0.5 asShortFloat mantissa |
|
3897 | 1917 |
|
11945 | 1918 |
0.25 asShortFloat exponent |
1919 |
0.25 asShortFloat mantissa |
|
3897 | 1920 |
|
11945 | 1921 |
0.00000011111 asShortFloat exponent |
1922 |
0.00000011111 asShortFloat mantissa |
|
3897 | 1923 |
" |
21850 | 1924 |
|
1925 |
"Modified: / 20-06-2017 / 11:41:11 / cg" |
|
3897 | 1926 |
! ! |
1927 |
||
1197 | 1928 |
!ShortFloat methodsFor:'testing'! |
1929 |
||
3147 | 1930 |
isFinite |
11945 | 1931 |
"return true, if the receiver is a finite float |
3147 | 1932 |
i.e. not NaN and not infinite." |
1933 |
||
1934 |
%{ /* NOCONTEXT */ |
|
8980
abee9fcb0385
cleanup isfinite() and isnan() - usage (HPUX)
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
1935 |
float fV = __shortFloatVal(self); |
3147 | 1936 |
|
3198
4f930c1c96e7
care for systems which do not define finite() / isnan() - sigh
Claus Gittinger <cg@exept.de>
parents:
3194
diff
changeset
|
1937 |
/* |
4f930c1c96e7
care for systems which do not define finite() / isnan() - sigh
Claus Gittinger <cg@exept.de>
parents:
3194
diff
changeset
|
1938 |
* notice: on machines which do not provide |
11945 | 1939 |
* a isfinite() macro or function (WIN32), |
4305
e71100d13b67
void possible return warning
Claus Gittinger <cg@exept.de>
parents:
4297
diff
changeset
|
1940 |
* this may always ret true here ... |
3198
4f930c1c96e7
care for systems which do not define finite() / isnan() - sigh
Claus Gittinger <cg@exept.de>
parents:
3194
diff
changeset
|
1941 |
*/ |
9121
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
1942 |
if (isfinitef(fV)) { RETURN (true); } |
3147 | 1943 |
%}. |
8980
abee9fcb0385
cleanup isfinite() and isnan() - usage (HPUX)
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
1944 |
^ false |
3147 | 1945 |
|
1946 |
" |
|
11945 | 1947 |
1.0 asShortFloat isFinite |
1948 |
(0.0 asShortFloat uncheckedDivide: 0.0) isFinite |
|
1949 |
(1.0 asShortFloat uncheckedDivide: 0.0) isFinite |
|
3147 | 1950 |
" |
1951 |
! |
|
1952 |
||
2387 | 1953 |
isNaN |
1954 |
"return true, if the receiver is an invalid float (NaN - not a number). |
|
1955 |
These are not created by ST/X float operations (they raise an exception); |
|
1956 |
however, inline C-code could produce them ..." |
|
1957 |
||
1958 |
%{ /* NOCONTEXT */ |
|
9121
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
1959 |
float sV = __shortFloatVal(self); |
3139 | 1960 |
|
3198
4f930c1c96e7
care for systems which do not define finite() / isnan() - sigh
Claus Gittinger <cg@exept.de>
parents:
3194
diff
changeset
|
1961 |
/* |
4f930c1c96e7
care for systems which do not define finite() / isnan() - sigh
Claus Gittinger <cg@exept.de>
parents:
3194
diff
changeset
|
1962 |
* notice: on machines which do not provide |
11945 | 1963 |
* a isnan() macro or function (WIN32), |
4305
e71100d13b67
void possible return warning
Claus Gittinger <cg@exept.de>
parents:
4297
diff
changeset
|
1964 |
* this may always ret false here ... |
3198
4f930c1c96e7
care for systems which do not define finite() / isnan() - sigh
Claus Gittinger <cg@exept.de>
parents:
3194
diff
changeset
|
1965 |
*/ |
9121
561e28aa22f9
fixed: isNaN and isFinite (for WIN32)
Claus Gittinger <cg@exept.de>
parents:
8984
diff
changeset
|
1966 |
if (isnanf(sV)) { RETURN (true); } |
2392 | 1967 |
%}. |
1968 |
^ false |
|
2387 | 1969 |
|
3147 | 1970 |
" |
7356 | 1971 |
1.0 asShortFloat isNaN |
11945 | 1972 |
(0.0 asShortFloat uncheckedDivide: 0.0) isNaN |
1973 |
(0.0 asShortFloat uncheckedDivide: 0.0 asShortFloat) isNaN |
|
3147 | 1974 |
" |
2387 | 1975 |
! |
1976 |
||
6890
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6574
diff
changeset
|
1977 |
isNegativeZero |
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6574
diff
changeset
|
1978 |
"many systems have two float.Pnt zeros" |
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6574
diff
changeset
|
1979 |
|
17396 | 1980 |
%{ /* NOCONTEXT */ |
17394 | 1981 |
#if defined(__BORLANDC__) |
1982 |
union { float f; int i; } __u; |
|
1983 |
__u.f = __shortFloatVal(self); |
|
1984 |
RETURN ( (__u.f == 0.0 && __u.i < 0) ? true : false ); |
|
17391 | 1985 |
#else |
17382 | 1986 |
RETURN ( (__shortFloatVal(self) == 0.0 && signbit(__shortFloatVal(self)) != 0) ? true : false ); |
17391 | 1987 |
#endif |
6890
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6574
diff
changeset
|
1988 |
%}. |
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6574
diff
changeset
|
1989 |
|
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6574
diff
changeset
|
1990 |
" |
11945 | 1991 |
0.0 asShortFloat isNegativeZero |
1992 |
-0.0 asShortFloat isNegativeZero |
|
6890
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6574
diff
changeset
|
1993 |
" |
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6574
diff
changeset
|
1994 |
! |
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6574
diff
changeset
|
1995 |
|
1197 | 1996 |
negative |
17382 | 1997 |
"return true if the receiver is less than zero. |
1998 |
-0.0 is positive for now." |
|
1197 | 1999 |
|
2000 |
%{ /* NOCONTEXT */ |
|
2001 |
||
17382 | 2002 |
RETURN ( __shortFloatVal(self) < 0.0 ? true : false ); |
2003 |
// RETURN ( signbit(__shortFloatVal(self)) != 0 ? true : false ); |
|
5411 | 2004 |
%}. |
17382 | 2005 |
|
2006 |
" |
|
19570
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
2007 |
0.0 asShortFloat negative |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
2008 |
-0.0 asShortFloat negative |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
2009 |
1.0 asShortFloat negative |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
2010 |
-1.0 asShortFloat negative |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
2011 |
(1.0 uncheckedDivide: 0.0) asShortFloat negative |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
2012 |
(-1.0 uncheckedDivide: 0.0) asShortFloat negative |
17382 | 2013 |
" |
1197 | 2014 |
! |
2015 |
||
5357 | 2016 |
numberOfBits |
2017 |
"return the size (in bits) of the real; |
|
2018 |
typically, 32 is returned here, |
|
2019 |
but who knows ..." |
|
2020 |
||
2021 |
%{ /* NOCONTEXT */ |
|
2022 |
||
8913 | 2023 |
RETURN (__mkSmallInteger (sizeof(float) * 8)); |
5357 | 2024 |
%} |
2025 |
||
2026 |
" |
|
11945 | 2027 |
1.2 numberOfBits |
2028 |
1.2 asShortFloat numberOfBits |
|
5357 | 2029 |
" |
2030 |
||
2031 |
! |
|
2032 |
||
1197 | 2033 |
positive |
18852 | 2034 |
"return true if the receiver is greater or equal to zero (not negative) |
17382 | 2035 |
0.0 and -0.0 are positive for now." |
1197 | 2036 |
|
2037 |
%{ /* NOCONTEXT */ |
|
2038 |
||
17382 | 2039 |
RETURN ( __shortFloatVal(self) >= 0.0 ? true : false ); |
2040 |
// RETURN ( (signbit(__shortFloatVal(self)) == 0 ? true : false ) ); |
|
2041 |
%}. |
|
2042 |
||
2043 |
" |
|
19570
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
2044 |
0.0 asShortFloat positive |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
2045 |
-0.0 asShortFloat positive |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
2046 |
1.0 asShortFloat positive |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
2047 |
-1.0 asShortFloat positive |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
2048 |
(1.0 uncheckedDivide: 0.0) asShortFloat positive |
017c53457288
lazy computation of epsilon
Claus Gittinger <cg@exept.de>
parents:
19325
diff
changeset
|
2049 |
(-1.0 uncheckedDivide: 0.0) asShortFloat positive |
17382 | 2050 |
" |
1880 | 2051 |
! |
2052 |
||
2053 |
strictlyPositive |
|
2054 |
"return true if the receiver is greater than zero" |
|
2055 |
||
2056 |
%{ /* NOCONTEXT */ |
|
2057 |
||
2058 |
RETURN ( (__shortFloatVal(self) > 0.0) ? true : false ); |
|
2059 |
%} |
|
1197 | 2060 |
! ! |
2061 |
||
5552 | 2062 |
!ShortFloat methodsFor:'truncation & rounding'! |
3897 | 2063 |
|
7356 | 2064 |
ceiling |
2065 |
"return the smallest integer which is greater or equal to the receiver." |
|
2066 |
||
2067 |
|val| |
|
2068 |
||
2069 |
%{ |
|
2070 |
float fVal; |
|
2071 |
||
22788 | 2072 |
#if (defined(__i386__) || defined(__x86_64__)) && defined(__GNUC__) |
7356 | 2073 |
fVal = ceilf(__shortFloatVal(self)); |
2074 |
#else |
|
2075 |
fVal = (float)(ceil((double)__shortFloatVal(self))); |
|
2076 |
#endif |
|
2077 |
/* |
|
2078 |
* ST-80 (and X3J20) returns integer. |
|
2079 |
*/ |
|
2080 |
if ((fVal >= (float)_MIN_INT) && (fVal <= (float)_MAX_INT)) { |
|
22788 | 2081 |
RETURN ( __mkSmallInteger( (INT) fVal ) ); |
7356 | 2082 |
} |
2083 |
__qMKSFLOAT(val, fVal); |
|
2084 |
%}. |
|
2085 |
^ val asInteger |
|
2086 |
||
2087 |
" |
|
11945 | 2088 |
0.5 asShortFloat ceiling |
2089 |
-0.5 asShortFloat ceiling |
|
7356 | 2090 |
" |
2091 |
! |
|
2092 |
||
2093 |
ceilingAsFloat |
|
2094 |
"return the smallest integer-valued float greater or equal to the receiver. |
|
11945 | 2095 |
This is much like #ceiling, but avoids a (possibly expensive) conversion |
7356 | 2096 |
of the result to an integer. |
2097 |
It may be useful, if the result is to be further used in another float-operation." |
|
2098 |
||
2099 |
%{ /* NOCONTEXT */ |
|
2100 |
float fVal; |
|
2101 |
OBJ v; |
|
2102 |
||
22788 | 2103 |
#if (defined(__i386__) || defined(__x86_64__)) && defined(__GNUC__) |
7356 | 2104 |
fVal = ceilf(__shortFloatVal(self)); |
2105 |
#else |
|
2106 |
fVal = (float) ceil((double)__shortFloatVal(self)); |
|
2107 |
#endif |
|
2108 |
__qMKSFLOAT(v, fVal); |
|
2109 |
RETURN (v); |
|
2110 |
%} |
|
2111 |
" |
|
11945 | 2112 |
0.5 asShortFloat ceilingAsFloat |
2113 |
-0.5 asShortFloat ceilingAsFloat |
|
2114 |
-1.5 asShortFloat ceilingAsFloat |
|
7356 | 2115 |
" |
2116 |
! |
|
2117 |
||
4100
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
2118 |
floor |
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
2119 |
"return the integer nearest the receiver towards negative infinity." |
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
2120 |
|
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
2121 |
|val| |
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
2122 |
|
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
2123 |
%{ |
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
2124 |
float fVal; |
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
2125 |
|
22788 | 2126 |
#if (defined(__i386__) || defined(__x86_64__)) && defined(__GNUC__) |
7356 | 2127 |
fVal = floorf(__shortFloatVal(self)); |
2128 |
#else |
|
2129 |
fVal = (float)(floor((double)__shortFloatVal(self))); |
|
2130 |
#endif |
|
4100
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
2131 |
/* |
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
2132 |
* ST-80 (and X3J20) returns integer. |
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
2133 |
*/ |
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
2134 |
if ((fVal >= (float)_MIN_INT) && (fVal <= (float)_MAX_INT)) { |
22788 | 2135 |
RETURN ( __mkSmallInteger( (INT) fVal ) ); |
4100
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
2136 |
} |
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
2137 |
__qMKSFLOAT(val, fVal); |
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
2138 |
%}. |
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
2139 |
^ val asInteger |
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
2140 |
|
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
2141 |
" |
11945 | 2142 |
0.5 asShortFloat floor |
2143 |
-0.5 asShortFloat floor |
|
4100
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
2144 |
" |
7356 | 2145 |
! |
4100
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
2146 |
|
7356 | 2147 |
floorAsFloat |
2148 |
"return the float which represents the next lower |
|
2149 |
integer nearest the receiver towards negative infinity. |
|
2150 |
Much like floor, but returns a float result - useful if the result |
|
2151 |
will be used in another float operation, to avoid costy int-conversion." |
|
4100
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
2152 |
|
7356 | 2153 |
%{ /* NOCONTEXT */ |
2154 |
float fVal; |
|
2155 |
OBJ v; |
|
2156 |
||
22788 | 2157 |
#if (defined(__i386__) || defined(__x86_64__)) && defined(__GNUC__) |
7356 | 2158 |
fVal = floorf(__shortFloatVal(self)); |
2159 |
#else |
|
2160 |
fVal = (float)(floor((double)__shortFloatVal(self))); |
|
2161 |
#endif |
|
2162 |
__qMKSFLOAT(v, fVal); |
|
2163 |
RETURN ( v ); |
|
2164 |
%} |
|
2165 |
||
2166 |
" |
|
11945 | 2167 |
0.5 asShortFloat floorAsFloat |
2168 |
-0.5 asShortFloat floorAsFloat |
|
7356 | 2169 |
" |
4100
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
2170 |
! |
a605c54bf9a9
fixed floor (endless recursion)
Claus Gittinger <cg@exept.de>
parents:
3983
diff
changeset
|
2171 |
|
3897 | 2172 |
fractionPart |
2173 |
"extract the after-decimal fraction part. |
|
7356 | 2174 |
such that (self truncated + self fractionPart) = self" |
3897 | 2175 |
|
2176 |
%{ /* NOCONTEXT */ |
|
14727
10825e6940d3
no modff, no fmodf with bcc
Claus Gittinger <cg@exept.de>
parents:
14719
diff
changeset
|
2177 |
#ifdef NO_MODFF |
10825e6940d3
no modff, no fmodf with bcc
Claus Gittinger <cg@exept.de>
parents:
14719
diff
changeset
|
2178 |
double dFrac, dTrunc; |
10825e6940d3
no modff, no fmodf with bcc
Claus Gittinger <cg@exept.de>
parents:
14719
diff
changeset
|
2179 |
#else |
14741 | 2180 |
# ifndef __VISUALC__ |
14727
10825e6940d3
no modff, no fmodf with bcc
Claus Gittinger <cg@exept.de>
parents:
14719
diff
changeset
|
2181 |
float modff(float, float *); |
14741 | 2182 |
# endif |
14727
10825e6940d3
no modff, no fmodf with bcc
Claus Gittinger <cg@exept.de>
parents:
14719
diff
changeset
|
2183 |
#endif |
14718 | 2184 |
float frac, trunc; |
3897 | 2185 |
|
5120
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
4825
diff
changeset
|
2186 |
__threadErrno = 0; |
14727
10825e6940d3
no modff, no fmodf with bcc
Claus Gittinger <cg@exept.de>
parents:
14719
diff
changeset
|
2187 |
#ifdef NO_MODFF |
10825e6940d3
no modff, no fmodf with bcc
Claus Gittinger <cg@exept.de>
parents:
14719
diff
changeset
|
2188 |
dFrac = modf((double)__shortFloatVal(self), &dTrunc) ; |
10825e6940d3
no modff, no fmodf with bcc
Claus Gittinger <cg@exept.de>
parents:
14719
diff
changeset
|
2189 |
frac = (float)dFrac; |
10825e6940d3
no modff, no fmodf with bcc
Claus Gittinger <cg@exept.de>
parents:
14719
diff
changeset
|
2190 |
#else |
14718 | 2191 |
frac = modff(__shortFloatVal(self), &trunc); |
14727
10825e6940d3
no modff, no fmodf with bcc
Claus Gittinger <cg@exept.de>
parents:
14719
diff
changeset
|
2192 |
#endif |
11945 | 2193 |
if (! isnan(frac)) { |
14719 | 2194 |
if (__threadErrno == 0) { |
2195 |
RETURN (__MKSFLOAT(frac)); |
|
2196 |
} |
|
3897 | 2197 |
} |
2198 |
%}. |
|
7403 | 2199 |
^ self class |
14719 | 2200 |
raise:#domainErrorSignal |
2201 |
receiver:self |
|
2202 |
selector:#fractionPart |
|
2203 |
arguments:#() |
|
2204 |
errorString:'bad receiver in fractionPart' |
|
3897 | 2205 |
|
2206 |
" |
|
11945 | 2207 |
1.6 asShortFloat fractionPart + 1.6 asShortFloat truncated |
2208 |
-1.6 asShortFloat fractionPart + -1.6 asShortFloat truncated |
|
7356 | 2209 |
|
14718 | 2210 |
1.0 asShortFloat fractionPart |
2211 |
0.5 asShortFloat fractionPart |
|
2212 |
0.25 asShortFloat fractionPart |
|
2213 |
3.14159 asShortFloat fractionPart |
|
2214 |
12345673.14159 asShortFloat fractionPart |
|
2215 |
123456731231231231.14159 asShortFloat fractionPart |
|
3897 | 2216 |
" |
7356 | 2217 |
! |
3897 | 2218 |
|
7356 | 2219 |
rounded |
2220 |
"return the receiver rounded to the nearest integer" |
|
2221 |
||
2222 |
|val| |
|
2223 |
||
11945 | 2224 |
%{ |
7356 | 2225 |
float fVal; |
2226 |
||
2227 |
fVal = __shortFloatVal(self); |
|
22788 | 2228 |
#if (defined(__i386__) || defined(__x86_64__)) && defined(__GNUC__) |
7356 | 2229 |
if (fVal < 0.0) { |
22788 | 2230 |
fVal = ceilf(fVal - (float)0.5); |
7356 | 2231 |
} else { |
22788 | 2232 |
fVal = floorf(fVal + (float)0.5); |
7356 | 2233 |
} |
2234 |
#else |
|
2235 |
if (fVal < 0.0) { |
|
22788 | 2236 |
fVal = (float)ceil((double)fVal - 0.5); |
7356 | 2237 |
} else { |
22788 | 2238 |
fVal = (float)floor((double)fVal + 0.5); |
7356 | 2239 |
} |
2240 |
#endif |
|
2241 |
/* |
|
22788 | 2242 |
* ST-80 (and X3J20) return integer. |
7356 | 2243 |
*/ |
2244 |
if ((fVal >= (float)_MIN_INT) && (fVal <= (float)_MAX_INT)) { |
|
22788 | 2245 |
RETURN ( __mkSmallInteger( (INT) fVal ) ); |
7356 | 2246 |
} |
2247 |
__qMKSFLOAT(val, fVal); |
|
2248 |
%}. |
|
2249 |
^ val asInteger |
|
2250 |
||
2251 |
" |
|
11945 | 2252 |
0.4 asShortFloat rounded |
2253 |
0.5 asShortFloat rounded |
|
2254 |
0.6 asShortFloat rounded |
|
2255 |
-0.4 asShortFloat rounded |
|
2256 |
-0.5 asShortFloat rounded |
|
2257 |
-0.6 asShortFloat rounded |
|
22788 | 2258 |
1e32 asShortFloat rounded |
7356 | 2259 |
" |
2260 |
! |
|
2261 |
||
2262 |
roundedAsFloat |
|
2263 |
"return the receiver rounded to the nearest integer as a float. |
|
11945 | 2264 |
This is much like #rounded, but avoids a (possibly expensive) conversion |
7356 | 2265 |
of the result to an integer. |
2266 |
It may be useful, if the result is to be further used in another float-operation." |
|
2267 |
||
2268 |
%{ /* NOCONTEXT */ |
|
2269 |
float fVal; |
|
2270 |
OBJ v; |
|
2271 |
||
2272 |
fVal = __shortFloatVal(self); |
|
22788 | 2273 |
#if (defined(__i386__) || defined(__x86_64__)) && defined(__GNUC__) |
7356 | 2274 |
if (fVal < 0.0) { |
22788 | 2275 |
fVal = ceilf(fVal - (float)0.5); |
7356 | 2276 |
} else { |
22788 | 2277 |
fVal = floorf(fVal + (float)0.5); |
7356 | 2278 |
} |
2279 |
#else |
|
2280 |
if (fVal < 0.0) { |
|
22788 | 2281 |
fVal = (float)ceil((double)fVal - 0.5); |
7356 | 2282 |
} else { |
22788 | 2283 |
fVal = (float)floor((double)fVal + 0.5); |
7356 | 2284 |
} |
2285 |
#endif |
|
2286 |
__qMKSFLOAT(v, fVal); |
|
2287 |
RETURN (v); |
|
2288 |
%} |
|
2289 |
||
2290 |
" |
|
11945 | 2291 |
0.4 asShortFloat rounded |
2292 |
0.5 asShortFloat rounded |
|
2293 |
0.6 asShortFloat rounded |
|
2294 |
-0.4 asShortFloat rounded |
|
2295 |
-0.5 asShortFloat rounded |
|
2296 |
-0.6 asShortFloat rounded |
|
7356 | 2297 |
|
11945 | 2298 |
0.4 asShortFloat roundedAsFloat |
2299 |
0.5 asShortFloat roundedAsFloat |
|
2300 |
0.6 asShortFloat roundedAsFloat |
|
2301 |
-0.4 asShortFloat roundedAsFloat |
|
2302 |
-0.5 asShortFloat roundedAsFloat |
|
2303 |
-0.6 asShortFloat roundedAsFloat |
|
7356 | 2304 |
" |
2305 |
! |
|
2306 |
||
2307 |
truncated |
|
2308 |
"return the receiver truncated towards zero as an integer" |
|
2309 |
||
2310 |
|val| |
|
2311 |
||
11945 | 2312 |
%{ |
7356 | 2313 |
float fVal; |
2314 |
||
2315 |
fVal = __shortFloatVal(self); |
|
22788 | 2316 |
#if (defined(__i386__) || defined(__x86_64__)) && defined(__GNUC__) |
7356 | 2317 |
if (fVal < 0.0) { |
22788 | 2318 |
fVal = ceilf(fVal); |
7356 | 2319 |
} else { |
22788 | 2320 |
fVal = floorf(fVal); |
7356 | 2321 |
} |
2322 |
#else |
|
2323 |
if (fVal < 0.0) { |
|
22788 | 2324 |
fVal = (float)ceil((double)fVal); |
7356 | 2325 |
} else { |
22788 | 2326 |
fVal = (float)floor((double)fVal); |
7356 | 2327 |
} |
2328 |
#endif |
|
2329 |
/* |
|
2330 |
* ST-80 (and X3J20) returns integer. |
|
2331 |
*/ |
|
2332 |
if ((fVal >= (float)_MIN_INT) && (fVal <= (float)_MAX_INT)) { |
|
22788 | 2333 |
RETURN ( __mkSmallInteger( (INT) fVal ) ); |
7356 | 2334 |
} |
2335 |
__qMKSFLOAT(val, fVal); |
|
2336 |
%}. |
|
2337 |
^ val asInteger |
|
2338 |
||
2339 |
" |
|
11945 | 2340 |
0.5 asShortFloat truncated |
2341 |
-0.5 asShortFloat truncated |
|
2342 |
0.5 asShortFloat truncatedAsFloat |
|
2343 |
-0.5 asShortFloat truncatedAsFloat |
|
7356 | 2344 |
" |
2345 |
! |
|
2346 |
||
2347 |
truncatedAsFloat |
|
2348 |
"return the receiver truncated towards zero as a float. |
|
11945 | 2349 |
This is much like #truncated, but avoids a (possibly expensive) conversion |
7356 | 2350 |
of the result to an integer. |
11945 | 2351 |
It may be useful, if the result is to be further used in another |
7356 | 2352 |
float-operation." |
2353 |
||
2354 |
%{ /* NOCONTEXT */ |
|
2355 |
float fVal; |
|
2356 |
OBJ v; |
|
2357 |
||
2358 |
fVal = __shortFloatVal(self); |
|
22788 | 2359 |
#if (defined(__i386__) || defined(xx__x86_64__)) && defined(__GNUC__) |
7356 | 2360 |
if (fVal < 0.0) { |
22788 | 2361 |
fVal = ceilf(fVal); |
7356 | 2362 |
} else { |
22788 | 2363 |
fVal = floorf(fVal); |
7356 | 2364 |
} |
2365 |
#else |
|
2366 |
if (fVal < 0.0) { |
|
22788 | 2367 |
fVal = (float)ceil((double)fVal); |
7356 | 2368 |
} else { |
22788 | 2369 |
fVal = (float)floor((double)fVal); |
7356 | 2370 |
} |
2371 |
#endif |
|
2372 |
__qMKSFLOAT(v, fVal); |
|
2373 |
RETURN (v); |
|
2374 |
%} |
|
2375 |
||
2376 |
" |
|
11945 | 2377 |
0.5 asShortFloat truncated |
2378 |
-0.5 asShortFloat truncated |
|
2379 |
0.5 asShortFloat truncatedAsFloat |
|
2380 |
-0.5 asShortFloat truncatedAsFloat |
|
7356 | 2381 |
" |
3897 | 2382 |
! ! |
2383 |
||
1880 | 2384 |
!ShortFloat class methodsFor:'documentation'! |
1197 | 2385 |
|
2386 |
version |
|
18841 | 2387 |
^ '$Header$' |
12486
f1ef886832f1
change __isString() to __isStringLike() in primitive code
Stefan Vogel <sv@exept.de>
parents:
11945
diff
changeset
|
2388 |
! |
f1ef886832f1
change __isString() to __isStringLike() in primitive code
Stefan Vogel <sv@exept.de>
parents:
11945
diff
changeset
|
2389 |
|
f1ef886832f1
change __isString() to __isStringLike() in primitive code
Stefan Vogel <sv@exept.de>
parents:
11945
diff
changeset
|
2390 |
version_CVS |
18841 | 2391 |
^ '$Header$' |
1197 | 2392 |
! ! |
15012 | 2393 |
|
16670 | 2394 |
|
2395 |
ShortFloat initialize! |