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. Ever heard of casting?

Ever heard of casting?

Scheduled Pinned Locked Moved The Weird and The Wonderful
question
33 Posts 10 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.
  • L Luc Pattyn

    chevu wrote:

    how can you say algo i gave takes more time to debug.. complexity of given algo is O(n)

    O(n) and O(lg(n)) apply to execution time, not debugging time. There are no formulas for debugging time; it depends on number of statements, decision points, readability of code, and initial number of bugs. Your code has more than 5 bugs, it will take you lots of time to find all of them. I suggest you try and fix and run it until the result is correct. :|

    Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


    I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.


    J Offline
    J Offline
    johannesnestler
    wrote on last edited by
    #24

    thank you for answering to chevu - he didn't get the hint - "debug time" :rose:

    1 Reply Last reply
    0
    • C chevu

      sorry i messed up with code... This one is correct code

      decimal res = 10;
      decimal multi = 10;
      decimal rem = 1;
      int pow = 18;//For pow 0 you can directly return with 0

      while(pow > 1)
      {
      res *= res;
      rem *= (pow%2)? multi:1;
      pow /= 2;
      }
      res *= rem;

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #25

      Here's how:

          static decimal pow(decimal x, **u**int n)
          {
              decimal result = 1;
              while (n > 0)
              {
                  if ((n & 1) == 1)
                  {
                      result \*= x;
                  }
                  n >>= 1;
                  if (n == 0)
                      break;   //not nice, but needed in case x\*x overflow on the last step
                  x \*= x;
              }
              return result;
          }
      

      Good luck! ok so it's not the best possible code, I just hacked it together, but it works (tested)

      modified on Friday, March 5, 2010 11:40 AM

      L 1 Reply Last reply
      0
      • L Lost User

        Here's how:

            static decimal pow(decimal x, **u**int n)
            {
                decimal result = 1;
                while (n > 0)
                {
                    if ((n & 1) == 1)
                    {
                        result \*= x;
                    }
                    n >>= 1;
                    if (n == 0)
                        break;   //not nice, but needed in case x\*x overflow on the last step
                    x \*= x;
                }
                return result;
            }
        

        Good luck! ok so it's not the best possible code, I just hacked it together, but it works (tested)

        modified on Friday, March 5, 2010 11:40 AM

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

        harold aptroot wrote:

        (tested)

        that is unacceptable. This is the Coding Horrors forum after all. You're expected to publish something that is completely wrong, yet claim it is correct. :)

        Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


        I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.


        L 1 Reply Last reply
        0
        • L Luc Pattyn

          harold aptroot wrote:

          (tested)

          that is unacceptable. This is the Coding Horrors forum after all. You're expected to publish something that is completely wrong, yet claim it is correct. :)

          Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


          I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.


          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #27

          Oops! Sorry :)

          L 1 Reply Last reply
          0
          • L Lost User

            Oops! Sorry :)

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

            No problem. Anyway, it fails for negative n. :)

            Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


            I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.


            L 1 Reply Last reply
            0
            • L Luc Pattyn

              No problem. Anyway, it fails for negative n. :)

              Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


              I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.


              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #29

              It's not supposed to work for negative n anyway

              L 1 Reply Last reply
              0
              • L Lost User

                It's not supposed to work for negative n anyway

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

                I didn't see any specs; you could have tested and thrown an InvalidArgumentException; or made the second parameter a uint. [EDIT]Negative exponents result in divisions, which for integers tend to yield either 0 or 1 depending on the value of a.[/EDIT] :)

                Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.


                L 1 Reply Last reply
                0
                • L Luc Pattyn

                  I didn't see any specs; you could have tested and thrown an InvalidArgumentException; or made the second parameter a uint. [EDIT]Negative exponents result in divisions, which for integers tend to yield either 0 or 1 depending on the value of a.[/EDIT] :)

                  Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                  I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.


                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #31

                  Is that really necessary..? Fortunately adding just 1 letter of code takes the problem away.

                  1 Reply Last reply
                  0
                  • A ArchimaX

                    This is an excerpt from some sample code provided in the documentation for an EFT interface

                    decimal divider;
                    // we need 10^18, but Math.Pow does not support decimal
                    // types and decimal does not provide a power function
                    divider = 10*10*10;
                    divider = Decimal.Multiply(divider,10*10*10*10*10);
                    divider = Decimal.Multiply(divider,10*10*10*10*10);
                    divider = Decimal.Multiply(divider,10*10*10*10*10);

                    Blows my mind :-)

                    R Offline
                    R Offline
                    Rob Grainger
                    wrote on last edited by
                    #32

                    Not sure a cast is the best approach here - decimal types are OK for currency and other situations where accuracy is critical. Floating point types can introduce rounding errors - it all depends how the value is used. Best solution I've seen in the comments is 1E18M, but really 1E19M may be better ;-)

                    A 1 Reply Last reply
                    0
                    • R Rob Grainger

                      Not sure a cast is the best approach here - decimal types are OK for currency and other situations where accuracy is critical. Floating point types can introduce rounding errors - it all depends how the value is used. Best solution I've seen in the comments is 1E18M, but really 1E19M may be better ;-)

                      A Offline
                      A Offline
                      Avi Berger
                      wrote on last edited by
                      #33

                      Yes! I'm sorry I could only give you one five. When the problem domain calls for decimal calculations rather than floating point, casting from a floating point type would be a greater horror.

                      Please do not read this signature.

                      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