-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfloat.c
115 lines (101 loc) · 2.79 KB
/
float.c
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*
** EPITECH PROJECT, 2024
** B-PDG-300-BDX-3-1-PDGRUSH2-marius.marolleau
** File description:
** float
*/
#include "rush.h"
#include "float.h"
#include "char.h"
#include "new.h"
#include <stdio.h>
static void float_ctor(float_class_t *this, va_list *args)
{
if (!this || !args)
raise("Null pointer passed");
this->v = va_arg(*args, double);
}
static void float_dtor(float_class_t *this)
{
if (!this)
raise("Null pointer passed");
this->v = 0;
}
static char *float_string(float_class_t *this)
{
char *ptr = NULL;
size_t size_res = 0;
if (!this)
raise("Null pointer passed");
size_res = snprintf(NULL, 0, "<%s (%f)>", this->base.__name__, this->v);
ptr = malloc(sizeof(char) * size_res + 1);
sprintf(ptr, "<%s (%f)>", this->base.__name__, this->v);
return ptr;
}
static
float_class_t *float_add(const float_class_t *this, const float_class_t *other)
{
if (!this || !other)
raise("Null pointer passed");
return new(Float, this->v + other->v);
}
static
float_class_t *float_sub(const float_class_t *this, const float_class_t *other)
{
if (!this || !other)
raise("Null pointer passed");
return new(Float, this->v - other->v);
}
static
float_class_t *float_mul(const float_class_t *this, const float_class_t *other)
{
if (!this || !other)
raise("Null pointer passed");
return new(Float, this->v * other->v);
}
static
float_class_t *float_div(const float_class_t *this, const float_class_t *other)
{
if (!this || !other)
raise("Null pointer passed");
return new(Float, this->v / other->v);
}
static
bool float_eq(const float_class_t *this, const float_class_t *other)
{
if (!this || !other)
raise("Null pointer passed");
return this->v == other->v;
}
static
bool float_lt(const float_class_t *this, const float_class_t *other)
{
if (!this || !other)
raise("Null pointer passed");
return this->v < other->v;
}
static
bool float_gt(const float_class_t *this, const float_class_t *other)
{
if (!this || !other)
raise("Null pointer passed");
return this->v > other->v;
}
static const float_class_t _description = {
{ /* Class struct */
.__size__ = sizeof(float_class_t),
.__name__ = "Float",
.__ctor__ = (ctor_t)&float_ctor,
.__dtor__ = (dtor_t)&float_dtor,
.__str__ = (to_string_t)&float_string,
.__add__ = (binary_operator_t)&float_add,
.__sub__ = (binary_operator_t)&float_sub,
.__mul__ = (binary_operator_t)&float_mul,
.__div__ = (binary_operator_t)&float_div,
.__eq__ = (binary_comparator_t)&float_eq,
.__gt__ = (binary_comparator_t)&float_gt,
.__lt__ = (binary_comparator_t)&float_lt
},
.v = 0
};
const class_t *Float = (const class_t *)&_description;