or run

tessl search
Log in

Version

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
pypipkg:pypi/wtfpython@3.0.x
tile.json

tessl/pypi-wtfpython

tessl install tessl/pypi-wtfpython@3.0.0

Educational collection of surprising Python code snippets that demonstrate counter-intuitive behaviors and language internals

Agent Success

Agent success rate when using this tile

93%

Improvement

Agent success rate improvement when using this tile compared to baseline

1.06x

Baseline

Agent success rate without this tile

88%

task.mdevals/scenario-2/

Financial Calculator with Precise Rounding

Build a simple financial calculator module that handles monetary calculations with proper rounding behavior and validates numeric inputs to prevent precision errors.

Requirements

Your calculator should handle three key operations:

  1. Round monetary amounts - Round decimal amounts to 2 decimal places using proper banker's rounding (round half to even)
  2. Convert string amounts to numbers - Parse string representations of monetary amounts, but reject extremely large strings that could cause performance issues
  3. Validate precision - Check if a given float value can be represented precisely without floating-point errors

Rounding Behavior

When rounding amounts to 2 decimal places:

  • 2.125 should round to 2.12 (half rounds to even)
  • 2.135 should round to 2.14 (half rounds to even)
  • 2.126 should round to 2.13 (standard rounding up)
  • 2.124 should round to 2.12 (standard rounding down)

String Conversion Limits

Your converter should:

  • Accept reasonable numeric strings (e.g., "12345.67")
  • Reject extremely long numeric strings (over 4000 digits) by raising a ValueError with message "String too long for safe conversion"
  • Handle both positive and negative numbers

Precision Validation

Your validator should check if a float value equals its expected mathematical value:

  • The value 0.3 when computed from 0.1 + 0.2 is imprecise (should return False when compared to expected 0.3)
  • The value 1.0 when computed from 0.5 + 0.5 is precise (should return True)
  • Individual float values like 0.1 may have representation errors (should return False)
  • Clean fractions like 0.5 or 0.25 are exact (should return True)

Test Cases

  • Round 2.125 to 2 decimal places and verify it equals 2.12 @test
  • Round 2.135 to 2 decimal places and verify it equals 2.14 @test
  • Convert the string "12345.67" to a number and verify it equals 12345.67 @test
  • Attempting to convert a string with 5000 digits raises a ValueError @test
  • Validate that 0.1 + 0.2 is not precisely equal to 0.3 @test
  • Validate that 0.5 + 0.5 is precisely equal to 1.0 @test

@generates

API

def round_amount(value: float, decimals: int = 2) -> float:
    """
    Round a monetary amount using banker's rounding (round half to even).

    Args:
        value: The amount to round
        decimals: Number of decimal places (default: 2)

    Returns:
        Rounded value
    """
    pass

def convert_string_to_number(value_str: str) -> float:
    """
    Convert a string to a number with safety checks.

    Args:
        value_str: String representation of a number

    Returns:
        The numeric value

    Raises:
        ValueError: If the string is too long (over 4000 digits)
    """
    pass

def is_precise(value: float) -> bool:
    """
    Check if a float value is precisely representable.

    Args:
        value: The float value to check

    Returns:
        True if the value is precise, False if there are precision errors
    """
    pass

Dependencies { .dependencies }

wtfpython { .dependency }

Provides educational insights into Python numeric behaviors, including banker's rounding, integer string conversion limits, and float precision quirks.

@satisfied-by