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. A little tiny horror

A little tiny horror

Scheduled Pinned Locked Moved The Weird and The Wonderful
35 Posts 17 Posters 163 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.
  • J jhwurmbach

    Robert Surtees wrote:

    I'm guessing the shift is a wee bit faster.

    Which, as was argued here, is very probably of no importance, while the resulting obfuscation of the intent of the calculation is important.

    Let's think the unthinkable, let's do the undoable, let's prepare to grapple with the ineffable itself, and see if we may not eff it after all.
    Douglas Adams, "Dirk Gently's Holistic Detective Agency"

    R Offline
    R Offline
    Robert Surtees
    wrote on last edited by
    #18

    jhwurmbach wrote:

    Robert Surtees wrote: I'm guessing the shift is a wee bit faster. Which, as was argued here, is very probably of no importance, while the resulting obfuscation of the intent of the calculation is important.

    I'd say it's important. Gary's XT doesn't have an 8087 plugged in.

    1 Reply Last reply
    0
    • R Robert Surtees

      David Kentley wrote:

      It's possible that, as an optimization, "fixing" this has zero impact on real world performance while (slightly) obfuscating the code.

      I'm pretty sure pow() only takes and returns doubles which means the original code is casting two ints in and one back out in addition to its internal pow goodness. I'm guessing the shift is a wee bit faster. :)

      CPalliniC Offline
      CPalliniC Offline
      CPallini
      wrote on last edited by
      #19

      Robert Surtees wrote:

      I'm pretty sure pow() only takes and returns doubles which means the original code is casting two ints in and one back out in addition to its internal pow goodness. I'm guessing the shift is a wee bit faster.

      VC++ compiler is smart enough to implement it using shift instead of pow. Anyway, IMHO shift syntax is far more clean. :)

      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
      [my articles]

      In testa che avete, signor di Ceprano?

      1 Reply Last reply
      0
      • L leppie

        Like:

        int pow(int i, int j)
        {
        switch (j)
        case 0: return 1;
        case 1: return i;
        case 2: return i * i;
        case 3: return i * i * i;
        case 4: return i * i * i * i;
        ...
        }

        ;P

        xacc.ide - now with IronScheme support
        IronScheme - 1.0 alpha 1 out now

        T Offline
        T Offline
        Tim Smith
        wrote on last edited by
        #20

        int pow(int i, int j) { switch (j) { default: return 0; case 0: return 1; case 32: i *= i; case 31: i *= i; case 30: i *= i; ... case 1: return i; } } .... ewwwww

        Tim Smith I'm going to patent thought. I have yet to see any prior art.

        L CPalliniC 2 Replies Last reply
        0
        • T Tim Smith

          int pow(int i, int j) { switch (j) { default: return 0; case 0: return 1; case 32: i *= i; case 31: i *= i; case 30: i *= i; ... case 1: return i; } } .... ewwwww

          Tim Smith I'm going to patent thought. I have yet to see any prior art.

          L Offline
          L Offline
          leppie
          wrote on last edited by
          #21

          Even better :p

          xacc.ide - now with IronScheme support
          IronScheme - 1.0 alpha 1 out now

          1 Reply Last reply
          0
          • C Chris Maunder

            Shift left is way cooler than pow any day. And in the end it's all about how good the code looks, eh? ;)

            cheers, Chris Maunder

            CodeProject.com : C++ MVP

            P Offline
            P Offline
            peterchen
            wrote on last edited by
            #22

            words to live code by! :D

            We are a big screwed up dysfunctional psychotic happy family - some more screwed up, others more happy, but everybody's psychotic joint venture definition of CP
            My first real C# project | Linkify!| FoldWithUs! | sighist

            1 Reply Last reply
            0
            • Q QuiJohn

              I'd have to see more of the code to decide if this really deserves to be a horror. It's possible that, as an optimization, "fixing" this has zero impact on real world performance while (slightly) obfuscating the code. I'm a much bigger fan of readable code than optimizations with negligible performance improvements. (Not that "2 << i" is that unreadable, but you get the idea.)


              Faith is a fine invention For gentlemen who see; But microscopes are prudent In an emergency!            -Emily Dickinson

              P Offline
              P Offline
              peterchen
              wrote on last edited by
              #23

              Can you guarantee that floating point inaccuracies + truncation in the cast doesn't introduce a problem? A quick check shows they don't on VC8, but I wouldn#t have bet on it. Further, if your compiler uses the canonical (if simplistic) implementation of double pow_simple(double x, double y) { return exp(y*log(x)); } you fail pretty quickly with pow(2,3) = 7.9999999999999982 To add a pitfall to a lurking bug: if you use the default %f specifier for that, it dutifully prints 8.000000, but truncates it to 7 when casting to int. Also, when using 64-bit integers, starting with pow(2,51) double loses on accuracy.

              We are a big screwed up dysfunctional psychotic happy family - some more screwed up, others more happy, but everybody's psychotic joint venture definition of CP
              My first real C# project | Linkify!| FoldWithUs! | sighist

              N 1 Reply Last reply
              0
              • C Chris Maunder

                Shift left is way cooler than pow any day. And in the end it's all about how good the code looks, eh? ;)

                cheers, Chris Maunder

                CodeProject.com : C++ MVP

                P Offline
                P Offline
                PaulPrice
                wrote on last edited by
                #24

                Words of wisdom, I can often spend hours getting my comments and keywords in the right places to make the code look good and colourful

                Paul

                1 Reply Last reply
                0
                • T Tim Smith

                  int pow(int i, int j) { switch (j) { default: return 0; case 0: return 1; case 32: i *= i; case 31: i *= i; case 30: i *= i; ... case 1: return i; } } .... ewwwww

                  Tim Smith I'm going to patent thought. I have yet to see any prior art.

                  CPalliniC Offline
                  CPalliniC Offline
                  CPallini
                  wrote on last edited by
                  #25

                  (stack madness variant)

                  int imul(int i, int j)
                  {
                  if (j==0 ) return 0;
                  else return imul(i,j-1) + i;
                  }

                  int ipow(int i, int j)
                  {
                  if (j==0) return 1;
                  else return imul(ipow(i,j-1) , i);
                  }

                  :-D

                  If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                  [my articles]

                  In testa che avete, signor di Ceprano?

                  1 Reply Last reply
                  0
                  • G Gary Wheeler

                    I found this expression in our current source code:

                    int i;
                    //...
                    (int)pow(2,i)

                    This was in code written by a senior developer :wtf:. I replaced it with the following expression:

                    (1 << i)

                    Software Zen: delete this;

                    C Offline
                    C Offline
                    ClementsDan
                    wrote on last edited by
                    #26

                    Do you work for the same company as me? I've seen someone write code just like that -- to create a bitmask.

                    1 Reply Last reply
                    0
                    • G Gary Wheeler

                      I found this expression in our current source code:

                      int i;
                      //...
                      (int)pow(2,i)

                      This was in code written by a senior developer :wtf:. I replaced it with the following expression:

                      (1 << i)

                      Software Zen: delete this;

                      T Offline
                      T Offline
                      Tim Carmichael
                      wrote on last edited by
                      #27

                      But... but... but... this ISN'T VB... Is it possible to have bad code written by a senior developer in a non-VB language??? ;P

                      R 1 Reply Last reply
                      0
                      • G Gary Wheeler

                        I found this expression in our current source code:

                        int i;
                        //...
                        (int)pow(2,i)

                        This was in code written by a senior developer :wtf:. I replaced it with the following expression:

                        (1 << i)

                        Software Zen: delete this;

                        P Offline
                        P Offline
                        Pete OHanlon
                        wrote on last edited by
                        #28

                        Gary Wheeler wrote:

                        (1 << i)

                        Moral of the story. Shift happens.

                        Deja View - the feeling that you've seen this post before.

                        My blog | My articles

                        G 1 Reply Last reply
                        0
                        • C Chris Maunder

                          Shift left is way cooler than pow any day. And in the end it's all about how good the code looks, eh? ;)

                          cheers, Chris Maunder

                          CodeProject.com : C++ MVP

                          R Offline
                          R Offline
                          RichardM1
                          wrote on last edited by
                          #29

                          I'm uncomfortable saying this :sigh: No, it is all about the app meeting the requirements. One of the requirements (too often, only implied) is that the code be readable and maintainable. It may be that << is better looking than pow in some cases, but maybe not. What if that was coding a requirement directly? I would expect any coder worth his salt to understand and translate, but I know too many programmers that are not worth their salt.

                          I want to die like my Grandfather. Peaceful, Sleeping. Not screaming like his passengers.

                          1 Reply Last reply
                          0
                          • T Tim Carmichael

                            But... but... but... this ISN'T VB... Is it possible to have bad code written by a senior developer in a non-VB language??? ;P

                            R Offline
                            R Offline
                            RichardM1
                            wrote on last edited by
                            #30

                            We senior developers are very resourceful! :-\

                            I want to die like my Grandfather. Peaceful, Sleeping. Not screaming like his passengers.

                            1 Reply Last reply
                            0
                            • P Pete OHanlon

                              Gary Wheeler wrote:

                              (1 << i)

                              Moral of the story. Shift happens.

                              Deja View - the feeling that you've seen this post before.

                              My blog | My articles

                              G Offline
                              G Offline
                              Gary Wheeler
                              wrote on last edited by
                              #31

                              <DeepMysteriousVoice> You have become known to us. Your capacity for puns is disturbing... </DeepMysteriousVoice>

                              Software Zen: delete this;

                              P 1 Reply Last reply
                              0
                              • J jhwurmbach

                                Robert Surtees wrote:

                                I'm guessing the shift is a wee bit faster.

                                Which, as was argued here, is very probably of no importance, while the resulting obfuscation of the intent of the calculation is important.

                                Let's think the unthinkable, let's do the undoable, let's prepare to grapple with the ineffable itself, and see if we may not eff it after all.
                                Douglas Adams, "Dirk Gently's Holistic Detective Agency"

                                G Offline
                                G Offline
                                Gary Wheeler
                                wrote on last edited by
                                #32

                                Based on what he was using this for (constructing a bit string), I would say using the pow() obscured the intent more than the shift operation did.

                                Software Zen: delete this;

                                1 Reply Last reply
                                0
                                • G Gary Wheeler

                                  <DeepMysteriousVoice> You have become known to us. Your capacity for puns is disturbing... </DeepMysteriousVoice>

                                  Software Zen: delete this;

                                  P Offline
                                  P Offline
                                  Pete OHanlon
                                  wrote on last edited by
                                  #33

                                  Gary Wheeler wrote:

                                  You have become known to us. Your capacity for puns is disturbing...

                                  A curse upun you.

                                  Deja View - the feeling that you've seen this post before.

                                  My blog | My articles

                                  G 1 Reply Last reply
                                  0
                                  • P Pete OHanlon

                                    Gary Wheeler wrote:

                                    You have become known to us. Your capacity for puns is disturbing...

                                    A curse upun you.

                                    Deja View - the feeling that you've seen this post before.

                                    My blog | My articles

                                    G Offline
                                    G Offline
                                    Gary Wheeler
                                    wrote on last edited by
                                    #34

                                    Quoting Peter Lorre in The Raven[^]: "Ah! You defend yourself, you coward!" :laugh:

                                    Software Zen: delete this;

                                    1 Reply Last reply
                                    0
                                    • P peterchen

                                      Can you guarantee that floating point inaccuracies + truncation in the cast doesn't introduce a problem? A quick check shows they don't on VC8, but I wouldn#t have bet on it. Further, if your compiler uses the canonical (if simplistic) implementation of double pow_simple(double x, double y) { return exp(y*log(x)); } you fail pretty quickly with pow(2,3) = 7.9999999999999982 To add a pitfall to a lurking bug: if you use the default %f specifier for that, it dutifully prints 8.000000, but truncates it to 7 when casting to int. Also, when using 64-bit integers, starting with pow(2,51) double loses on accuracy.

                                      We are a big screwed up dysfunctional psychotic happy family - some more screwed up, others more happy, but everybody's psychotic joint venture definition of CP
                                      My first real C# project | Linkify!| FoldWithUs! | sighist

                                      N Offline
                                      N Offline
                                      Notorious_pd
                                      wrote on last edited by
                                      #35

                                      That is the first thing I thought of when I saw this. The thought of converting floating point to integers using casts keeps me awake at night. I am actually currently working on a project at work that has a few of these. I just started here at the time I noticed them. For fun, I thought it would be neat to try compiling this project (which was developed in VC++ 6.0) with VC 2005, and VC 2005 choked on these instances. Good job to the compiler team on that one. It also choked on a few things that I thought VC++ 6.0 should have, such as assigning an int literal to a CString. A few more bonus points to the team there, although 6.0 definitely should have caught that. Of the pow(int,int)s, at least one could've used the x *= 2 form, as it was in a loop, and the others the shift form. In the end, I decided not to touch any of them, because for all I know the truncation does mess things up but somebody spent a bunch of hours putting in "fixes" to work around this. The routines in question don't seem to have any bugs for now, but since a lot of the other ones do, I decided to focus my energy on those.

                                      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