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