repos / gbc

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

gbc / tests
xplshn  ·  2025-09-10

floats.c

C
 1#include <stdio.h>
 2
 3int main(void) {
 4    char buffer[100];
 5    int x = 2;
 6    double y = 3.14;
 7    double z = 2.71;
 8
 9    // --- Integer Test ---
10    printf("--- Testing Integer Switch ---\n");
11    switch (x) {
12        case 1:
13            printf("x is one\n");
14            break;
15        case 2:
16        case 3:
17            printf("x is two or three\n");
18            break;
19        default:
20            printf("x is something else\n");
21            break;
22    }
23    printf("Integer test passed.\n\n");
24
25    // --- Float Test ---
26    printf("--- Testing Float Operations ---\n");
27    double result = y * z; // 3.14 * 2.71 = 8.5094
28
29    if (result > 8.5 && result < 8.51) {
30        sprintf(buffer, "Float multiplication successful: %f * %f = %f\n", y, z, result);
31        printf("%s", buffer);
32    } else {
33        sprintf(buffer, "Float multiplication failed: %f * %f = %f\n", y, z, result);
34        printf("%s", buffer);
35    }
36
37    double div_res = y / 2.0; // 3.14 / 2.0 = 1.57
38    if (div_res > 1.56 && div_res < 1.58) {
39        sprintf(buffer, "Float division successful: %f / 2.0 = %f\n", y, div_res);
40        printf("%s", buffer);
41    } else {
42        sprintf(buffer, "Float division failed: %f / 2.0 = %f\n", y, div_res);
43        printf("%s", buffer);
44    }
45
46    return 0;
47}