1 # Copyright (C) 2008, The Perl Foundation.
6 php_math.pir - PHP math Standard Library
16 .const num PI = 3.14159265358979323846
18 .sub 'longtobase' :anon
21 .const string digits = '0123456789abcdefghijklmnopqrstuvwxyz'
25 $S1 = substr digits, $I0, 1
32 .macro ROUND_WITH_FUZZ(val, places)
38 unless tmp >= 0.0 goto L11
49 .macro TRIG1(args, func)
52 unless argc != 1 goto L1
57 $I0 = isa $P1, 'PhpFloat'
68 =item C<int abs(int number)>
70 Return the absolute value of the number
75 .param pmc args :slurpy
78 unless argc != 1 goto L1
83 $P1 = $P1.'to_number'()
84 $I0 = isa $P1, 'PhpFloat'
90 $I0 = isa $P1, 'PhpInteger'
99 =item C<float acos(float number)>
101 Return the arc cosine of the number in radians
106 .param pmc args :slurpy
110 =item C<float acosh(float number)>
112 Returns the inverse hyperbolic cosine of the number, i.e. the value whose hyperbolic cosine is number
117 .param pmc args :slurpy
121 =item C<float asin(float number)>
123 Returns the arc sine of the number in radians
128 .param pmc args :slurpy
132 =item C<float asinh(float number)>
134 Returns the inverse hyperbolic sine of the number, i.e. the value whose hyperbolic sine is number
139 .param pmc args :slurpy
143 =item C<float atan(float number)>
145 Returns the arc tangent of the number in radians
150 .param pmc args :slurpy
154 =item C<float atan2(float y, float x)>
156 Returns the arc tangent of y/x, with the resulting quadrant determined by the signs of y and x
161 .param pmc args :slurpy
164 unless argc != 2 goto L1
176 =item C<float atanh(float number)>
178 Returns the inverse hyperbolic tangent of the number, i.e. the value whose hyperbolic tangent is number
183 .param pmc args :slurpy
187 =item C<string base_convert(string number, int frombase, int tobase)>
189 Converts a number in a string from any base <= 36 to any base <= 36
194 .param pmc args :slurpy
197 unless argc != 3 goto L1
213 error(E_WARNING, "Invalid `from base' (", $I2, ")")
220 error(E_WARNING, "Invalid `to base' (", $I3, ")")
223 $P0 = $P1.'to_base'($I2)
224 $I0 = isa $P0, 'PhpInteger'
227 $S0 = longtobase($I0, $I3)
233 =item C<int bindec(string binary_number)>
235 Returns the decimal equivalent of the binary number
240 .param pmc args :slurpy
243 unless argc != 1 goto L1
251 $P0 = $P1.'to_base'(2)
255 =item C<float ceil(float number)>
257 Returns the next highest integer value of the number
262 .param pmc args :slurpy
265 unless argc != 1 goto L1
270 $P1 = $P1.'to_number'()
271 $I0 = isa $P1, 'PhpFloat'
277 $I0 = isa $P1, 'PhpInteger'
285 =item C<float cos(float number)>
287 Returns the cosine of the number in radians
292 .param pmc args :slurpy
296 =item C<float cosh(float number)>
298 Returns the hyperbolic cosine of the number, defined as (exp(number) + exp(-number))/2
303 .param pmc args :slurpy
307 =item C<string decbin(int decimal_number)>
309 Returns a string containing a binary representation of the number
314 .param pmc args :slurpy
317 unless argc != 1 goto L1
323 $S0 = longtobase($I1, 2)
327 =item C<string dechex(int decimal_number)>
329 Returns a string containing a hexadecimal representation of the given number
334 .param pmc args :slurpy
337 unless argc != 1 goto L1
343 $S0 = longtobase($I1, 16)
347 =item C<string decoct(int decimal_number)>
349 Returns a string containing an octal representation of the given number
354 .param pmc args :slurpy
357 unless argc != 1 goto L1
363 $S0 = longtobase($I1, 8)
367 =item C<float deg2rad(float number)>
369 Converts the number in degrees to the radian equivalent
374 .param pmc args :slurpy
377 unless argc != 1 goto L1
388 =item C<float exp(float number)>
390 Returns e raised to the power of the number
395 .param pmc args :slurpy
397 ($I0, number) = parse_parameters('d', args :flat)
405 =item C<float expm1(float number)>
407 Returns exp(number) - 1, computed in a way that accurate even when the value of number is close to zero
409 NOT IMPLEMENTED. WARNING: this function is experimental.
417 =item C<float floor(float number)>
419 Returns the next lowest integer value from the number
424 .param pmc args :slurpy
427 unless argc != 1 goto L1
432 $P1 = $P1.'to_number'()
433 $I0 = isa $P1, 'PhpFloat'
439 $I0 = isa $P1, 'PhpInteger'
447 =item C<float fmod(float x, float y)>
449 Returns the remainder of dividing x by y as a float
454 .param pmc args :slurpy
457 ($I0, x, y) = parse_parameters('dd', args :flat)
465 =item C<int hexdec(string hexadecimal_number)>
467 Returns the decimal equivalent of the hexadecimal number
472 .param pmc args :slurpy
475 unless argc != 1 goto L1
483 $P0 = $P1.'to_base'(16)
487 =item C<float hypot(float num1, float num2)>
489 Returns sqrt(num1*num1 + num2*num2)
494 .param pmc args :slurpy
497 unless argc != 2 goto L1
512 =item C<bool is_finite(float val)>
514 Returns whether argument is finite
519 .param pmc args :slurpy
521 ($I0, val) = parse_parameters('d', args :flat)
525 $P0 = val.'is_finite'()
529 =item C<bool is_infinite(float val)>
531 Returns whether argument is infinite
536 .param pmc args :slurpy
538 ($I0, val) = parse_parameters('d', args :flat)
542 $P0 = val.'is_infinite'()
546 =item C<bool is_nan(float val)>
548 Returns whether argument is not a number
553 .param pmc args :slurpy
555 ($I0, val) = parse_parameters('d', args :flat)
563 =item C<float log(float number, [float base])>
565 Returns the natural logarithm of the number, or the base log if base is specified
570 .param pmc args :slurpy
573 unless argc == 1 goto L1
579 unless argc == 2 goto L2
584 unless $N2 <= 0.0 goto L3
585 error(E_WARNING, "base must be greater than 0")
597 =item C<float log10(float number)>
599 Returns the base-10 logarithm of the number
604 .param pmc args :slurpy
607 unless argc != 1 goto L1
617 =item C<float log1p(float number)>
619 Returns log(1 + number), computed in a way that accurate even when the value of number is close to zero
621 NOT IMPLEMENTED. WARNING: this function is experimental.
629 =item C<string number_format(float number [, int num_decimal_places [, string dec_seperator, string thousands_seperator]])>
631 Formats a number with grouped thousands
636 .param pmc args :slurpy
639 .local string thousand_sep, dec_point
642 unless argc == 1 goto L1
644 $P1 = $P1.'to_number'()
646 .return _number_format($N1, 0, dec_point, thousand_sep)
648 unless argc == 2 goto L2
650 $P1 = $P1.'to_number'()
654 .return _number_format($N1, $I2, dec_point, thousand_sep)
656 unless argc == 4 goto L3
658 $P1 = $P1.'to_number'()
663 $I0 = isa $P3, 'PhpUndef'
666 $I3 = length dec_point
668 dec_point =substr dec_point, 0, 1
671 $I0 = isa $P4, 'PhpUndef'
674 $I4 = length thousand_sep
676 thousand_sep =substr thousand_sep, 0, 1
678 .return _number_format($N1, $I2, dec_point, thousand_sep)
684 .sub '_number_format' :anon
687 .param string dec_point
688 .param string thousand_sep
689 .local int is_negative
695 unless dec < 0 goto L2
698 .ROUND_WITH_FUZZ(d, dec)
700 $S0 = concat '%.', $S1
702 new $P0, 'FixedFloatArray'
706 tmpbuf = sprintf $S0, $P0
708 $I0 = index tmpbuf, '.'
709 unless $I0 < 0 goto L3
715 $S0 = substr tmpbuf, $I1
716 unless dec_point goto L4
717 $S0 = concat dec_point, $S0
721 unless $I1 < 0 goto L5
725 $S1 = substr tmpbuf, $I1, $I2
726 $S0 = concat $S1, $S0
728 unless $I0 > 0 goto L6
729 unless thousand_sep goto L4
730 $S0 = concat thousand_sep, $S0
733 unless is_negative goto L7
734 $S0 = concat '-', $S0
739 =item C<int octdec(string octal_number)>
741 Returns the decimal equivalent of an octal string
746 .param pmc args :slurpy
749 unless argc != 1 goto L1
757 $P0 = $P1.'to_base'(8)
761 =item C<float pi(void)>
763 Returns an approximation of pi
768 .param pmc args :slurpy
772 =item C<number pow(number base, number exponent)>
774 Returns base raised to the power of exponent. Returns integer result when possible
779 .param pmc args :slurpy
782 ($I0, base, exponent) = parse_parameters('z/z/', args :flat)
786 base = base.'to_number'()
787 exponent = exponent.'to_number'()
788 $I0 = isa base, 'PhpInteger'
790 $I0 = isa exponent, 'PhpInteger'
804 =item C<float rad2deg(float number)>
806 Converts the radian number to the equivalent number in degrees
811 .param pmc args :slurpy
814 unless argc != 1 goto L1
825 =item C<float round(float number [, int precision])>
827 Returns the number rounded to specified precision
832 .param pmc args :slurpy
844 unless argc == 2 goto L3
848 $I0 = isa $P1, 'PhpInteger'
850 unless $I2 >= 0 goto L5
854 $I0 = isa $P1, 'PhpFloat'
859 .ROUND_WITH_FUZZ($N0, $N2)
865 =item C<float sin(float number)>
867 Returns the sine of the number in radians
872 .param pmc args :slurpy
876 =item C<float sinh(float number)>
878 Returns the hyperbolic sine of the number, defined as (exp(number) - exp(-number))/2
883 .param pmc args :slurpy
887 =item C<float sqrt(float number)>
889 Returns the square root of the number
894 .param pmc args :slurpy
897 unless argc != 1 goto L1
907 =item C<float tan(float number)>
909 Returns the tangent of the number in radians
914 .param pmc args :slurpy
918 =item C<float tanh(float number)>
920 Returns the hyperbolic tangent of the number, defined as sinh(number)/cosh(number)
925 .param pmc args :slurpy
937 # vim: expandtab shiftwidth=4 ft=pir: