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. Fun with double

Fun with double

Scheduled Pinned Locked Moved The Weird and The Wonderful
csharpcom
12 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.
  • S Offline
    S Offline
    Super Lloyd
    wrote on last edited by
    #1

    I am doing a lot of computational maths work at home at the moment and, just for giggle, I thought of writing the following method. Code behave as incorrectly as expected! :-D

    static void Main(string[] args)
    {
    var e = 1e18;
    Console.WriteLine(IsEven(e));
    Console.WriteLine(IsEven(e + 1));
    Console.WriteLine(IsEven(-e));
    Console.WriteLine(IsEven(-e + 1));
    }
    bool IsEven(double d)
    {
    if (d < 0) return (d % 2) > -1;
    return (d % 2) < 1;
    }

    A new .NET Serializer All in one Menu-Ribbon Bar Taking over the world since 1371!

    D K realJSOPR 3 Replies Last reply
    0
    • S Super Lloyd

      I am doing a lot of computational maths work at home at the moment and, just for giggle, I thought of writing the following method. Code behave as incorrectly as expected! :-D

      static void Main(string[] args)
      {
      var e = 1e18;
      Console.WriteLine(IsEven(e));
      Console.WriteLine(IsEven(e + 1));
      Console.WriteLine(IsEven(-e));
      Console.WriteLine(IsEven(-e + 1));
      }
      bool IsEven(double d)
      {
      if (d < 0) return (d % 2) > -1;
      return (d % 2) < 1;
      }

      A new .NET Serializer All in one Menu-Ribbon Bar Taking over the world since 1371!

      D Offline
      D Offline
      Daniel Pfeffer
      wrote on last edited by
      #2

      What makes this even nicer is that both 1.0e+18 and 1.0 are exactly representable as a double, but their sum isn't. A nice demonstration of one of the pitfalls of floating-point arithmetic.

      Ad astra - both ways!

      S 1 Reply Last reply
      0
      • D Daniel Pfeffer

        What makes this even nicer is that both 1.0e+18 and 1.0 are exactly representable as a double, but their sum isn't. A nice demonstration of one of the pitfalls of floating-point arithmetic.

        Ad astra - both ways!

        S Offline
        S Offline
        Super Lloyd
        wrote on last edited by
        #3

        ha, yeah, it's better than summing up 0.1 10 times, since 0.1 is an approximation to start with... :-D

        A new .NET Serializer All in one Menu-Ribbon Bar Taking over the world since 1371!

        1 Reply Last reply
        0
        • S Super Lloyd

          I am doing a lot of computational maths work at home at the moment and, just for giggle, I thought of writing the following method. Code behave as incorrectly as expected! :-D

          static void Main(string[] args)
          {
          var e = 1e18;
          Console.WriteLine(IsEven(e));
          Console.WriteLine(IsEven(e + 1));
          Console.WriteLine(IsEven(-e));
          Console.WriteLine(IsEven(-e + 1));
          }
          bool IsEven(double d)
          {
          if (d < 0) return (d % 2) > -1;
          return (d % 2) < 1;
          }

          A new .NET Serializer All in one Menu-Ribbon Bar Taking over the world since 1371!

          K Offline
          K Offline
          Kirk 10389821
          wrote on last edited by
          #4

          When you start with approximations... You should conclude with "apporximately correct" = "correct" There are ONLY Zeroes and Ones... Everything else is up for interpretation!

          S 1 Reply Last reply
          0
          • K Kirk 10389821

            When you start with approximations... You should conclude with "apporximately correct" = "correct" There are ONLY Zeroes and Ones... Everything else is up for interpretation!

            S Offline
            S Offline
            Super Lloyd
            wrote on last edited by
            #5

            01001100 01101001 01101011 01100101 00101100 00100000 01110100 01101111 01110100 01100001 01101100 01101100 01111001 00100000 01101101 01100001 01101110 00100001

            A new .NET Serializer All in one Menu-Ribbon Bar Taking over the world since 1371!

            K raddevusR 2 Replies Last reply
            0
            • S Super Lloyd

              01001100 01101001 01101011 01100101 00101100 00100000 01110100 01101111 01110100 01100001 01101100 01101100 01111001 00100000 01101101 01100001 01101110 00100001

              A new .NET Serializer All in one Menu-Ribbon Bar Taking over the world since 1371!

              K Offline
              K Offline
              Kirk 10389821
              wrote on last edited by
              #6

              No finer compliment to a response has EVER been paid... Thank You!

              1 Reply Last reply
              0
              • S Super Lloyd

                01001100 01101001 01101011 01100101 00101100 00100000 01110100 01101111 01110100 01100001 01101100 01101100 01111001 00100000 01101101 01100001 01101110 00100001

                A new .NET Serializer All in one Menu-Ribbon Bar Taking over the world since 1371!

                raddevusR Offline
                raddevusR Offline
                raddevus
                wrote on last edited by
                #7

                I got : L Ei then I got bored and gave up. :rolleyes: Oh, I can't believe this is out there: SPOILER Binary to Ascii Text Converter[^] I pasted it in and converted.

                S 1 Reply Last reply
                0
                • raddevusR raddevus

                  I got : L Ei then I got bored and gave up. :rolleyes: Oh, I can't believe this is out there: SPOILER Binary to Ascii Text Converter[^] I pasted it in and converted.

                  S Offline
                  S Offline
                  Super Lloyd
                  wrote on last edited by
                  #8

                  Damn, I had to quickly write a program to do that, if only I had known! :rolleyes: :laugh:

                  A new .NET Serializer All in one Menu-Ribbon Bar Taking over the world since 1371!

                  P 1 Reply Last reply
                  0
                  • S Super Lloyd

                    Damn, I had to quickly write a program to do that, if only I had known! :rolleyes: :laugh:

                    A new .NET Serializer All in one Menu-Ribbon Bar Taking over the world since 1371!

                    P Offline
                    P Offline
                    Peter_in_2780
                    wrote on last edited by
                    #9

                    I must be old! I read that fairly quickly. Years spent eyeball reading punched paper tape. ASCII brain still works; I think I've lost baudot. Cheers, Peter

                    Software rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012

                    1 Reply Last reply
                    0
                    • S Super Lloyd

                      I am doing a lot of computational maths work at home at the moment and, just for giggle, I thought of writing the following method. Code behave as incorrectly as expected! :-D

                      static void Main(string[] args)
                      {
                      var e = 1e18;
                      Console.WriteLine(IsEven(e));
                      Console.WriteLine(IsEven(e + 1));
                      Console.WriteLine(IsEven(-e));
                      Console.WriteLine(IsEven(-e + 1));
                      }
                      bool IsEven(double d)
                      {
                      if (d < 0) return (d % 2) > -1;
                      return (d % 2) < 1;
                      }

                      A new .NET Serializer All in one Menu-Ribbon Bar Taking over the world since 1371!

                      realJSOPR Offline
                      realJSOPR Offline
                      realJSOP
                      wrote on last edited by
                      #10

                      Either use Math.Round, or convert the value to a decimal type before performing math, and then back to a double after performing math.

                      ".45 ACP - because shooting twice is just silly" - JSOP, 2010
                      -----
                      You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
                      -----
                      When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013

                      S 1 Reply Last reply
                      0
                      • realJSOPR realJSOP

                        Either use Math.Round, or convert the value to a decimal type before performing math, and then back to a double after performing math.

                        ".45 ACP - because shooting twice is just silly" - JSOP, 2010
                        -----
                        You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
                        -----
                        When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013

                        S Offline
                        S Offline
                        Super Lloyd
                        wrote on last edited by
                        #11

                        lol initially I was wondering what was your point.. but I realise now it's subtle sarcasm, got me, hahah! :laugh:

                        A new .NET Serializer All in one Menu-Ribbon Bar Taking over the world since 1371!

                        realJSOPR 1 Reply Last reply
                        0
                        • S Super Lloyd

                          lol initially I was wondering what was your point.. but I realise now it's subtle sarcasm, got me, hahah! :laugh:

                          A new .NET Serializer All in one Menu-Ribbon Bar Taking over the world since 1371!

                          realJSOPR Offline
                          realJSOPR Offline
                          realJSOP
                          wrote on last edited by
                          #12

                          :)

                          ".45 ACP - because shooting twice is just silly" - JSOP, 2010
                          -----
                          You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
                          -----
                          When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013

                          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