File size: 1,255 Bytes
71174bc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// NOTE: Purpose of this is to mimic C++ float point precision in TypeScript

export class Float32 {
    private value: number;

    constructor(value: number) {
        this.value = Math.fround(value); // Ensure 32-bit precision
    }

    static add(a: Float32, b: Float32): Float32 {
        return new Float32(Math.fround(a.value + b.value));
    }

    static subtract(a: Float32, b: Float32): Float32 {
        return new Float32(Math.fround(a.value - b.value));
    }

    static multiply(a: Float32, b: Float32): Float32 {
        return new Float32(Math.fround(a.value * b.value));
    }

    static divide(a: Float32, b: Float32): Float32 {
        return new Float32(Math.fround(a.value / b.value));
    }

    static pow(base: Float32, exponent: number): Float32 {
        return new Float32(Math.fround(Math.pow(base.value, exponent)));
    }

    static sqrt(a: Float32): Float32 {
        return new Float32(Math.fround(Math.sqrt(a.value)));
    }

    static exp(a: Float32): Float32 {
        return new Float32(Math.fround(Math.exp(a.value)));
    }

    static max(a: Float32, b: Float32): Float32 {
        return new Float32(Math.fround(Math.max(a.value, b.value)));
    }

    toNumber(): number {
        return this.value;
    }
}