Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Other Discussions
  3. The Weird and The Wonderful
  4. Wrong on so many levels

Wrong on so many levels

Scheduled Pinned Locked Moved The Weird and The Wonderful
performance
8 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    ARopo
    wrote on last edited by
    #1

    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.

    C P A D E 5 Replies Last reply
    0
    • A ARopo

      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.

      C Offline
      C Offline
      Chris Boden
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • A ARopo

        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.

        P Offline
        P Offline
        PIEBALDconsult
        wrote on last edited by
        #3

        ARopo wrote:

        in the finance sector

        Then he should have also checked for parentheses and red ink.

        1 Reply Last reply
        0
        • A ARopo

          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.

          A Offline
          A Offline
          ARopo
          wrote on last edited by
          #4

          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); }

          1 Reply Last reply
          0
          • A ARopo

            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.

            D Offline
            D Offline
            David Skelly
            wrote on last edited by
            #5

            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.

            A L 2 Replies Last reply
            0
            • D David Skelly

              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.

              A Offline
              A Offline
              ARopo
              wrote on last edited by
              #6

              Sometime you can get odd rounding errors when dealing with high precision, but sprintf a float into a buffer and you're more likely to introduce rounding errors, %f usually defaults to 6 decimial places

              1 Reply Last reply
              0
              • D David Skelly

                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.

                L Offline
                L Offline
                Luc Pattyn
                wrote on last edited by
                #7

                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).


                1 Reply Last reply
                0
                • A ARopo

                  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.

                  E Offline
                  E Offline
                  elchupathingy
                  wrote on last edited by
                  #8

                  Reminds me of my computer science class in high school lol

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups