def _sample_conversion_decimal(dimension, is_extrapolation):
    """Samples to and from units and values."""
    base_unit, target_unit = random.sample(list(dimension.keys()), 2)
    scale = sympy.Rational(dimension[base_unit]) / dimension[target_unit]
    scale_non_decimal = _factor_non_decimal(sympy.denom(scale))
    entropy = 9 if is_extrapolation else 7
    base_value = number.non_integer_decimal(entropy, signed=False)
    base_value = display.Decimal(base_value.value * scale_non_decimal)
    target_value = display.Decimal(base_value.value * scale)
    return base_value, base_unit, target_value, target_unit
示例#2
0
 def testRound(self):
   decimal = display.Decimal(sympy.Rational(2675, 1000))  # 2.675
   self.assertEqual(sympy.sympify(decimal.round()), sympy.Integer(3))
   self.assertEqual(sympy.sympify(decimal.round(1)), sympy.Rational(27, 10))
   self.assertEqual(sympy.sympify(decimal.round(2)), sympy.Rational(268, 100))
   self.assertEqual(sympy.sympify(decimal.round(3)),
                    sympy.Rational(2675, 1000))
示例#3
0
def integer_or_decimal(entropy, signed):
    """Returns integer or non-integer decimal; 50% probability of each."""
    if random.choice([False, True]):
        # Represent it as a decimal so that arithmetic operations are supported:
        return display.Decimal(integer(entropy, signed))
    else:
        return non_integer_decimal(entropy, signed)
def add_or_sub(entropy, entropy_fn, bounded=False):
    context = None
    is_question = context is None
    context = composition.Context()

    is_addition = random.choice([False, True])
    entropy_p, entropy_q, sample_args = entropy_fn(entropy)

    p = display.Decimal(integer(entropy_p, bounded))
    q = display.Decimal(integer(entropy_q, bounded))
    p, q = context.sample(sample_args, [p, q])

    if is_addition:
        return arithmetic._add_question_or_entity(context, p, q, is_question)
    else:
        return arithmetic._sub_question_or_entity(context, p, q, is_question)
示例#5
0
 def testStr(self):
   self.assertEqual(str(display.Decimal(sympy.Rational(0, 10))), '0')
   self.assertEqual(str(display.Decimal(sympy.Rational(-1, 10))), '-0.1')
   self.assertEqual(str(display.Decimal(sympy.Rational(-11, 10))), '-1.1')
   self.assertEqual(str(display.Decimal(sympy.Rational(11, 10))), '1.1')
   self.assertEqual(str(display.Decimal(sympy.Rational(101, 1))), '101')
   self.assertEqual(
       str(display.Decimal(sympy.Rational(20171, 1000000))), '0.020171')
示例#6
0
 def testComparison(self):
   decimal = display.Decimal(sympy.Rational(-1, 2))
   # pylint: disable=g-generic-assert
   self.assertFalse(decimal != -0.5)
   self.assertTrue(decimal != 0)
   self.assertFalse(decimal < -0.5)
   self.assertTrue(decimal < 0)
   self.assertTrue(decimal <= -0.5)
   self.assertTrue(decimal <= 0)
   self.assertFalse(decimal > -0.5)
   self.assertTrue(decimal > -1)
   self.assertTrue(decimal >= -0.5)
   self.assertFalse(decimal >= 0)
   self.assertFalse(decimal == 0)
   self.assertTrue(decimal == -0.5)
示例#7
0
def non_integer_decimal(entropy, signed):
    """Returns a random decimal; integer divided by random power of ten.

  Guaranteed to be non-integer (i.e., numbers after the decimal point).

  Args:
    entropy: Float.
    signed: Boolean. Whether to also return negative numbers.

  Returns:
    Non-integer decimal.
  """
    while True:
        base = integer(entropy, signed)
        shift = random.randint(1, int(math.ceil(entropy)))
        divisor = 10**shift
        if base % divisor != 0:
            return display.Decimal(sympy.Rational(base, divisor))
示例#8
0
 def testAdd(self):
   self.assertEqual((display.Decimal(2) + display.Decimal(3)).value, 5)
示例#9
0
 def testBasic_ten(self):
   decimal = display.Decimal(10)
   self.assertEqual(str(decimal), '10')
   self.assertEqual(sympy.sympify(decimal), sympy.Integer(10))
   self.assertEqual(decimal.decimal_places(), 0)
示例#10
0
 def testNegation(self):
   decimal = display.Decimal(sympy.Rational(1, 2))
   decimal = -decimal
   self.assertNotEqual(decimal, 0.5)
   self.assertEqual(decimal, -0.5)
示例#11
0
 def testInt(self):
   decimal = display.Decimal(123)
   self.assertEqual(int(decimal), 123)
示例#12
0
 def testInt_errorIfNonInt(self):
   decimal = display.Decimal(sympy.Rational(1, 2))
   with self.assertRaisesRegexp(TypeError, 'Cannot represent'):
     int(decimal)
示例#13
0
 def testBasic(self):
   decimal = display.Decimal(sympy.Rational(123, 100))
   self.assertEqual(str(decimal), '1.23')
   self.assertEqual(sympy.sympify(decimal), sympy.Rational(123, 100))
   self.assertEqual(decimal.decimal_places(), 2)
示例#14
0
 def testMul(self):
   self.assertEqual((display.Decimal(2) * display.Decimal(3)).value, 6)
示例#15
0
 def testSub(self):
   self.assertEqual((display.Decimal(2) - display.Decimal(3)).value, -1)
示例#16
0
def round_number(value, sample_args, context=None):
    """Question for rounding integers and decimals."""
    del value  # unused for now
    if context is None:
        context = composition.Context()

    entropy, sample_args = sample_args.peel()

    # This is the power of 10 to round to. E.g., power == 0 corresponds to
    # rounding to the nearest integer; power == -2 corresponds to rounding to two
    # decimal places, and power == 3 corresponds to rounding to the nearest 1000.
    power = random.randint(-7, 6)

    answer_entropy = 1 + random.uniform(0, entropy / 2)
    entropy = max(1, entropy - answer_entropy)
    value_integer = number.integer(answer_entropy, signed=True)

    remainder_divisor = 10**int(math.ceil(entropy))
    remainder_range_lower = -remainder_divisor / 2
    remainder_range_upper = remainder_divisor / 2

    if value_integer <= 0:
        remainder_range_lower += 1
    if value_integer >= 0:
        remainder_range_upper -= 1

    remainder = random.randint(remainder_range_lower, remainder_range_upper)
    input_ = value_integer + sympy.Rational(remainder, remainder_divisor)
    scale = 10**power if power >= 0 else sympy.Rational(1, 10**(-power))
    input_ = input_ * scale
    value = value_integer * scale
    if not number.is_integer(input_):
        input_ = display.Decimal(input_)
    if not number.is_integer(value):
        value = display.Decimal(value)

    (input_, ) = context.sample(sample_args, [input_])

    if power > 0:
        # Rounding to a power of ten.
        round_to = 10**power
        if random.choice([False, True]):
            # Write the rounding value as a word instead.
            round_to = display.StringNumber(
                round_to, join_number_words_with_hyphens=False)
        description = 'paling dekat {round_to}'.format(round_to=round_to)
    elif power == 0 and random.choice([False, True]):
        # Round to nearest integer.
        description = 'bilangan terdekat'
    else:
        # Round to decimal places.
        description = random.choice(['{dps} desimal'])
        if False:  # no plural indonesia
            # Plural
            description += 's'
        dps = -power
        if random.choice([False, True]):
            dps = display.StringNumber(dps)
        description = description.format(dps=dps)

    template = random.choice([
        'Bulatkan {input} ke {description}.',
        'Berapakah {input} dibulatkan menjadi {description}?',
    ])

    return example.Problem(question=example.question(context,
                                                     template,
                                                     input=input_,
                                                     description=description),
                           answer=value)
示例#17
0
 def testStr_verySmall(self):
   # Tests it doesn't display in "scientific" notation 1E-9.
   decimal = display.Decimal(sympy.Rational(1, 1000000000))
   self.assertEqual(str(decimal), '0.000000001')