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