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