from random import choice, randint, seed from mpmath import mpf from mpmath.libmp import (from_int, from_str, mpf_div, mpf_mul, round_ceiling, round_down, round_floor, round_nearest, round_up) from mpmath.libmp.libintmath import trailing from mpmath.libmp.libmpf import mpf_rdiv_int def test_div_1_3(): a = from_int(1) b = from_int(3) c = from_int(-1) # floor rounds down, ceiling rounds up assert mpf_div(a, b, 7, round_floor) == from_str('0.01010101', base=2) assert mpf_div(a, b, 7, round_ceiling) == from_str('0.01010110', base=2) assert mpf_div(a, b, 7, round_down) == from_str('0.01010101', base=2) assert mpf_div(a, b, 7, round_up) == from_str('0.01010110', base=2) assert mpf_div(a, b, 7, round_nearest) == from_str('0.01010101', base=2) # floor rounds up, ceiling rounds down assert mpf_div(c, b, 7, round_floor) == from_str('-0.01010110', base=2) assert mpf_div(c, b, 7, round_ceiling) == from_str('-0.01010101', base=2) assert mpf_div(c, b, 7, round_down) == from_str('-0.01010101', base=2) assert mpf_div(c, b, 7, round_up) == from_str('-0.01010110', base=2) assert mpf_div(c, b, 7, round_nearest) == from_str('-0.01010101', base=2) def test_mpf_divi_1_3(): a = 1 b = from_int(3) c = -1 assert mpf_rdiv_int(a, b, 7, round_floor) == from_str('0.01010101', base=2) assert mpf_rdiv_int(a, b, 7, round_ceiling) == from_str('0.01010110', base=2) assert mpf_rdiv_int(a, b, 7, round_down) == from_str('0.01010101', base=2) assert mpf_rdiv_int(a, b, 7, round_up) == from_str('0.01010110', base=2) assert mpf_rdiv_int(a, b, 7, round_nearest) == from_str('0.01010101', base=2) assert mpf_rdiv_int(c, b, 7, round_floor) == from_str('-0.01010110', base=2) assert mpf_rdiv_int(c, b, 7, round_ceiling) == from_str('-0.01010101', base=2) assert mpf_rdiv_int(c, b, 7, round_down) == from_str('-0.01010101', base=2) assert mpf_rdiv_int(c, b, 7, round_up) == from_str('-0.01010110', base=2) assert mpf_rdiv_int(c, b, 7, round_nearest) == from_str('-0.01010101', base=2) def test_div_300(): q = from_int(1000000) a = from_int(300499999) # a/q is a little less than a half-integer b = from_int(300500000) # b/q exactly a half-integer c = from_int(300500001) # c/q is a little more than a half-integer # Check nearest integer rounding (prec=9 as 2**8 < 300 < 2**9) assert mpf_div(a, q, 9, round_down) == from_int(300) assert mpf_div(b, q, 9, round_down) == from_int(300) assert mpf_div(c, q, 9, round_down) == from_int(300) assert mpf_div(a, q, 9, round_up) == from_int(301) assert mpf_div(b, q, 9, round_up) == from_int(301) assert mpf_div(c, q, 9, round_up) == from_int(301) # Nearest even integer is down assert mpf_div(a, q, 9, round_nearest) == from_int(300) assert mpf_div(b, q, 9, round_nearest) == from_int(300) assert mpf_div(c, q, 9, round_nearest) == from_int(301) # Nearest even integer is up a = from_int(301499999) b = from_int(301500000) c = from_int(301500001) assert mpf_div(a, q, 9, round_nearest) == from_int(301) assert mpf_div(b, q, 9, round_nearest) == from_int(302) assert mpf_div(c, q, 9, round_nearest) == from_int(302) def test_tight_integer_division(): # Test that integer division at tightest possible precision is exact N = 100 seed(1) for i in range(N): a = choice([1, -1]) * randint(1, 1<