I started to try and implement new data types, specifically floating-point values, using C. I came up with this sort of way on implementing binary128
using longer_int
which I haven't fully understood.
The way how I started was to implement it via copying the code and modifying it. However, the code is C++, and C doesn't support OOP, so what I have to do is to try and translate the code.
I can't find the C version for
longer_int
because this question I once owned (the same account but now deleted) is now deleted and I can't view the links.
With the help of these C++ files, how can I implement binary128
using C?
And just so this question won't get closed, I want to know how to translate the OOP format in
longer_int.h
.class longer_int { private: char num_str[]; public: longer_int(); longer_int(const int &num); longer_int(const char &num[]); longer_int(const longer_int &num); // Sample code from it, now translated to C, but still OOP
You should begin with something like this: a struct
instead of a class
, ignore the public
/private
keywords, the constructors are methods that return the my_int128
. There is no overloading in C, so each different constructor must have a different name. Another school of thought would say that you don't need specialized constructors, but simply special method setters that can copy data from an int
, from a char[]
and from a my_int128
(and I prefer this school of thought). I give an example setter at the end.
#include <stdio.h>
#include <string.h>
typedef struct
{
char num_str[16]; /* fixed to int128 */
} my_int128;
/* Don't need this: my_int128 li = { 0 }; is enough!
my_int128 create_int128()
{
my_int128 li = { 0 };
return li;
}
*/
my_int128 create_int128_from_num(int num)
{
my_int128 li = { 0 };
/* TODO: set li with num */
return li;
}
my_int128 create_int128_from_char_array(const char num[16])
{
my_int128 li = { 0 };
memcpy(li.num_str, num, 16);
return li;
}
my_int128 create_int128_from_int128_ptr(const my_int128 *num)
{
return create_int128_from_char_array(num->num_str);
}
The setter:
void set_from_int(my_int128 *li, int num)
{
/* TODO: set li with num */
}