M
mike3
Hi.
I was programming this thing, and for some reason this just doesn't
"feel" right:
--
Primer on what this is supposed to be.
1. First of all, "u8" = unsigned char, "s16" = signed short, "s32" =
signed long,
"DIGIT32PTR" = DIGIT32 *, and DIGIT32 = unsigned long = "u32",
2. "BigFloat" = "big" (as in, multiprecision) "float"ing point number,
3. sign field = sign (1 for negative, 0 for positive),
4. err = error flag (if a computation fails, this is set.),
5. exp = exponent (from -32768 to 32767),
6. length = length of number in DIGIT32s,
7. digits = pointer to array of "length"'s worth of DIGIT32s that
holds
the significand of the number, least significant first -- MSB is
always
1 for normalized representation. This is allocated by the
constructors,
and freed by the destructors (BigFloat() and ~BigFloat(),
respectively).
8. All the operators there are comparison, addition, subtraction,
multiplication, and division.
9. "FG3DError" is a type used to return error values when the given
operation fails.
--
--- begin big long and crappy piece of code ---
class BigFloat {
private:
/* Data fields */
u8 sign;
u8 err;
s16 exp;
int length;
DIGIT32PTR digits;
/* Friends */
friend FG3DError FG3DMPFloat_Normalize(BigFloat *);
friend FG3DError FG3DMPFloat_Copy(BigFloat *, const BigFloat *);
friend FG3DError FG3DMPFloat_AddUnsigned(BigFloat *, const
BigFloat *, const BigFloat *);
friend FG3DError FG3DMPFloat_SubUnsigned(BigFloat *, const
BigFloat *, const BigFloat *);
friend FG3DError FG3DMPFloat_MulUnsigned(BigFloat *, const
BigFloat *, const BigFloat *);
friend FG3DError FG3DMPFloat_MulUnsignedSmall(BigFloat *, const
BigFloat *, DIGIT32);
friend FG3DError FG3DMPFloat_DivUnsigned(BigFloat *, const
BigFloat *, const BigFloat *);
friend FG3DError FG3DMPFloat_DivUnsignedSmall(BigFloat *, const
BigFloat *, DIGIT32);
friend FG3DError FG3DMPFloat_CmpUnsigned(int *, const BigFloat
*, const BigFloat *);
friend FG3DError FG3DMPFloat_CmpUnsignedSmall(int *, const
BigFloat *, DIGIT32);
friend FG3DError FG3DMPFloat_Add(BigFloat *, const BigFloat *,
const BigFloat *);
friend FG3DError FG3DMPFloat_Sub(BigFloat *, const BigFloat *,
const BigFloat *);
friend FG3DError FG3DMPFloat_Mul(BigFloat *, const BigFloat *,
const BigFloat *);
friend FG3DError FG3DMPFloat_MulSmallU(BigFloat *, const
BigFloat *, u32);
friend FG3DError FG3DMPFloat_MulSmallS(BigFloat *, const
BigFloat *, s32);
friend FG3DError FG3DMPFloat_Div(BigFloat *, const BigFloat *,
const BigFloat *);
friend FG3DError FG3DMPFloat_DivSmallU(BigFloat *, const
BigFloat *, u32);
friend FG3DError FG3DMPFloat_DivSmallS(BigFloat *, const
BigFloat *, s32);
friend FG3DError FG3DMPFloat_Cmp(int *, const BigFloat *, const
BigFloat *);
friend FG3DError FG3DMPFloat_CmpSmallU(int *, const BigFloat *,
u32);
friend FG3DError FG3DMPFloat_CmpSmallS(int *, const BigFloat *,
s32);
friend FG3DError FG3DMPFloat_PrintBin(LPTSTR, BigFloat *);
/* Private members */
FG3DError Init(int); /* initialize to a given
precision */
FG3DError InitNoZero(int); /* initialize to a given
precision with no zeroize prep */
FG3DError EquateUI(u32); /* equate to unsigned
integer */
FG3DError EquateSI(s32); /* equate to signed
integer */
FG3DError EquateBF(const BigFloat *); /* equate to another
BigFloat */
public:
BigFloat(); /* default constructor */
BigFloat(u32, int); /* construct to unsigned
small int with given precision */
BigFloat(s32, int); /* construct to signed
small int with given precision */
BigFloat(const BigFloat &); /* construct to another
BigFloat */
BigFloat(BOOL, int); /* construct without
initial zeroization (DANGEROUS!) */
~BigFloat(); /* default destructor */
DIGIT32 GetDigit(int); /* get a digit */
void SetDigit(DIGIT32, int); /* set a digit */
s16 GetExponent(); /* get exponent */
void SetExponent(s16 newexp); /* set exponent */
u8 GetSign(); /* get sign */
void SetSign(u8 newsign); /* set sign */
int GetLength(); /* get length */
FG3DError GetUI(u32 *); /* get unsigned integer
representation */
FG3DError GetSI(s32 *); /* get signed integer
representation */
/* Overloaded operators */
friend BOOL operator>=(const BigFloat &, const BigFloat &);
friend BOOL operator>=(const BigFloat &, const u32);
friend BOOL operator>=(const BigFloat &, const s32);
friend BOOL operator>(const BigFloat &, const BigFloat &);
friend BOOL operator>(const BigFloat &, const u32);
friend BOOL operator>(const BigFloat &, const s32);
friend BOOL operator==(const BigFloat &, const BigFloat &);
friend BOOL operator==(const BigFloat &, const u32);
friend BOOL operator==(const BigFloat &, const s32);
friend BOOL operator<(const BigFloat &, const BigFloat &);
friend BOOL operator<(const BigFloat &, const u32);
friend BOOL operator<(const BigFloat &, const s32);
friend BOOL operator<=(const BigFloat &, const BigFloat &);
friend BOOL operator<=(const BigFloat &, const u32);
friend BOOL operator<=(const BigFloat &, const s32);
BigFloat & BigFloat:perator=(const BigFloat &);
BigFloat & BigFloat:perator=(u32);
BigFloat & BigFloat:perator=(s32);
const BigFloat & BigFloat:perator+=(const BigFloat &);
friend BigFloat operator+(const BigFloat &, const BigFloat &);
const BigFloat & BigFloat:perator-=(const BigFloat &);
friend BigFloat operator-(const BigFloat &, const BigFloat &);
const BigFloat & BigFloat:perator*=(const BigFloat &);
const BigFloat & BigFloat:perator*=(u32);
const BigFloat & BigFloat:perator*=(s32);
friend BigFloat operator*(const BigFloat &, const BigFloat &);
friend BigFloat operator*(const BigFloat &, u32);
friend BigFloat operator*(const BigFloat &, s32);
friend BigFloat operator*(u32, const BigFloat &);
friend BigFloat operator*(s32, const BigFloat &);
const BigFloat & BigFloat:perator/=(const BigFloat &);
const BigFloat & BigFloat:perator/=(u32);
const BigFloat & BigFloat:perator/=(s32);
friend BigFloat operator/(const BigFloat &, const BigFloat &);
friend BigFloat operator/(const BigFloat &, u32);
friend BigFloat operator/(const BigFloat &, s32);
friend BigFloat operator/(u32, const BigFloat &);
friend BigFloat operator/(s32, const BigFloat &);
};
--- end big long crappy piece of code ---
Is this thing total crap? For example, is it bad to have so many
friends in there
like that?! I'd like you to laugh and giggle at this, and hopefully
offer a detailed,
but useful, critique (not a flame, but a good critique of the code).
This is my first attempt at doing something serious with C++ (I've
done a lot of
C before, but not much C++), so, naturally, if I totally effed it all
up I would not
be surprised. But I'd like to at least know...
I was programming this thing, and for some reason this just doesn't
"feel" right:
--
Primer on what this is supposed to be.
1. First of all, "u8" = unsigned char, "s16" = signed short, "s32" =
signed long,
"DIGIT32PTR" = DIGIT32 *, and DIGIT32 = unsigned long = "u32",
2. "BigFloat" = "big" (as in, multiprecision) "float"ing point number,
3. sign field = sign (1 for negative, 0 for positive),
4. err = error flag (if a computation fails, this is set.),
5. exp = exponent (from -32768 to 32767),
6. length = length of number in DIGIT32s,
7. digits = pointer to array of "length"'s worth of DIGIT32s that
holds
the significand of the number, least significant first -- MSB is
always
1 for normalized representation. This is allocated by the
constructors,
and freed by the destructors (BigFloat() and ~BigFloat(),
respectively).
8. All the operators there are comparison, addition, subtraction,
multiplication, and division.
9. "FG3DError" is a type used to return error values when the given
operation fails.
--
--- begin big long and crappy piece of code ---
class BigFloat {
private:
/* Data fields */
u8 sign;
u8 err;
s16 exp;
int length;
DIGIT32PTR digits;
/* Friends */
friend FG3DError FG3DMPFloat_Normalize(BigFloat *);
friend FG3DError FG3DMPFloat_Copy(BigFloat *, const BigFloat *);
friend FG3DError FG3DMPFloat_AddUnsigned(BigFloat *, const
BigFloat *, const BigFloat *);
friend FG3DError FG3DMPFloat_SubUnsigned(BigFloat *, const
BigFloat *, const BigFloat *);
friend FG3DError FG3DMPFloat_MulUnsigned(BigFloat *, const
BigFloat *, const BigFloat *);
friend FG3DError FG3DMPFloat_MulUnsignedSmall(BigFloat *, const
BigFloat *, DIGIT32);
friend FG3DError FG3DMPFloat_DivUnsigned(BigFloat *, const
BigFloat *, const BigFloat *);
friend FG3DError FG3DMPFloat_DivUnsignedSmall(BigFloat *, const
BigFloat *, DIGIT32);
friend FG3DError FG3DMPFloat_CmpUnsigned(int *, const BigFloat
*, const BigFloat *);
friend FG3DError FG3DMPFloat_CmpUnsignedSmall(int *, const
BigFloat *, DIGIT32);
friend FG3DError FG3DMPFloat_Add(BigFloat *, const BigFloat *,
const BigFloat *);
friend FG3DError FG3DMPFloat_Sub(BigFloat *, const BigFloat *,
const BigFloat *);
friend FG3DError FG3DMPFloat_Mul(BigFloat *, const BigFloat *,
const BigFloat *);
friend FG3DError FG3DMPFloat_MulSmallU(BigFloat *, const
BigFloat *, u32);
friend FG3DError FG3DMPFloat_MulSmallS(BigFloat *, const
BigFloat *, s32);
friend FG3DError FG3DMPFloat_Div(BigFloat *, const BigFloat *,
const BigFloat *);
friend FG3DError FG3DMPFloat_DivSmallU(BigFloat *, const
BigFloat *, u32);
friend FG3DError FG3DMPFloat_DivSmallS(BigFloat *, const
BigFloat *, s32);
friend FG3DError FG3DMPFloat_Cmp(int *, const BigFloat *, const
BigFloat *);
friend FG3DError FG3DMPFloat_CmpSmallU(int *, const BigFloat *,
u32);
friend FG3DError FG3DMPFloat_CmpSmallS(int *, const BigFloat *,
s32);
friend FG3DError FG3DMPFloat_PrintBin(LPTSTR, BigFloat *);
/* Private members */
FG3DError Init(int); /* initialize to a given
precision */
FG3DError InitNoZero(int); /* initialize to a given
precision with no zeroize prep */
FG3DError EquateUI(u32); /* equate to unsigned
integer */
FG3DError EquateSI(s32); /* equate to signed
integer */
FG3DError EquateBF(const BigFloat *); /* equate to another
BigFloat */
public:
BigFloat(); /* default constructor */
BigFloat(u32, int); /* construct to unsigned
small int with given precision */
BigFloat(s32, int); /* construct to signed
small int with given precision */
BigFloat(const BigFloat &); /* construct to another
BigFloat */
BigFloat(BOOL, int); /* construct without
initial zeroization (DANGEROUS!) */
~BigFloat(); /* default destructor */
DIGIT32 GetDigit(int); /* get a digit */
void SetDigit(DIGIT32, int); /* set a digit */
s16 GetExponent(); /* get exponent */
void SetExponent(s16 newexp); /* set exponent */
u8 GetSign(); /* get sign */
void SetSign(u8 newsign); /* set sign */
int GetLength(); /* get length */
FG3DError GetUI(u32 *); /* get unsigned integer
representation */
FG3DError GetSI(s32 *); /* get signed integer
representation */
/* Overloaded operators */
friend BOOL operator>=(const BigFloat &, const BigFloat &);
friend BOOL operator>=(const BigFloat &, const u32);
friend BOOL operator>=(const BigFloat &, const s32);
friend BOOL operator>(const BigFloat &, const BigFloat &);
friend BOOL operator>(const BigFloat &, const u32);
friend BOOL operator>(const BigFloat &, const s32);
friend BOOL operator==(const BigFloat &, const BigFloat &);
friend BOOL operator==(const BigFloat &, const u32);
friend BOOL operator==(const BigFloat &, const s32);
friend BOOL operator<(const BigFloat &, const BigFloat &);
friend BOOL operator<(const BigFloat &, const u32);
friend BOOL operator<(const BigFloat &, const s32);
friend BOOL operator<=(const BigFloat &, const BigFloat &);
friend BOOL operator<=(const BigFloat &, const u32);
friend BOOL operator<=(const BigFloat &, const s32);
BigFloat & BigFloat:perator=(const BigFloat &);
BigFloat & BigFloat:perator=(u32);
BigFloat & BigFloat:perator=(s32);
const BigFloat & BigFloat:perator+=(const BigFloat &);
friend BigFloat operator+(const BigFloat &, const BigFloat &);
const BigFloat & BigFloat:perator-=(const BigFloat &);
friend BigFloat operator-(const BigFloat &, const BigFloat &);
const BigFloat & BigFloat:perator*=(const BigFloat &);
const BigFloat & BigFloat:perator*=(u32);
const BigFloat & BigFloat:perator*=(s32);
friend BigFloat operator*(const BigFloat &, const BigFloat &);
friend BigFloat operator*(const BigFloat &, u32);
friend BigFloat operator*(const BigFloat &, s32);
friend BigFloat operator*(u32, const BigFloat &);
friend BigFloat operator*(s32, const BigFloat &);
const BigFloat & BigFloat:perator/=(const BigFloat &);
const BigFloat & BigFloat:perator/=(u32);
const BigFloat & BigFloat:perator/=(s32);
friend BigFloat operator/(const BigFloat &, const BigFloat &);
friend BigFloat operator/(const BigFloat &, u32);
friend BigFloat operator/(const BigFloat &, s32);
friend BigFloat operator/(u32, const BigFloat &);
friend BigFloat operator/(s32, const BigFloat &);
};
--- end big long crappy piece of code ---
Is this thing total crap? For example, is it bad to have so many
friends in there
like that?! I'd like you to laugh and giggle at this, and hopefully
offer a detailed,
but useful, critique (not a flame, but a good critique of the code).
This is my first attempt at doing something serious with C++ (I've
done a lot of
C before, but not much C++), so, naturally, if I totally effed it all
up I would not
be surprised. But I'd like to at least know...