[Python] Float Pointing Search Issue

Discussão em 'Python' iniciado por Stack, Junho 7, 2025.

  1. Stack

    Stack Membro Participativo

    I'm trying to do simple search in float ranges -

    ratios range = [2.199164; 2.277869] and X range = [21.072100; 21.373130]

    formula is ratio * X = target. Problem is to find matching ratios and X which gives target = 47.925670. I also know that ratio=2.252316 and X=21.278393 gives target. It's looks simple to write in python, but problem is it never catch that ratio and X, even if I use step as 0.000001. I have try to use Decimal but still not helping. Here is also my code:

    from tqdm import tqdm

    start_ratio = 2.199164
    end_ratio = 2.277869
    target = 47.92567055
    X = 21.072100
    X_end = 21.373130
    step = 0.000001
    EPSILON = 1e-6

    found = []


    X_steps = int((X_end - X) / step)
    ratio_steps = int((end_ratio - start_ratio) / step)

    initial_start_ratio = start_ratio


    for _ in tqdm(range(X_steps + 1), desc="Scanning keys"):
    current_start_ratio = initial_start_ratio
    for _ in range(ratio_steps + 1):
    current = current_start_ratio * X
    if abs(current - target) < EPSILON:
    print(f"[FOUND] current_start_ratio={current_start_ratio:.6f}, key={X:.6f}, value={current:.6f}")
    with open("./data/phase_2.txt", "a") as file: # Use "a" to append, not overwrite
    file.write(
    f"[FOUND] current_start_ratio={current_start_ratio:.6f}, key={X:.6f}, value={current:.6f}\n")
    found.append((current_start_ratio, X, current))
    current_start_ratio += step
    X += step

    print(f"Matches found: {len(found)}")

    Continue reading...

Compartilhe esta Página