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.
  • OriginalGriffO OriginalGriff

    Never mind casting - at least he didn't use a loop:

            decimal tenE18 = 1;
            for (int i = 0; i < 18; i++)
                {
                tenE18 \*= 10;
                }
    

    Still, I think I will stick to the old-fashioned, boring way:

            decimal d = 1E18M;
    

    [edit]Got the number of zeros wrong, didn't I? Oops.[/edit]

    You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy

    modified on Wednesday, March 3, 2010 11:21 AM

    C Offline
    C Offline
    chevu
    wrote on last edited by
    #3

    well you can do something that save time and processing time, try this decimal res = 10; decimal rem = 1; int pow = 18; while(pow > 0) { res *= res; rem *= (pow%2)? 1:10; pow /= 2; } res *= rem;

    J L L 3 Replies Last reply
    0
    • OriginalGriffO OriginalGriff

      Never mind casting - at least he didn't use a loop:

              decimal tenE18 = 1;
              for (int i = 0; i < 18; i++)
                  {
                  tenE18 \*= 10;
                  }
      

      Still, I think I will stick to the old-fashioned, boring way:

              decimal d = 1E18M;
      

      [edit]Got the number of zeros wrong, didn't I? Oops.[/edit]

      You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy

      modified on Wednesday, March 3, 2010 11:21 AM

      S Offline
      S Offline
      Stanciu Vlad
      wrote on last edited by
      #4

      OriginalGriff wrote:

      Still, I think I will stick to the old-fashioned, boring way:

      decimal d = 10E18M;

      //
      // TODO: Check the number is 10^18
      //

      decimal bigNumber = 1000000000000000000M;

      This makes the old-fashioned way more exciting :laugh:

      I have no smart signature yet...

      J 1 Reply Last reply
      0
      • OriginalGriffO OriginalGriff

        Never mind casting - at least he didn't use a loop:

                decimal tenE18 = 1;
                for (int i = 0; i < 18; i++)
                    {
                    tenE18 \*= 10;
                    }
        

        Still, I think I will stick to the old-fashioned, boring way:

                decimal d = 1E18M;
        

        [edit]Got the number of zeros wrong, didn't I? Oops.[/edit]

        You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy

        modified on Wednesday, March 3, 2010 11:21 AM

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

        run your code -> how many digits do you count? ;P

        OriginalGriffO 1 Reply Last reply
        0
        • C chevu

          well you can do something that save time and processing time, try this decimal res = 10; decimal rem = 1; int pow = 18; while(pow > 0) { res *= res; rem *= (pow%2)? 1:10; pow /= 2; } res *= rem;

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

          :wtf: test your code - it saves processing time, but raises debug time - if you are able to compile it ;P

          C 1 Reply Last reply
          0
          • J johannesnestler

            run your code -> how many digits do you count? ;P

            OriginalGriffO Offline
            OriginalGriffO Offline
            OriginalGriff
            wrote on last edited by
            #7

            I did - and checked they gave the same results before I posted it. ;P

            You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy

            "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
            "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

            J 1 Reply Last reply
            0
            • C chevu

              well you can do something that save time and processing time, try this decimal res = 10; decimal rem = 1; int pow = 18; while(pow > 0) { res *= res; rem *= (pow%2)? 1:10; pow /= 2; } res *= rem;

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

              that is completely wrong in many ways. did you try a simple example, say pow=1? :~

              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.


              1 Reply Last reply
              0
              • OriginalGriffO OriginalGriff

                I did - and checked they gave the same results before I posted it. ;P

                You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy

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

                :omg: lol - same results... Check it:

                using System;

                namespace DecimalTest
                {
                class Program
                {
                static void Main(string[] args)
                {
                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);
                Console.WriteLine(divider);

                        decimal tenE18 = 10;
                        for (int i = 0; i < 18; i++)
                            tenE18 \*= 10;
                        Console.WriteLine(tenE18);
                        Console.WriteLine(10E18M);
                
                        Console.WriteLine(String.Format("{0:N}", Math.Pow(10d, 18d)));
                     
                        Console.ReadKey();
                    }
                }
                

                }

                who invented those nasty zero!?! :laugh:

                OriginalGriffO 1 Reply Last reply
                0
                • J johannesnestler

                  :omg: lol - same results... Check it:

                  using System;

                  namespace DecimalTest
                  {
                  class Program
                  {
                  static void Main(string[] args)
                  {
                  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);
                  Console.WriteLine(divider);

                          decimal tenE18 = 10;
                          for (int i = 0; i < 18; i++)
                              tenE18 \*= 10;
                          Console.WriteLine(tenE18);
                          Console.WriteLine(10E18M);
                  
                          Console.WriteLine(String.Format("{0:N}", Math.Pow(10d, 18d)));
                       
                          Console.ReadKey();
                      }
                  }
                  

                  }

                  who invented those nasty zero!?! :laugh:

                  OriginalGriffO Offline
                  OriginalGriffO Offline
                  OriginalGriff
                  wrote on last edited by
                  #10

                  Yeah, I checked my results matched! :-O I didn't check his! Oops.

                  You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy

                  "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                  "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                  J 1 Reply Last reply
                  0
                  • S Stanciu Vlad

                    OriginalGriff wrote:

                    Still, I think I will stick to the old-fashioned, boring way:

                    decimal d = 10E18M;

                    //
                    // TODO: Check the number is 10^18
                    //

                    decimal bigNumber = 1000000000000000000M;

                    This makes the old-fashioned way more exciting :laugh:

                    I have no smart signature yet...

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

                    using System;

                    namespace DecimalTest
                    {
                    class Program
                    {
                    static void Main(string[] args)
                    {
                    decimal bigNumber = 1000000000000000000M;
                    decimal bigNumber2 = 10E18M;

                            Console.WriteLine("Is {0} the same as {1}?", bigNumber, bigNumber2);
                            if (bigNumber == bigNumber2)
                                Console.WriteLine("WTF???");
                            else
                                Console.WriteLine("No - Maths saved!");
                         
                            Console.ReadKey();
                        }
                    }
                    

                    }

                    1 Reply Last reply
                    0
                    • OriginalGriffO OriginalGriff

                      Yeah, I checked my results matched! :-O I didn't check his! Oops.

                      You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy

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

                      yea - it was even hard for me to check his crazy code - lol. There is some resistence to execute such a thing - :)

                      C 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 :-)

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

                        castling? sure, I do it every day. :)

                        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.


                        1 Reply Last reply
                        0
                        • C chevu

                          well you can do something that save time and processing time, try this decimal res = 10; decimal rem = 1; int pow = 18; while(pow > 0) { res *= res; rem *= (pow%2)? 1:10; pow /= 2; } res *= rem;

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

                          Is that an attempt at binary exponentiation?

                          1 Reply Last reply
                          0
                          • J johannesnestler

                            :wtf: test your code - it saves processing time, but raises debug time - if you are able to compile it ;P

                            C Offline
                            C Offline
                            chevu
                            wrote on last edited by
                            #15

                            it might have a syntax error but you can do it this way.. you can refer algorithm by corman... n compare to given algo for (int i = 0; i < 18; i++) { tenE18 *= 10; } this one will run for 18times.. for debuging also you have to go through this loop 18 times... then how can you say algo i gave takes more time to debug.. complexity of given algo is O(n) and complexity of algo i gave is O(lg(n))....

                            modified on Wednesday, March 3, 2010 10:37 PM

                            L 1 Reply Last reply
                            0
                            • C chevu

                              it might have a syntax error but you can do it this way.. you can refer algorithm by corman... n compare to given algo for (int i = 0; i < 18; i++) { tenE18 *= 10; } this one will run for 18times.. for debuging also you have to go through this loop 18 times... then how can you say algo i gave takes more time to debug.. complexity of given algo is O(n) and complexity of algo i gave is O(lg(n))....

                              modified on Wednesday, March 3, 2010 10:37 PM

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

                              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 1 Reply Last reply
                              0
                              • J johannesnestler

                                yea - it was even hard for me to check his crazy code - lol. There is some resistence to execute such a thing - :)

                                C Offline
                                C Offline
                                chevu
                                wrote on last edited by
                                #17

                                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 L 2 Replies 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
                                  Luc Pattyn
                                  wrote on last edited by
                                  #18

                                  chevu wrote:

                                  This one is correct code

                                  wrong again: 1. the result for pow=18 is wrong. 2. pow=5 and pow=6 give the same result??? I think you have abundantly proven now that your code has high debugging complexity. :(

                                  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.


                                  C 1 Reply Last reply
                                  0
                                  • L Luc Pattyn

                                    chevu wrote:

                                    This one is correct code

                                    wrong again: 1. the result for pow=18 is wrong. 2. pow=5 and pow=6 give the same result??? I think you have abundantly proven now that your code has high debugging complexity. :(

                                    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.


                                    C Offline
                                    C Offline
                                    chevu
                                    wrote on last edited by
                                    #19

                                    sorry dude... i had really forget to check till 18... coz of odd even cases that code will fail... I knw you people are getting irritated by now, but you can check this code

                                    double pow(long long a, long long b)
                                    {
                                    if(b == 0)
                                    return 1.0;
                                    else if(b == 1)
                                    return a;
                                    else if(b%2 == 0)
                                    return pow(a*a,b/2);
                                    else
                                    return a* pow(a*a,b/2);
                                    }

                                    i have tested this code upto long long limits...

                                    S J 2 Replies Last reply
                                    0
                                    • C chevu

                                      sorry dude... i had really forget to check till 18... coz of odd even cases that code will fail... I knw you people are getting irritated by now, but you can check this code

                                      double pow(long long a, long long b)
                                      {
                                      if(b == 0)
                                      return 1.0;
                                      else if(b == 1)
                                      return a;
                                      else if(b%2 == 0)
                                      return pow(a*a,b/2);
                                      else
                                      return a* pow(a*a,b/2);
                                      }

                                      i have tested this code upto long long limits...

                                      S Offline
                                      S Offline
                                      Stanciu Vlad
                                      wrote on last edited by
                                      #20

                                      Recursive functions brings color in life :-D

                                      I have no smart signature yet...

                                      1 Reply Last reply
                                      0
                                      • C chevu

                                        sorry dude... i had really forget to check till 18... coz of odd even cases that code will fail... I knw you people are getting irritated by now, but you can check this code

                                        double pow(long long a, long long b)
                                        {
                                        if(b == 0)
                                        return 1.0;
                                        else if(b == 1)
                                        return a;
                                        else if(b%2 == 0)
                                        return pow(a*a,b/2);
                                        else
                                        return a* pow(a*a,b/2);
                                        }

                                        i have tested this code upto long long limits...

                                        J Offline
                                        J Offline
                                        J4amieC
                                        wrote on last edited by
                                        #21

                                        chevu wrote:

                                        long long a

                                        If I were a canadian, thats how I would describe this thread.

                                        modified on Thursday, March 4, 2010 8:11 AM

                                        C 1 Reply Last reply
                                        0
                                        • J J4amieC

                                          chevu wrote:

                                          long long a

                                          If I were a canadian, thats how I would describe this thread.

                                          modified on Thursday, March 4, 2010 8:11 AM

                                          C Offline
                                          C Offline
                                          chevu
                                          wrote on last edited by
                                          #22

                                          what kind of comment was that?

                                          J 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