File size: 3,022 Bytes
f6d6874
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c1fc4c3
f6d6874
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import gradio as gr
import math

# === Utility Functions ===

def convert_number(input_str):
    try:
        if '× 10^' in input_str:
            base_str, exponent_str = input_str.split('× 10^')
            base = float(base_str.strip())
            exponent = int(exponent_str.strip())
            result = base * (10 ** exponent)
            return str(result)
        elif 'e' in input_str.lower():
            result = float(input_str)
            return str(result)
        else:
            num = float(input_str)
            if num == 0:
                return "0 × 10^0"
            exponent = int(math.floor(math.log10(abs(num))))
            base = num / (10 ** exponent)
            base = round(base, 6)
            return f"{base} × 10^{exponent}"
    except Exception as e:
        return f"Error: {e}"

def parse_standard_form(s):
    try:
        base, exponent = s.split("× 10^")
        base = float(base.strip())
        exponent = int(exponent.strip())
        return base, exponent
    except:
        return None, None

def standard_form_calculator(num1_str, num2_str, operation):
    a, m = parse_standard_form(num1_str)
    b, n = parse_standard_form(num2_str)
    if a is None or b is None:
        return "Invalid input format. Use format like: 3.2 × 10^4"
    if operation == "multiply":
        base = a * b
        exponent = m + n
    elif operation == "divide":
        base = a / b
        exponent = m - n
    else:
        return "Invalid operation"

    while base >= 10:
        base /= 10
        exponent += 1
    while base < 1:
        base *= 10
        exponent -= 1

    return f"{round(base, 6)} × 10^{exponent}"

# === Gradio Interface ===

# Converter UI
with gr.Blocks() as convert_ui:
    gr.Markdown("## 🔁 Standard ↔ Ordinary Conversion")
    input_box = gr.Textbox(label="Enter number (e.g., '2.3 × 10^4', '1e-3', or '23000')")
    output_box = gr.Textbox(label="Result", interactive=False)
    convert_btn = gr.Button("Convert")
    convert_btn.click(fn=convert_number, inputs=input_box, outputs=output_box)

# Calculator UI
with gr.Blocks() as calculator_ui:
    gr.Markdown("## ✖️➗ Standard Form Calculator")
    num1_input = gr.Textbox(label="First Number (e.g., '3.2 × 10^4')", value="3.2 × 10^4")
    num2_input = gr.Textbox(label="Second Number (e.g., '2 × 10^3')", value="2 × 10^3")
    operation_input = gr.Radio(["multiply", "divide"], label="Choose Operation")
    calc_result = gr.Textbox(label="Result", interactive=False)
    calc_btn = gr.Button("Calculate")
    calc_btn.click(fn=standard_form_calculator, inputs=[num1_input, num2_input, operation_input], outputs=calc_result)

# Main app with Tab selection
with gr.Blocks() as app:
    gr.Markdown("## 🔢 Standard Form Helper Tool By Rumaisa - Source Code Academia")
    tabs = gr.Tabs()
    with tabs:
        with gr.TabItem("Convert"):
            convert_ui.render()
        with gr.TabItem("Calculator"):
            calculator_ui.render()

# Launch the app
app.launch()