2 # Copyright (C) 2001-2009, Parrot Foundation.
7 t/pmc/float.t - Floating-point Numbers
19 .const int TESTS = 159
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()
89 .include 'fp_equality.pasm'
91 .sub 'basic_assignment'
95 is($P0, 0.001, 'basic float assignment 1', PRECISION)
98 is($P0, 12.5, 'basic assignment 2', PRECISION)
101 is($P0, 1000.0, 'basic integer assignment', PRECISION)
103 $P0 = 'Twelve point five'
104 is($P0, 0.0, 'basic string assignment', PRECISION)
108 is($I0, 123, 'rounding to integer')
112 is($N0, 123.45, 'get_float_value', PRECISION)
116 is($S0, '123.45', 'get string')
119 $P1 = get_class ['Float']
120 is($P0, 12.49, 'setting value from string', PRECISION)
123 .sub 'add_number_to_self'
128 is($P0, 0.002, 'add number to self', PRECISION)
131 .sub 'sub_number_from_self'
136 is($P0, 0.0, 'sub number from self', PRECISION)
139 .sub 'multiply_number_by_self'
144 is($P0, 15227.56, 'multiply number by self', PRECISION)
147 .sub 'divide_number_by_self'
152 is($P0, 1.0, 'divide number by self', PRECISION)
155 .sub 'divide_by_zero'
164 push_eh divide_by_zero_handler
167 nok(1, 'divide by zero')
170 divide_by_zero_handler:
174 like($S1, ':s division by zero', 'divide by zero')
177 .sub 'truth_positive_float'
179 float_1 = new ['Float']
181 ok(float_1, 'Truth of a positive float')
184 .sub 'truth_negative_float'
186 float_1 = new ['Float']
188 ok(float_1, 'Truth of a negative float')
191 .sub 'truth_positive_integer'
193 float_1 = new ['Float']
195 ok(float_1, 'Truth of a positive integer')
198 .sub 'truth_negative_integer'
200 float_1 = new ['Float']
202 ok(float_1, 'Truth of a negative integer')
207 float_1 = new ['Float']
209 nok(float_1, 'Falseness of 0')
212 .sub 'falseness_0.000'
214 float_1 = new ['Float']
216 nok(float_1, 'Falseness of 0.000')
219 .sub 'integer_addition'
224 is($P0, 1.001, 'Basic integer arithmetic: addition (1)', PRECISION)
227 is($P0, -0.999, 'Basic integer arithmetic: addition (2)', PRECISION)
230 .sub 'integer_substraction'
235 is($P0, 26.45, 'Basic integer arithmetic: subtraction (1)', PRECISION)
238 is($P0, 50.45, 'Basic integer arithmetic: subtraction (2)', PRECISION)
241 .sub 'integer_multiplication'
246 is($P0, 10.0, 'Basic integer arithmetic: multiplication (1)', PRECISION)
249 is($P0, -10.0, 'Basic integer arithmetic: multiplication (2)', PRECISION)
252 is($P0, 0.0, 'Basic integer arithmetic: multiplication (3)', PRECISION)
255 .sub 'integer_division'
260 is($P0, 10000.0, 'Basic integer arithmetic: division (1)', PRECISION)
263 is($P0, 0.01, 'Basic integer arithmetic: division (2)', PRECISION)
266 .sub 'number_addition'
271 is($P0, 1.201, 'Basic numeric arithmetic: addition (1)', PRECISION)
274 is($P0, -1.199, 'Basic numeric arithmetic: addition (2)', PRECISION)
277 .sub 'number_substraction'
282 is($P0, 99.99, 'Basic numeric arithmetic: subtraction (1)', PRECISION)
285 is($P0, 100.0, 'Basic numeric arithmetic: subtraction (2)', PRECISION)
288 .sub 'number_multiplication'
293 is($P0, 0.1235, 'Basic numeric arithmetic: multiplication (1)', PRECISION)
296 is($P0, -0.3211, 'Basic numeric arithmetic: multiplication (2)', PRECISION)
299 is($P0, 0.0, 'Basic numeric arithmetic: multiplication (3)', PRECISION)
302 .sub 'number_division'
307 is($P0, 2e8, 'Basic numeric arithmetic: division (1)', PRECISION)
310 is($P0, 50000.0, 'Basic numeric arithmetic: division (2)', PRECISION)
313 .sub 'increment_decrement'
318 is($P0, 1.5, 'increment (1)', PRECISION)
320 is($P0, 0.5, 'decrement (1)', PRECISION)
322 is($P0, -.5, 'decrement (2)', PRECISION)
324 is($P0, 0.5, 'increment (2)', PRECISION)
331 is($P0, -0.5, 'Neg', PRECISION)
335 is($P1, 0.5, 'Neg is involutive', PRECISION)
339 load_bytecode 'config.pbc'
341 $P2 = $P1['has_negative_zero']
342 unless $P2 goto negative_zero_todoed
349 like($S0, '^\-0', 'negative zero')
352 negative_zero_todoed:
353 todo(1, '-0.0 not implemented, TT#313')
367 if $P0 == $P1 goto equality_1
370 ok($I0, 'equal floats')
373 if $P0 == $P2 goto equality_2
376 ok($I0, 'different floats are not equal')
379 if $P0 != $P2 goto equality_3
382 ok($I0, "different floats are different")
385 if $P0 != $P1 goto equality_4
388 ok($I0, "equal floats aren't different")
391 eq_num $P0, $P1, equality_5
394 ok($I0, "equal floats are eq_num")
397 eq_num $P0, $P2, equality_6
400 ok($I0, "different floats aren't eq_num")
403 ne_num $P0, $P2, equality_7
406 ok($I0, "different floats are ne_num")
409 ne_num $P0, $P1, equality_8
412 ok($I0, "equal floats aren't ne_num")
415 .sub 'is_interface_done'
420 bool1 = does pmc1, "scalar"
421 ok(bool1, 'Float does "scalar"')
423 bool1 = does pmc1, "float"
424 ok(bool1, 'Float does "float"')
426 bool1 = does pmc1, "no_interface"
427 nok(bool1, 'Float does not "no_interface"')
434 is($P0, $P0, 'abs does not change positive floats')
438 is($P0, 1.0, 'abs of -1.0', PRECISION)
442 is($P0, 5.0, 'abs of -5.0', PRECISION)
460 ok($I0, 'lt irreflexive')
483 lt_num $P1, $P2, lt_num_1
486 ok($I0, 'lt_num true')
489 lt_num $P1, $P4, lt_num_2
492 ok($I0, 'lt_num irreflexive')
495 lt_num $P1, $P3, lt_num_3
498 ok($I0, 'lt_num false')
522 ok($I0, 'le_p_nc false')
528 ok($I0, 'le reflexive')
545 le_num $P1, $P2, le_num_1
548 ok($I0, 'le_num true')
551 le_num $P1, $P4, le_num_2
554 ok($I0, 'le_num reflexive')
557 le_num $P1, $P3, le_num_3
560 ok($I0, 'le_num false')
572 ok($I0, 'comparison ops: gt nok')
578 nok($I0, 'comparison ops: gt irreflexive')
584 ok($I0, 'comparison ops: gt ok')
599 gt_num $P1, $P2, gt_num_1
602 ok($I0, 'comparison ops: gt_num nok')
605 gt_num $P1, $P4, gt_num_2
608 ok($I0, 'comparison ops: gt_num irreflexive')
611 gt_num $P1, $P3, gt_num_3
614 ok($I0, 'comparison ops: gt_num ok')
626 ok($I0, 'comparison ops: ge nok')
632 ok($I0, 'comparison ops: ge reflexive')
638 ok($I0, 'comparison ops: ge ok')
653 ge_num $P1, $P2, ge_num_1
656 ok($I0, 'comparison ops: ge_num nok')
659 ge_num $P1, $P4, ge_num_2
662 ok($I0, 'comparison ops: ge_num reflexive')
665 ge_num $P1, $P3, ge_num_3
668 ok($I0, 'comparison ops: ge_num ok')
679 is($I0, 0, 'comparison ops: cmp_p_n: equality')
682 is($I0, 1, 'comparison ops: cmp_p_n: gt')
685 is($I0, -1, 'comparison ops: cmp_p_n: lt')
692 $P4 = new ['Integer']
693 $P5 = new ['Integer']
704 nok($I0, 'comparison ops: isgt nok')
707 nok($I0, 'comparison ops: isgt irreflexive')
710 ok($I0, 'comparison ops: isgt ok')
713 ok($I0, 'comparison ops: isgt ok with Float and Integer')
716 nok($I0, 'comparison ops: isgt nok with Float and Integer')
719 nok($I0, 'comparison ops: isgt irreflexive (different PMCs)')
726 $P4 = new ['Integer']
727 $P5 = new ['Integer']
738 nok($I0, 'comparison ops: isge nok')
741 ok($I0, 'comparison ops: isge reflexive')
744 ok($I0, 'comparison ops: isge ok')
747 ok($I0, 'comparison ops: isge ok with Float and Integer')
750 nok($I0, 'comparison ops: isge nok with Float and Integer')
753 ok($I0, 'comparison ops: isge reflexive (different PMCs)')
760 $P4 = new ['Integer']
761 $P5 = new ['Integer']
772 ok($I0, 'comparison ops: islt ok')
775 nok($I0, 'comparison ops: islt irreflexive')
778 nok($I0, 'comparison ops: islt nok')
781 nok($I0, 'comparison ops: islt nok with Float and Integer')
784 ok($I0, 'comparison ops: islt ok with Float and Integer')
787 nok($I0, 'comparison ops: islt irreflexive (different PMCs)')
794 $P4 = new ['Integer']
795 $P5 = new ['Integer']
806 ok($I0, 'comparison ops: isle ok')
809 ok($I0, 'comparison ops: isle reflexive')
812 nok($I0, 'comparison ops: isle nok')
815 nok($I0, 'comparison ops: isle nok with Float and Integer')
818 ok($I0, 'comparison ops: isle ok with Float and Integer')
821 ok($I0, 'comparison ops: isle reflexive (different PMCs)')
828 $P4 = new ['Integer']
836 ok($I0, 'iseq reflexive, same PMC')
839 ok($I0, 'iseq reflexive, different PMCs')
842 nok($I0, 'iseq nok with two Floats')
845 nok($I0, 'iseq nok between an Integer and a Float')
852 $P4 = new ['Integer']
860 nok($I0, 'isne irreflexive, same PMC')
863 nok($I0, 'isne irreflexive, different PMCs')
866 ok($I0, 'isne ok with two Floats')
869 ok($I0, 'isne ok between an Integer and a Float')
872 .sub 'instantiate_str'
873 .const 'Float' pi = "3.1"
874 $P1 = get_class ['Float']
876 is(pi, 3.1, 'instantiate_str', PRECISION)
879 .sub 'cmp_subclasses'
880 $P0 = subclass 'Float', 'Flt'
889 is(-1, $I0, 'cmp functions for subclasses (lt)')
892 is(0, $I0, 'cmp functions for subclasses (eq)')
895 is(1, $I0, 'cmp functions for subclasses (gt)')
904 array = new 'FixedPMCArray'
914 $S0 = sprintf '%s(%.1f) is %.9f', array
915 is($P1, expected, $S0, PRECISION)
919 test_method('acos', 0.0, 1.570796327)
920 test_method('acos', 0.5, 1.047197551)
924 test_method('cos', 0.0, 1.0)
925 test_method('cos', 0.5, 0.877582562)
929 test_method('asec', 1.0, 0.0)
930 test_method('asec', 3.0, 1.230959417)
934 test_method('asin', 0.0, 0.0)
935 test_method('asin', 0.5, 0.523598776)
939 test_method('atan', 0.0, 0.0)
940 test_method('atan', 0.5, 0.463647609)
950 $P2 = $P0.'atan2'($P1)
951 is($P2, 0.950546841, 'atan2 as a method', PRECISION)
955 test_method('cosh', 0.0, 1.0)
956 test_method('cosh', 0.5, 1.127625965)
960 test_method('exp', 0.0, 1.0)
961 test_method('exp', 0.5, 1.648721271)
965 test_method('ln', 1.0, 0.0)
966 test_method('ln', 45.0, 3.806662490)
967 test_method('ln', 0.5, -0.693147181)
971 test_method('log10', 1000.0, 3.0)
972 test_method('log10', 0.5, -0.301029996)
976 test_method('log2', 32.0, 5.0)
977 test_method('log2', 0.5, -1.0)
981 test_method('sec', 0.0, 1.0)
982 test_method('sec', 0.5, 1.139493927)
986 test_method('sech', 0.0, 1.0)
987 test_method('sech', 0.5, 0.886818884)
991 test_method('sin', 0.0, 0.0)
992 test_method('sin', 0.5, 0.479425539)
996 test_method('sinh', 0.0, 0.0)
997 test_method('sinh', 0.5, 0.521095305)
1001 test_method('tan', 0.0, 0.0)
1002 test_method('tan', 0.5, 0.546302490)
1006 test_method('tanh', 0.0, 0.0)
1007 test_method('tanh', 0.5, 0.462117157)
1011 test_method('sqrt', 16.0, 4.0)
1012 test_method('sqrt', 2.0, 1.414213562)
1019 # vim: expandtab shiftwidth=4 ft=pir: