2 # Copyright (C) 2001-2010, Parrot Foundation.
7 t/pmc/float.t - Floating-point Numbers
19 .const int TESTS = 166
20 .const num PRECISION = 0.000001
23 .include 'test_more.pir'
28 sub_number_from_self()
29 multiply_number_by_self()
30 divide_number_by_self()
32 truth_positive_float()
33 truth_negative_float()
34 truth_positive_integer()
35 truth_negative_integer()
39 integer_substraction()
40 integer_multiplication()
44 number_multiplication()
91 .include 'fp_equality.pasm'
93 .sub 'basic_assignment'
94 # Assignments can morph to other PMC type,
95 # use a new Float for each test to be sure we are testing
100 is($P0, 0.001, 'basic float assignment 1', PRECISION)
104 is($P0, 12.5, 'basic assignment 2', PRECISION)
108 is($P0, 1000.0, 'basic integer assignment', PRECISION)
111 $P0 = 'Twelve point five'
112 is($P0, 0.0, 'basic string assignment', PRECISION)
117 is($I0, 123, 'rounding to integer')
122 is($N0, 123.45, 'get_float_value', PRECISION)
127 is($S0, '123.45', 'get string')
132 is($S0, '123.45', 'get_repr')
136 is($P0, 12.49, 'setting value from String', PRECISION)
139 .sub 'add_number_to_self'
144 is($P0, 0.002, 'add number to self', PRECISION)
147 .sub 'sub_number_from_self'
152 is($P0, 0.0, 'sub number from self', PRECISION)
155 .sub 'multiply_number_by_self'
160 is($P0, 15227.56, 'multiply number by self', PRECISION)
163 .sub 'divide_number_by_self'
168 is($P0, 1.0, 'divide number by self', PRECISION)
171 .sub 'divide_by_zero'
180 push_eh divide_by_zero_handler
183 nok(1, 'divide by zero')
186 divide_by_zero_handler:
187 ok(1, "divide by zero throws exception")
190 .sub 'truth_positive_float'
192 float_1 = new ['Float']
194 ok(float_1, 'Truth of a positive float')
197 .sub 'truth_negative_float'
199 float_1 = new ['Float']
201 ok(float_1, 'Truth of a negative float')
204 .sub 'truth_positive_integer'
206 float_1 = new ['Float']
208 ok(float_1, 'Truth of a positive integer')
211 .sub 'truth_negative_integer'
213 float_1 = new ['Float']
215 ok(float_1, 'Truth of a negative integer')
220 float_1 = new ['Float']
222 nok(float_1, 'Falseness of 0')
225 .sub 'falseness_0.000'
227 float_1 = new ['Float']
229 nok(float_1, 'Falseness of 0.000')
232 .sub 'integer_addition'
237 is($P0, 1.001, 'Basic integer arithmetic: addition (1)', PRECISION)
240 is($P0, -0.999, 'Basic integer arithmetic: addition (2)', PRECISION)
243 .sub 'integer_substraction'
248 is($P0, 26.45, 'Basic integer arithmetic: subtraction (1)', PRECISION)
251 is($P0, 50.45, 'Basic integer arithmetic: subtraction (2)', PRECISION)
254 .sub 'integer_multiplication'
259 is($P0, 10.0, 'Basic integer arithmetic: multiplication (1)', PRECISION)
262 is($P0, -10.0, 'Basic integer arithmetic: multiplication (2)', PRECISION)
265 is($P0, 0.0, 'Basic integer arithmetic: multiplication (3)', PRECISION)
268 .sub 'integer_division'
273 is($P0, 10000.0, 'Basic integer arithmetic: division (1)', PRECISION)
276 is($P0, 0.01, 'Basic integer arithmetic: division (2)', PRECISION)
279 .sub 'number_addition'
284 is($P0, 1.201, 'Basic numeric arithmetic: addition (1)', PRECISION)
287 is($P0, -1.199, 'Basic numeric arithmetic: addition (2)', PRECISION)
290 .sub 'number_substraction'
295 is($P0, 99.99, 'Basic numeric arithmetic: subtraction (1)', PRECISION)
298 is($P0, 100.0, 'Basic numeric arithmetic: subtraction (2)', PRECISION)
301 .sub 'number_multiplication'
306 is($P0, 0.1235, 'Basic numeric arithmetic: multiplication (1)', PRECISION)
309 is($P0, -0.3211, 'Basic numeric arithmetic: multiplication (2)', PRECISION)
312 is($P0, 0.0, 'Basic numeric arithmetic: multiplication (3)', PRECISION)
315 .sub 'number_division'
320 is($P0, 2e8, 'Basic numeric arithmetic: division (1)', PRECISION)
323 is($P0, 50000.0, 'Basic numeric arithmetic: division (2)', PRECISION)
326 .sub 'increment_decrement'
331 is($P0, 1.5, 'increment (1)', PRECISION)
333 is($P0, 0.5, 'decrement (1)', PRECISION)
335 is($P0, -.5, 'decrement (2)', PRECISION)
337 is($P0, 0.5, 'increment (2)', PRECISION)
344 is($P0, -0.5, 'Neg', PRECISION)
348 is($P1, 0.5, 'Neg is involutive', PRECISION)
352 load_bytecode 'config.pbc'
354 $P2 = $P1['has_negative_zero']
355 unless $P2 goto negative_zero_todoed
362 is($S0, "-0", "negative 0.0 to string")
365 negative_zero_todoed:
366 todo(0, '-0.0 not implemented, TT #313')
380 if $P0 == $P1 goto equality_1
383 ok($I0, 'equal floats')
386 if $P0 == $P2 goto equality_2
389 ok($I0, 'different floats are not equal')
392 if $P0 != $P2 goto equality_3
395 ok($I0, "different floats are different")
398 if $P0 != $P1 goto equality_4
401 ok($I0, "equal floats aren't different")
404 eq_num $P0, $P1, equality_5
407 ok($I0, "equal floats are eq_num")
410 eq_num $P0, $P2, equality_6
413 ok($I0, "different floats aren't eq_num")
416 ne_num $P0, $P2, equality_7
419 ok($I0, "different floats are ne_num")
422 ne_num $P0, $P1, equality_8
425 ok($I0, "equal floats aren't ne_num")
428 .sub 'is_interface_done'
433 bool1 = does pmc1, "scalar"
434 ok(bool1, 'Float does "scalar"')
436 bool1 = does pmc1, "float"
437 ok(bool1, 'Float does "float"')
439 bool1 = does pmc1, "no_interface"
440 nok(bool1, 'Float does not "no_interface"')
447 is($P0, $P0, 'abs does not change positive floats')
451 is($P0, 1.0, 'abs of -1.0', PRECISION)
455 is($P0, 5.0, 'abs of -5.0', PRECISION)
459 is($P1, 6.0, 'abs two operands from -6.0', PRECISION)
460 is($P0, -6.0, 'abs two operands source unchanged', PRECISION)
478 ok($I0, 'lt irreflexive')
501 lt_num $P1, $P2, lt_num_1
504 ok($I0, 'lt_num true')
507 lt_num $P1, $P4, lt_num_2
510 ok($I0, 'lt_num irreflexive')
513 lt_num $P1, $P3, lt_num_3
516 ok($I0, 'lt_num false')
540 ok($I0, 'le_p_nc false')
546 ok($I0, 'le reflexive')
563 le_num $P1, $P2, le_num_1
566 ok($I0, 'le_num true')
569 le_num $P1, $P4, le_num_2
572 ok($I0, 'le_num reflexive')
575 le_num $P1, $P3, le_num_3
578 ok($I0, 'le_num false')
590 ok($I0, 'comparison ops: gt nok')
596 nok($I0, 'comparison ops: gt irreflexive')
602 ok($I0, 'comparison ops: gt ok')
617 gt_num $P1, $P2, gt_num_1
620 ok($I0, 'comparison ops: gt_num nok')
623 gt_num $P1, $P4, gt_num_2
626 ok($I0, 'comparison ops: gt_num irreflexive')
629 gt_num $P1, $P3, gt_num_3
632 ok($I0, 'comparison ops: gt_num ok')
644 ok($I0, 'comparison ops: ge nok')
650 ok($I0, 'comparison ops: ge reflexive')
656 ok($I0, 'comparison ops: ge ok')
671 ge_num $P1, $P2, ge_num_1
674 ok($I0, 'comparison ops: ge_num nok')
677 ge_num $P1, $P4, ge_num_2
680 ok($I0, 'comparison ops: ge_num reflexive')
683 ge_num $P1, $P3, ge_num_3
686 ok($I0, 'comparison ops: ge_num ok')
697 is($I0, 0, 'comparison ops: cmp_p_n: equality')
700 is($I0, 1, 'comparison ops: cmp_p_n: gt')
703 is($I0, -1, 'comparison ops: cmp_p_n: lt')
710 $P4 = new ['Integer']
711 $P5 = new ['Integer']
722 nok($I0, 'comparison ops: isgt nok')
725 nok($I0, 'comparison ops: isgt irreflexive')
728 ok($I0, 'comparison ops: isgt ok')
731 ok($I0, 'comparison ops: isgt ok with Float and Integer')
734 nok($I0, 'comparison ops: isgt nok with Float and Integer')
737 nok($I0, 'comparison ops: isgt irreflexive (different PMCs)')
744 $P4 = new ['Integer']
745 $P5 = new ['Integer']
756 nok($I0, 'comparison ops: isge nok')
759 ok($I0, 'comparison ops: isge reflexive')
762 ok($I0, 'comparison ops: isge ok')
765 ok($I0, 'comparison ops: isge ok with Float and Integer')
768 nok($I0, 'comparison ops: isge nok with Float and Integer')
771 ok($I0, 'comparison ops: isge reflexive (different PMCs)')
778 $P4 = new ['Integer']
779 $P5 = new ['Integer']
790 ok($I0, 'comparison ops: islt ok')
793 nok($I0, 'comparison ops: islt irreflexive')
796 nok($I0, 'comparison ops: islt nok')
799 nok($I0, 'comparison ops: islt nok with Float and Integer')
802 ok($I0, 'comparison ops: islt ok with Float and Integer')
805 nok($I0, 'comparison ops: islt irreflexive (different PMCs)')
812 $P4 = new ['Integer']
813 $P5 = new ['Integer']
824 ok($I0, 'comparison ops: isle ok')
827 ok($I0, 'comparison ops: isle reflexive')
830 nok($I0, 'comparison ops: isle nok')
833 nok($I0, 'comparison ops: isle nok with Float and Integer')
836 ok($I0, 'comparison ops: isle ok with Float and Integer')
839 ok($I0, 'comparison ops: isle reflexive (different PMCs)')
846 $P4 = new ['Integer']
854 ok($I0, 'iseq reflexive, same PMC')
857 ok($I0, 'iseq reflexive, different PMCs')
860 nok($I0, 'iseq nok with two Floats')
863 nok($I0, 'iseq nok between an Integer and a Float')
870 $P4 = new ['Integer']
878 nok($I0, 'isne irreflexive, same PMC')
881 nok($I0, 'isne irreflexive, different PMCs')
884 ok($I0, 'isne ok with two Floats')
887 ok($I0, 'isne ok between an Integer and a Float')
890 .sub 'instantiate_str'
891 .const 'Float' pi = "3.1"
892 $P1 = get_class ['Float']
894 is(pi, 3.1, 'instantiate_str', PRECISION)
897 .sub 'cmp_subclasses'
898 $P0 = subclass 'Float', 'Flt'
907 is(-1, $I0, 'cmp functions for subclasses (lt)')
910 is(0, $I0, 'cmp functions for subclasses (eq)')
913 is(1, $I0, 'cmp functions for subclasses (gt)')
922 array = new 'FixedPMCArray'
932 $S0 = sprintf '%s(%.1f) is %.9f', array
933 is($P1, expected, $S0, PRECISION)
937 test_method('acos', 0.0, 1.570796327)
938 test_method('acos', 0.5, 1.047197551)
942 test_method('cos', 0.0, 1.0)
943 test_method('cos', 0.5, 0.877582562)
947 test_method('asec', 1.0, 0.0)
948 test_method('asec', 3.0, 1.230959417)
952 test_method('asin', 0.0, 0.0)
953 test_method('asin', 0.5, 0.523598776)
957 test_method('atan', 0.0, 0.0)
958 test_method('atan', 0.5, 0.463647609)
968 $P2 = $P0.'atan2'($P1)
969 is($P2, 0.950546841, 'atan2 as a method', PRECISION)
973 test_method('cosh', 0.0, 1.0)
974 test_method('cosh', 0.5, 1.127625965)
978 test_method('exp', 0.0, 1.0)
979 test_method('exp', 0.5, 1.648721271)
983 test_method('ln', 1.0, 0.0)
984 test_method('ln', 45.0, 3.806662490)
985 test_method('ln', 0.5, -0.693147181)
989 test_method('log10', 1000.0, 3.0)
990 test_method('log10', 0.5, -0.301029996)
994 test_method('log2', 32.0, 5.0)
995 test_method('log2', 0.5, -1.0)
999 test_method('sec', 0.0, 1.0)
1000 test_method('sec', 0.5, 1.139493927)
1004 test_method('csc', 0.5, 2.0858296)
1005 test_method('csc', 1.0, 1.1883951)
1009 test_method('sech', 0.0, 1.0)
1010 test_method('sech', 0.5, 0.886818884)
1014 test_method('sin', 0.0, 0.0)
1015 test_method('sin', 0.5, 0.479425539)
1019 test_method('sinh', 0.0, 0.0)
1020 test_method('sinh', 0.5, 0.521095305)
1024 test_method('tan', 0.0, 0.0)
1025 test_method('tan', 0.5, 0.546302490)
1029 test_method('cot', 0.5, 1.8304877)
1030 test_method('cot', 1.0, 0.64209262)
1034 test_method('tanh', 0.0, 0.0)
1035 test_method('tanh', 0.5, 0.462117157)
1039 test_method('sqrt', 16.0, 4.0)
1040 test_method('sqrt', 2.0, 1.414213562)
1047 # vim: expandtab shiftwidth=4 ft=pir: