repos / gbc

GBC - Go B Compiler
git clone https://github.com/xplshn/gbc.git

gbc / tests
xplshn  ·  2025-09-10

floats.bx

Bx
 1// Tests case statements with integers and floats, using extrn sprintf and printf.
 2
 3extrn sprintf, printf;
 4
 5main() {
 6    auto buffer 100;
 7    auto x = 2;
 8    auto y = 3.14;
 9    auto z = 2.71;
10
11    // --- Integer Test ---
12    printf("--- Testing Integer Switch ---\n");
13    switch (x) {
14        case 1:
15            printf("x is one\n");
16            break;
17        case 2, 3:
18            printf("x is two or three\n");
19            break;
20        default:
21            printf("x is something else\n");
22    }
23    printf("Integer test passed.\n\n");
24
25    // --- Float Test ---
26    printf("--- Testing Float Operations ---\n");
27    auto result = y * z; // 3.14 * 2.71 = 8.5094
28
29    // We can't switch on floats, so we'll use if/else to check the result
30    // and sprintf/printf to verify the value.
31    if (result > 8.5 && result < 8.51) {
32        sprintf(buffer, "Float multiplication successful: %f * %f = %f\n", y, z, result);
33        printf(buffer);
34    } else {
35        sprintf(buffer, "Float multiplication failed: %f * %f = %f\n", y, z, result);
36        printf(buffer);
37    }
38
39    auto div_res = y / 2.0; // 3.14 / 2.0 = 1.57
40    if (div_res > 1.56 && div_res < 1.58) {
41        sprintf(buffer, "Float division successful: %f / 2.0 = %f\n", y, div_res);
42        printf(buffer);
43    } else {
44        sprintf(buffer, "Float division failed: %f / 2.0 = %f\n", y, div_res);
45        printf(buffer);
46    }
47
48    return(0);
49}