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