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