tagged release 0.6.4
[parrot.git] / languages / WMLScript / runtime / wmlsfloat.pir
blobad271c42e6e65093483fef1449089130457db399
1 # Copyright (C) 2006-2008, The Perl Foundation.
2 # $Id$
4 =head1 NAME
6 runtime/wmlsfloat.pir - WMLScript Float library
8 =head1 DESCRIPTION
10 This library contains a set of typical arithmetic floating-point functions that
11 are frequently used by applications.
13 See "WMLScript Standard Libraries Specification", section 8 "Float".
15 =head1 FUNCTIONS
17 =cut
19 .loadlib 'wmls_ops'
20 .HLL 'WMLScript', 'wmls_group'
23 .sub 'getFloat'
24     new $P0, 'Hash'
26     .const .Sub _float_int = '_float_int'
27     $P0[0]  = _float_int
28     .const .Sub _float_floor = '_float_floor'
29     $P0[1]  = _float_floor
30     .const .Sub _float_ceil = '_float_ceil'
31     $P0[2]  = _float_ceil
32     .const .Sub _float_pow = '_float_pow'
33     $P0[3]  = _float_pow
34     .const .Sub _float_round = '_float_round'
35     $P0[4]  = _float_round
36     .const .Sub _float_sqrt = '_float_sqrt'
37     $P0[5]  = _float_sqrt
38     .const .Sub _float_maxFloat = '_float_maxFloat'
39     $P0[6]  = _float_maxFloat
40     .const .Sub _float_minFloat = '_float_minFloat'
41     $P0[7]  = _float_minFloat
43     .return ($P0)
44 .end
47 =head2 C<int(value)>
49 =head3 DESCRIPTION
51 Returns the integer part of the given value. If the value is already an integer,
52 the result is the value itself.
54 =head3 PARAMETERS
56 value = Number
58 =head3 RETURN VALUE
60 Integer or invalid.
62 =cut
64 .sub '_float_int' :anon
65     .param pmc value
66     .local pmc res
67     $P0 = value
68     $I0 = isa value, 'WmlsString'
69     unless $I0 goto L1
70     $P0 = value.'parseNumber'()
71   L1:
72     $I0 = isa $P0, 'WmlsInvalid'
73     if $I0 goto L2
74     $I0 = $P0
75     new res, 'WmlsInteger'
76     set res, $I0
77     goto L3
78   L2:
79     new res, 'WmlsInvalid'
80   L3:
81     .return (res)
82 .end
85 =head2 C<floor(value)>
87 =head3 DESCRIPTION
89 Returns the greatest integer value that is not greater than the given value. If
90 the value is already an integer, the result is the value itself.
92 =head3 PARAMETERS
94 value = Number
96 =head3 RETURN VALUE
98 Integer or invalid.
100 =cut
102 .sub '_float_floor' :anon
103     .param pmc value
104     .local pmc res
105     $P0 = value
106     $I0 = isa value, 'WmlsString'
107     unless $I0 goto L1
108     $P0 = value.'parseNumber'()
109   L1:
110     $I0 = isa $P0, 'WmlsInvalid'
111     if $I0 goto L2
112     $N0 = $P0
113     $I0 = floor $N0
114     new res, 'WmlsInteger'
115     set res, $I0
116     goto L3
117   L2:
118     new res, 'WmlsInvalid'
119   L3:
120     .return (res)
121 .end
124 =head2 C<ceil(value)>
126 =head3 DESCRIPTION
128 Returns the smallest integer value that is not less than the given value. If
129 the value is already an integer, the result is the value itself.
131 =head3 PARAMETERS
133 value = Number
135 =head3 RETURN VALUE
137 Integer or invalid.
139 =cut
141 .sub '_float_ceil' :anon
142     .param pmc value
143     .local pmc res
144     $P0 = value
145     $I0 = isa value, 'WmlsString'
146     unless $I0 goto L1
147     $P0 = value.'parseNumber'()
148   L1:
149     $I0 = isa $P0, 'WmlsInvalid'
150     if $I0 goto L2
151     $N0 = $P0
152     $I0 = ceil $N0
153     new res, 'WmlsInteger'
154     set res, $I0
155     goto L3
156   L2:
157     new res, 'WmlsInvalid'
158   L3:
159     .return (res)
160 .end
163 =head2 C<pow(value1, value2)>
165 =head3 DESCRIPTION
167 Returns an implementation-dependent approximation to the result of raising
168 value1 to the power of value2. If value1 is a negative number then value2
169 must be an integer.
171 =head3 PARAMETERS
173 value1 = Number
175 value2 = Number
177 =head3 RETURN VALUE
179 Floating-point or invalid.
181 =head3 EXCEPTIONS
183 If value1 == 0 and value2 < 0 then C<invalid> is returned.
185 If value1 < 0 and value2 is not an integer then C<invalid> is returned.
187 =cut
189 .sub '_float_pow' :anon
190     .param pmc value1
191     .param pmc value2
192     .local pmc res
193     $P1 = value1
194     $I0 = isa value1, 'WmlsString'
195     unless $I0 goto L1
196     $P1 = value1.'parseNumber'()
197   L1:
198     $I0 = isa $P1, 'WmlsInvalid'
199     if $I0 goto L2
200     $N1 = $P1
201     $P2 = value2
202     $I0 = isa value2, 'WmlsString'
203     unless $I0 goto L3
204     $P2 = value2.'parseNumber'()
205   L3:
206     $I0 = isa $P2, 'WmlsInvalid'
207     if $I0 goto L2
208     $N2 = $P2
209     unless $N1 == 0.0 goto L4
210     if $N2 < 0.0 goto L2
211     new res, 'WmlsFloat'
212     set res, 0.0
213     goto L5
214   L4:
215     unless $N1 < 0.0 goto L6
216     $I0 = isa $P2, 'WmlsFloat'
217     if $I0 goto L2
218   L6:
219     $N0 = pow $N1, $N2
220     new res, 'WmlsFloat'
221     set res, $N0
222     goto L5
223   L2:
224     new res, 'WmlsInvalid'
225   L5:
226     .return (res)
227 .end
230 =head2 C<round(value)>
232 =head3 DESCRIPTION
234 Returns the number value that is closest to the given value and is equal to a
235 mathematical integer. If two integer number values are equally close to the
236 value, the result is the larger number value. If the value is already an
237 integer, the result is the value itself.
239 =head3 PARAMETERS
241 value = Number
243 =head3 RETURN VALUE
245 Integer or invalid.
247 =cut
249 .sub '_float_round' :anon
250     .param pmc value
251     .local pmc res
252     $P0 = value
253     $I0 = isa value, 'WmlsString'
254     unless $I0 goto L1
255     $P0 = value.'parseNumber'()
256   L1:
257     $I0 = isa $P0, 'WmlsInvalid'
258     if $I0 goto L2
259     $N0 = $P0
260     $N0 += 0.5
261     $I0 = floor $N0
262     new res, 'WmlsInteger'
263     set res, $I0
264     goto L3
265   L2:
266     new res, 'WmlsInvalid'
267   L3:
268     .return (res)
269 .end
272 =head2 C<sqrt(value)>
274 =head3 DESCRIPTION
276 Returns an implementation-dependent approximation to the square root of
277 the given value.
279 =head3 PARAMETERS
281 value = Floating-point
283 =head3 RETURN VALUE
285 Floating-point or invalid.
287 =head3 EXCEPTIONS
289 If value is a negative number then invalid is returned.
291 =cut
293 .sub '_float_sqrt' :anon
294     .param pmc value
295     .local pmc res
296     $P0 = value
297     $I0 = isa value, 'WmlsString'
298     unless $I0 goto L1
299     $P0 = value.'parseNumber'()
300   L1:
301     $I0 = isa $P0, 'WmlsInvalid'
302     if $I0 goto L2
303     $N0 = $P0
304     if $N0 < 0.0 goto L2
305     $N1 = sqrt $N0
306     new res, 'WmlsFloat'
307     set res, $N1
308     goto L3
309   L2:
310     new res, 'WmlsInvalid'
311   L3:
312     .return (res)
313 .end
316 =head2 C<maxFloat()>
318 =head3 DESCRIPTION
320 Returns the maximum floating-point value supported by [IEEE754] single
321 precision floating-point format.
323 =head3 RETURN VALUE
325 Floating-point 3.40282347e+38.
327 =cut
329 .sub '_float_maxFloat' :anon
330     .local pmc res
331     new res, 'WmlsFloat'
332     set res, 3.40282347e+38
333     .return (res)
334 .end
337 =head2 C<minFloat()>
339 =head3 DESCRIPTION
341 Returns the smallest nonzero floating-point value supported by [IEEE754]
342 single precision floating-point format.
344 =head3 RETURN VALUE
346 Floating-point. Smaller than or equal to the normalised minimum single
347 precision floating-point value: 1.17549435e-38.
349 =cut
351 .sub '_float_minFloat' :anon
352     .local pmc res
353     new res, 'WmlsFloat'
354     set res, 1.17549435e-38
355     .return (res)
356 .end
359 =head1 AUTHORS
361 Francois Perrad.
363 =cut
366 # Local Variables:
367 #   mode: pir
368 #   fill-column: 100
369 # End:
370 # vim: expandtab shiftwidth=4 ft=pir: