473,399 Members | 3,656 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Just plain crap?

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::operator=(const BigFloat &);
BigFloat & BigFloat::operator=(u32);
BigFloat & BigFloat::operator=(s32);
const BigFloat & BigFloat::operator+=(const BigFloat &);
friend BigFloat operator+(const BigFloat &, const BigFloat &);
const BigFloat & BigFloat::operator-=(const BigFloat &);
friend BigFloat operator-(const BigFloat &, const BigFloat &);
const BigFloat & BigFloat::operator*=(const BigFloat &);
const BigFloat & BigFloat::operator*=(u32);
const BigFloat & BigFloat::operator*=(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::operator/=(const BigFloat &);
const BigFloat & BigFloat::operator/=(u32);
const BigFloat & BigFloat::operator/=(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...

Jun 22 '07 #1
6 1351
mike3 wrote:
Hi.

[...good questions, valid questions, regarding BigFloat UDT...]
Please don't use a double-dash line to separate pieces of your message,
some crappy newsreaders treat them as signature separators and throw
away any text after them when quoting your message. I am using one of
those crappy newsreaders, and too lazy to manually quote the whole
thing to comment on it in detail.

Regarding your code: I think it _could_ be distilled a bit, but as it
stands now, it's fine. A bit verbose, perhaps, but fine. You could
pick better (maybe shorter) names for your errors (by creative use of
namespaces, for example). You could reduce the number of overloaded
arithmetic and comparison operators by introducing special c-tors from
'u32' or 's32' types.

Remeber, that this code, once it's written, debugged, and published,
in most cases is going to be looked at only by the compiler. If you
feel up to it, you can scare the sh!t out of yourself by looking at
the standard library implementation of 'std::set', for example.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 22 '07 #2
On Jun 22, 7:03 am, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
mike3 wrote:
Hi.
[...good questions, valid questions, regarding BigFloat UDT...]

Please don't use a double-dash line to separate pieces of your message,
some crappy newsreaders treat them as signature separators and throw
away any text after them when quoting your message. I am using one of
those crappy newsreaders, and too lazy to manually quote the whole
thing to comment on it in detail.
Well alright. Would stars (*) work?
Regarding your code: I think it _could_ be distilled a bit, but as it
stands now, it's fine. A bit verbose, perhaps, but fine. You could
pick better (maybe shorter) names for your errors (by creative use of
namespaces, for example). You could reduce the number of overloaded
arithmetic and comparison operators by introducing special c-tors from
'u32' or 's32' types.
However it's slower, at least for small numbers. See, the constructor
has to both allocate the memory and make the number (and normalize
it too to boot!). Even if you optimize it it's still going to be
slower.
Then when you do not use the thing again, it throws it away. I suppose
I could go and keep around a few "little" BigFloats to hold the
numbers but that just seems weird to me. (Like if my algorithm needs
to multiply by 2, keeping a BigFloat labeled "Two" around there just
seems odd.)
Remeber, that this code, once it's written, debugged, and published,
in most cases is going to be looked at only by the compiler. If you
feel up to it, you can scare the sh!t out of yourself by looking at
the standard library implementation of 'std::set', for example.
Where can I find that? It must look really awful.
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask

Jun 22 '07 #3
mike3 wrote:
On Jun 22, 7:03 am, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
>[..] If you
feel up to it, you can scare the sh!t out of yourself by looking at
the standard library implementation of 'std::set', for example.

Where can I find that? It must look really awful.
Start with <setheader (which is most likely just a file somewhere
on your hard drive).

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 22 '07 #4
On Jun 22, 8:41 pm, mike3 <mike4...@yahoo.comwrote:
On Jun 22, 7:03 am, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
mike3 wrote:
[...good questions, valid questions, regarding BigFloat UDT...]
Please don't use a double-dash line to separate pieces of your message,
some crappy newsreaders treat them as signature separators and throw
away any text after them when quoting your message. I am using one of
those crappy newsreaders, and too lazy to manually quote the whole
thing to comment on it in detail.
Well alright. Would stars (*) work?
Even three hyphens.

[...]
Remeber, that this code, once it's written, debugged, and published,
in most cases is going to be looked at only by the compiler.
That statement is simply false. Hopefully, at least, the code
will be maintained.
If you
feel up to it, you can scare the sh!t out of yourself by looking at
the standard library implementation of 'std::set', for example.
Where can I find that? It must look really awful.
It's part of the your compiler. It often doesn't look too good,
but usually at least, just because of the naming conventions
that the standard requires. For the rest, the implementations
I've looked at are a good deal cleaner than a lot of the code I
see elsewhere.

--
James Kanze (Gabi Software) email: ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Jun 22 '07 #5

mike3 wrote in message...
>
ref: 'std::set'

Where can I find that? It must look really awful.
If you will read other posts in this NG, you'll find *lots* of information
and links to sites.

Start here:
http://www.parashift.com/c++-faq-lite/

Dinkumware site:
http://www.dinkumware.com/manuals/.

--
Bob R
POVrookie
Jun 22 '07 #6

James Kanze wrote in message...
On Jun 22, 8:41 pm, mike3 <mike4...@yahoo.comwrote:
[...]
ref: 'std::set'
>Where can I find that? It must look really awful.
>It's part of the your compiler. It often doesn't look too good,
It's like that old song, " ....she may be ugly, but she sure can cook.".

--
Bob <GR
POVrookie
Jun 23 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: Scott Townsend | last post by:
I have a form that I allow the user to use the ALT-S key to submit. I'd like to have just the S in the Submit button underlined, Is this possible? Thanks, Scott<-
47
by: Theatre Mgmt | last post by:
Sun project seeks to solve Java memory, execution issues http://story.news.yahoo.com/news?tmpl=story&u=/infoworld/20050325/tc_infoworld/58059&e=1&ncid= San Francisco (InfoWorld) - Sun...
6
by: David Allison | last post by:
Relationship problem ~ How to tell Owner his db is crap. His db was made in 1999 - 12 tables - none normalised - none with relationships.(YES REALLY) He only knows about inputing data - 22,000...
25
by: Tim | last post by:
Dear Developers, Firstly, I'm not sure where this post should go so I apologise if this is in the wrong group or area. I'm currently interviewing for a vb.net developer who doesn't mind...
2
by: Peter Nilsson | last post by:
In a post regarding toupper(), Richard Heathfield once asked me to think about what the conversion of a char to unsigned char would mean, and whether it was sensible to actually do so. And pete has...
2
by: Mike Bridge | last post by:
Is there any way to get Internet explorer to treat a text/plain .net page as plain text using asp.net? It seems like IE doesn't trust text/plain as a mime type, and so it (ironically) displays it...
19
by: MajorSetback | last post by:
I am starting up a home business and will be setting up a web site to market the software that I will be developing in C++. I was wondering if I should buy something like Dreamweaver or Frontpage...
66
by: mensanator | last post by:
Probably just me. I've only been using Access and SQL Server for 12 years, so I'm sure my opinions don't count for anything. I was, nevertheless, looking forward to Sqlite3. And now that gmpy...
31
by: Sam of California | last post by:
Is it accurate to say that "the preprocessor is just a pass in the parsing of the source file"? I responded to that comment by saying that the preprocessor is not just a pass. It processes...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.