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