Wrong on so many levels
-
BOOL IsNegative(float val) {
char buffer[10];
char* ptr;
char* strptr;ptr = buffer; sprintf(ptr, "%f", val); strptr = strstr(ptr, "-"); if (strptr == NULL) return FALSE; if (\*buffer + (strptr-ptr) == '-') return TRUE; return FALSE;
}
A friend posted me this when he worked in the finance sector. I recreated it from memory and I'am sure it was slightly worse than this.
-
BOOL IsNegative(float val) {
char buffer[10];
char* ptr;
char* strptr;ptr = buffer; sprintf(ptr, "%f", val); strptr = strstr(ptr, "-"); if (strptr == NULL) return FALSE; if (\*buffer + (strptr-ptr) == '-') return TRUE; return FALSE;
}
A friend posted me this when he worked in the finance sector. I recreated it from memory and I'am sure it was slightly worse than this.
Hahaha, I've seen things like that before. It always makes me feel good since I know that despite how poor a programmer I really am, there is someone worse out there.
-
BOOL IsNegative(float val) {
char buffer[10];
char* ptr;
char* strptr;ptr = buffer; sprintf(ptr, "%f", val); strptr = strstr(ptr, "-"); if (strptr == NULL) return FALSE; if (\*buffer + (strptr-ptr) == '-') return TRUE; return FALSE;
}
A friend posted me this when he worked in the finance sector. I recreated it from memory and I'am sure it was slightly worse than this.
ARopo wrote:
in the finance sector
Then he should have also checked for parentheses and red ink.
-
BOOL IsNegative(float val) {
char buffer[10];
char* ptr;
char* strptr;ptr = buffer; sprintf(ptr, "%f", val); strptr = strstr(ptr, "-"); if (strptr == NULL) return FALSE; if (\*buffer + (strptr-ptr) == '-') return TRUE; return FALSE;
}
A friend posted me this when he worked in the finance sector. I recreated it from memory and I'am sure it was slightly worse than this.
what I really admire about this is the somewhere it was called and tested like this
if (IsNegative(flVal))
instead of
if (flVal < 0)
though you could argue that the IsNegative function makes it clear to read at the calling end but then you could write the function like this
BOOL IsNegative(float flVal) { return (flVal < 0); }
-
BOOL IsNegative(float val) {
char buffer[10];
char* ptr;
char* strptr;ptr = buffer; sprintf(ptr, "%f", val); strptr = strstr(ptr, "-"); if (strptr == NULL) return FALSE; if (\*buffer + (strptr-ptr) == '-') return TRUE; return FALSE;
}
A friend posted me this when he worked in the finance sector. I recreated it from memory and I'am sure it was slightly worse than this.
Doesn't this relate back to the discussion on floating point numbers and epsilon in another thread? My C is a bit rusty (and was never good to start off with: it is the language of Satan and has driven better men than me insane) but I have a feeling that if you just compare a float to 0, you may not get the answer you expect.
-
Doesn't this relate back to the discussion on floating point numbers and epsilon in another thread? My C is a bit rusty (and was never good to start off with: it is the language of Satan and has driven better men than me insane) but I have a feeling that if you just compare a float to 0, you may not get the answer you expect.
-
Doesn't this relate back to the discussion on floating point numbers and epsilon in another thread? My C is a bit rusty (and was never good to start off with: it is the language of Satan and has driven better men than me insane) but I have a feeling that if you just compare a float to 0, you may not get the answer you expect.
No, most often it does not. There shouldn't be much discussion about the sign of a number. If you want to know whether it is negative, just use
x < 0
. That is quite different from the question "have we reached our target yet?", which does require some slack ("epsilon") to cope with rounding errors and other minor inaccuracies. There is one exception: the result of some operation could be an extremely small number (say -1.E-500), which is mathematically negative, but can not be represented in a 64-bit floating-point number, and then gets stored as zero (or maybe "NotANumber"). :)Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read formatted code with indentation, so please use PRE tags for code snippets.
I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).
-
BOOL IsNegative(float val) {
char buffer[10];
char* ptr;
char* strptr;ptr = buffer; sprintf(ptr, "%f", val); strptr = strstr(ptr, "-"); if (strptr == NULL) return FALSE; if (\*buffer + (strptr-ptr) == '-') return TRUE; return FALSE;
}
A friend posted me this when he worked in the finance sector. I recreated it from memory and I'am sure it was slightly worse than this.
Reminds me of my computer science class in high school lol