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. The Lounge
  3. Oh JavaScript, you silly goose.

Oh JavaScript, you silly goose.

Scheduled Pinned Locked Moved The Lounge
javascriptdatabasequestion
40 Posts 14 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 Lost User

    You didn't know about NaN yet? Or infinity? Or that JS uses elephanting floats all over the place? It gets better, 1/0 is positive infinity but 1/-0 is negative infinity, even though 0 and -0 compare equal to each other.

    Kornfeld Eliyahu PeterK Offline
    Kornfeld Eliyahu PeterK Offline
    Kornfeld Eliyahu Peter
    wrote on last edited by
    #20

    That positive 0 and negative 0, of course has nothing to do with JS... It comes from the IEEE 754 standard, and you can find it in C# implementation too... Exception handling is very expensive in terms of computer resources, so we already used to validate user input...In this case JS follows to the letter the standard... https://en.wikipedia.org/wiki/IEEE_floating_point[^]

    Skipper: We'll fix it. Alex: Fix it? How you gonna fix this? Skipper: Grit, spit and a whole lotta duct tape.

    "It never ceases to amaze me that a spacecraft launched in 1977 can be fixed remotely from Earth." ― Brian Cox

    L 1 Reply Last reply
    0
    • J Jeremy Falcon

      After years and years of using JavaScript, only now do I find that checking for zero on the denominator when dividing is a complete waste of time since JavaScript handles it gracefully without crashing.

      console.log(0 / 0); // returns NaN
      console.log(1 / 0); // returns Infinity

      // let's get fancy
      var divide = (...args) => args.reduce((a, b) => a / b);

      console.log(divide(0, 0)); // still returns NaN
      console.log(divide(1, 0)); // still returns Infinity

      // if I want to save the sucker in a database
      var result = divide(42, 0);
      saveMeSomehow.result = isNaN(result) || !isFinite(result) ? 0 : result;

      These young kids these days just don't know how spoiled they are. Note: I don't think the syntax highlighting supports ES6 yet, so yay I broke the Internet.

      Jeremy Falcon

      K Offline
      K Offline
      KarstenK
      wrote on last edited by
      #21

      Maybe you only need some parentheses like that:

      aveMeSomehow.result = ( isNaN(result) || !isFinite(result) ) ? 0 : result;

      to get things running. :rolleyes:

      Press F1 for help or google it. Greetings from Germany

      1 Reply Last reply
      0
      • Kornfeld Eliyahu PeterK Kornfeld Eliyahu Peter

        That positive 0 and negative 0, of course has nothing to do with JS... It comes from the IEEE 754 standard, and you can find it in C# implementation too... Exception handling is very expensive in terms of computer resources, so we already used to validate user input...In this case JS follows to the letter the standard... https://en.wikipedia.org/wiki/IEEE_floating_point[^]

        Skipper: We'll fix it. Alex: Fix it? How you gonna fix this? Skipper: Grit, spit and a whole lotta duct tape.

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

        I wrote:

        Or that JS uses elephanting floats all over the place

        1 Reply Last reply
        0
        • J Jeremy Falcon

          After years and years of using JavaScript, only now do I find that checking for zero on the denominator when dividing is a complete waste of time since JavaScript handles it gracefully without crashing.

          console.log(0 / 0); // returns NaN
          console.log(1 / 0); // returns Infinity

          // let's get fancy
          var divide = (...args) => args.reduce((a, b) => a / b);

          console.log(divide(0, 0)); // still returns NaN
          console.log(divide(1, 0)); // still returns Infinity

          // if I want to save the sucker in a database
          var result = divide(42, 0);
          saveMeSomehow.result = isNaN(result) || !isFinite(result) ? 0 : result;

          These young kids these days just don't know how spoiled they are. Note: I don't think the syntax highlighting supports ES6 yet, so yay I broke the Internet.

          Jeremy Falcon

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

          I'm glad my college math professor doesn't program in JavaScript, he'd have a fit seeing a variable equal to infinity. Nothing is ever equal to infinity because it isn't a single number. The result of an equation tends to infinity when a certain condition becomes true (I suck at math so I don't think any explanation would make much sense) In the divide by zero example, the limit of x/n as n tends to zero is infinity.

          1 Reply Last reply
          0
          • J Jeremy Falcon

            After years and years of using JavaScript, only now do I find that checking for zero on the denominator when dividing is a complete waste of time since JavaScript handles it gracefully without crashing.

            console.log(0 / 0); // returns NaN
            console.log(1 / 0); // returns Infinity

            // let's get fancy
            var divide = (...args) => args.reduce((a, b) => a / b);

            console.log(divide(0, 0)); // still returns NaN
            console.log(divide(1, 0)); // still returns Infinity

            // if I want to save the sucker in a database
            var result = divide(42, 0);
            saveMeSomehow.result = isNaN(result) || !isFinite(result) ? 0 : result;

            These young kids these days just don't know how spoiled they are. Note: I don't think the syntax highlighting supports ES6 yet, so yay I broke the Internet.

            Jeremy Falcon

            N Offline
            N Offline
            Nagy Vilmos
            wrote on last edited by
            #24

            :beer:

            veni bibi saltavi

            J 1 Reply Last reply
            0
            • N Nagy Vilmos

              :beer:

              veni bibi saltavi

              J Offline
              J Offline
              Jeremy Falcon
              wrote on last edited by
              #25

              Thanks man! Cheers.

              Jeremy Falcon

              1 Reply Last reply
              0
              • L Lost User

                Ok, but I must admit that I still do not like it. This way you always have to test the results which bloats up the code. First, it's an avoidable error and, assuming it comes to my attention, I will find out why the denominater can become zero and then eliminate the problem once and for all. So what's won by bravely keeping going until the error manifests itself somplace else? You will just have to spend more time tracing it back to its origin and then essentially doing the same work to correct it.

                The language is JavaScript. that of Mordor, which I will not utter here
                This is Javascript. If you put big wheels and a racing stripe on a golf cart, it's still a fucking golf cart.
                "I don't know, extraterrestrial?" "You mean like from space?" "No, from Canada." If software development were a circus, we would all be the clowns.

                B Offline
                B Offline
                Benito Aramando
                wrote on last edited by
                #26

                But if it didn't do this you would just always have to test the inputs instead to ensure the application doesn't throw an error, which isn't necessarily any better. Besides, it's possible you may not have to test the results anyway, if you aren't relying on them being a number, e.g. in the below code it doesn't matter if b is 0:

                if(a / b === 0.25){ ... }

                L 1 Reply Last reply
                0
                • P PIEBALDconsult

                  No; keeping the application from crashing is the application developer's job, not the language or library developer's job.

                  B Offline
                  B Offline
                  Benito Aramando
                  wrote on last edited by
                  #27

                  PIEBALDconsult wrote:

                  keeping the application from crashing is the application developer's job

                  The language shouldn't crash the application unnecessarily, though. The result of a division operation isn't necessarily useless or invalid just because the divisor was 0. If you want to test that x/y === 10 then when y equals 0 you probably just want a false result, not an unhandled exception. This behaviour means that you only have to put in special-case handling for y === 0 if you need it. At the same time, if your use case does require it but you forget to put it in, then you'll probably get the error thrown anyway when you handle the result of the division, rather than the bug being masked.

                  J D 2 Replies Last reply
                  0
                  • B Benito Aramando

                    But if it didn't do this you would just always have to test the inputs instead to ensure the application doesn't throw an error, which isn't necessarily any better. Besides, it's possible you may not have to test the results anyway, if you aren't relying on them being a number, e.g. in the below code it doesn't matter if b is 0:

                    if(a / b === 0.25){ ... }

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

                    Benito Aramando wrote:

                    But if it didn't do this you would just always have to test the inputs instead to ensure the application doesn't throw an error, which isn't necessarily any better.

                    Let's assume a very common case in C#: A reference is null and an exeption will be thrown if the program tries to use the reference. If this can be corrected, then testing beforehand and performing this correction is the way to go. If not, then what could I possibly accomplish by testing? I simply let the exeption fly, let it end up in some Pokemon exception handler and will get to read about it in the log. After that I will check the conditions that led to the error and will never have to worry about it ever again. Avoidable errors should indeed be avoided, not handled (gracefully or not) or tested. They are like sand in your gears. I want them to come to my attention and getting rid of them once and for all is worth the time and the work.

                    The language is JavaScript. that of Mordor, which I will not utter here
                    This is Javascript. If you put big wheels and a racing stripe on a golf cart, it's still a fucking golf cart.
                    "I don't know, extraterrestrial?" "You mean like from space?" "No, from Canada." If software development were a circus, we would all be the clowns.

                    B 1 Reply Last reply
                    0
                    • L Lost User

                      Benito Aramando wrote:

                      But if it didn't do this you would just always have to test the inputs instead to ensure the application doesn't throw an error, which isn't necessarily any better.

                      Let's assume a very common case in C#: A reference is null and an exeption will be thrown if the program tries to use the reference. If this can be corrected, then testing beforehand and performing this correction is the way to go. If not, then what could I possibly accomplish by testing? I simply let the exeption fly, let it end up in some Pokemon exception handler and will get to read about it in the log. After that I will check the conditions that led to the error and will never have to worry about it ever again. Avoidable errors should indeed be avoided, not handled (gracefully or not) or tested. They are like sand in your gears. I want them to come to my attention and getting rid of them once and for all is worth the time and the work.

                      The language is JavaScript. that of Mordor, which I will not utter here
                      This is Javascript. If you put big wheels and a racing stripe on a golf cart, it's still a fucking golf cart.
                      "I don't know, extraterrestrial?" "You mean like from space?" "No, from Canada." If software development were a circus, we would all be the clowns.

                      B Offline
                      B Offline
                      Benito Aramando
                      wrote on last edited by
                      #29

                      I should have put my statements the other way around. Your response to the one you quoted is fair enough, but your point is predicated on the assumption that the result of a division by 0 must always represent an error that should have been avoided before that point. This isn't necessarily the case.

                      L 1 Reply Last reply
                      0
                      • B Benito Aramando

                        I should have put my statements the other way around. Your response to the one you quoted is fair enough, but your point is predicated on the assumption that the result of a division by 0 must always represent an error that should have been avoided before that point. This isn't necessarily the case.

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

                        Most of the time it is, so if the exception comes and it is not, you know what to do to meet this precondition. The other way around some real errors may go unnoticed for a long time, plus the extra effort for tracing it back to its origin.

                        The language is JavaScript. that of Mordor, which I will not utter here
                        This is Javascript. If you put big wheels and a racing stripe on a golf cart, it's still a fucking golf cart.
                        "I don't know, extraterrestrial?" "You mean like from space?" "No, from Canada." If software development were a circus, we would all be the clowns.

                        J B 2 Replies Last reply
                        0
                        • J Jeremy Falcon

                          After years and years of using JavaScript, only now do I find that checking for zero on the denominator when dividing is a complete waste of time since JavaScript handles it gracefully without crashing.

                          console.log(0 / 0); // returns NaN
                          console.log(1 / 0); // returns Infinity

                          // let's get fancy
                          var divide = (...args) => args.reduce((a, b) => a / b);

                          console.log(divide(0, 0)); // still returns NaN
                          console.log(divide(1, 0)); // still returns Infinity

                          // if I want to save the sucker in a database
                          var result = divide(42, 0);
                          saveMeSomehow.result = isNaN(result) || !isFinite(result) ? 0 : result;

                          These young kids these days just don't know how spoiled they are. Note: I don't think the syntax highlighting supports ES6 yet, so yay I broke the Internet.

                          Jeremy Falcon

                          Richard DeemingR Offline
                          Richard DeemingR Offline
                          Richard Deeming
                          wrote on last edited by
                          #31

                          .NET does exactly the same thing with floating-point arithmetic. AFAIK, so does Java. The behaviour is required by IEEE-754[^]:

                          IEEE-754 : Frequently Asked Questions : Why doesn't division by zero (or overflow, or underflow) stop the program or trigger an error?[^]

                          The 754 model encourages robust programs. It is intended not only for numerical analysts but also for spreadsheet users, database systems, or even coffee pots. The propagation rules for NaNs and infinities allow inconsequential exceptions to vanish. Similarly, gradual underflow maintains error properties over a precision's range.


                          "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                          "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

                          J 1 Reply Last reply
                          0
                          • Richard DeemingR Richard Deeming

                            .NET does exactly the same thing with floating-point arithmetic. AFAIK, so does Java. The behaviour is required by IEEE-754[^]:

                            IEEE-754 : Frequently Asked Questions : Why doesn't division by zero (or overflow, or underflow) stop the program or trigger an error?[^]

                            The 754 model encourages robust programs. It is intended not only for numerical analysts but also for spreadsheet users, database systems, or even coffee pots. The propagation rules for NaNs and infinities allow inconsequential exceptions to vanish. Similarly, gradual underflow maintains error properties over a precision's range.


                            "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                            J Offline
                            J Offline
                            Jeremy Falcon
                            wrote on last edited by
                            #32

                            I never knew that. Thanks.

                            Jeremy Falcon

                            1 Reply Last reply
                            0
                            • B Benito Aramando

                              PIEBALDconsult wrote:

                              keeping the application from crashing is the application developer's job

                              The language shouldn't crash the application unnecessarily, though. The result of a division operation isn't necessarily useless or invalid just because the divisor was 0. If you want to test that x/y === 10 then when y equals 0 you probably just want a false result, not an unhandled exception. This behaviour means that you only have to put in special-case handling for y === 0 if you need it. At the same time, if your use case does require it but you forget to put it in, then you'll probably get the error thrown anyway when you handle the result of the division, rather than the bug being masked.

                              J Offline
                              J Offline
                              Jeremy Falcon
                              wrote on last edited by
                              #33

                              Exactly. Having a system crash over an operation like that is not the right solution. In a second and third generation language I can see having a fault since everything has a fault so it's just how it's done, but most certainly not in a fourth generation language.

                              Jeremy Falcon

                              1 Reply Last reply
                              0
                              • L Lost User

                                Most of the time it is, so if the exception comes and it is not, you know what to do to meet this precondition. The other way around some real errors may go unnoticed for a long time, plus the extra effort for tracing it back to its origin.

                                The language is JavaScript. that of Mordor, which I will not utter here
                                This is Javascript. If you put big wheels and a racing stripe on a golf cart, it's still a fucking golf cart.
                                "I don't know, extraterrestrial?" "You mean like from space?" "No, from Canada." If software development were a circus, we would all be the clowns.

                                J Offline
                                J Offline
                                Jeremy Falcon
                                wrote on last edited by
                                #34

                                JavaScript still throws errors for a lot of things, but it and many other fourth generation languages (so I've been told) have decided to do something about dividing by zero being worthy of an "error". I happen to agree it's not an error, since infinity is a valid concept.

                                Jeremy Falcon

                                1 Reply Last reply
                                0
                                • B Benito Aramando

                                  PIEBALDconsult wrote:

                                  keeping the application from crashing is the application developer's job

                                  The language shouldn't crash the application unnecessarily, though. The result of a division operation isn't necessarily useless or invalid just because the divisor was 0. If you want to test that x/y === 10 then when y equals 0 you probably just want a false result, not an unhandled exception. This behaviour means that you only have to put in special-case handling for y === 0 if you need it. At the same time, if your use case does require it but you forget to put it in, then you'll probably get the error thrown anyway when you handle the result of the division, rather than the bug being masked.

                                  D Offline
                                  D Offline
                                  DarkChuky CR
                                  wrote on last edited by
                                  #35

                                  I prefer the application to crash, because if the application doesn't crash in a /0, the programmer needs to know what to do in the case we get a division with 0 without exception, call it division by zero handling.... this then is for Advanced programmers, I can see a lot of code running and doing stuff and crashing some lines after the /0 where the programmer is in a different context, and will have hard time to understand the the issue is a /0 that was not handled 10 lines before... Then yes I prefer it to blow up in front of my face, a /0 exception is always easier to track. Think on null validations, you had learned to do Null validations because a null crash so ugly that you want to avoid it... I had seen code where a null was not validated (the -> expressions make it so easy to crash due bad null validations) and the code crash 100 lines after that missed validation. So, I'm not telling is bad or good, just I think it's for advanced programmers, the ones that already learned the price of a division by zero

                                  B 1 Reply Last reply
                                  0
                                  • L Lost User

                                    Most of the time it is, so if the exception comes and it is not, you know what to do to meet this precondition. The other way around some real errors may go unnoticed for a long time, plus the extra effort for tracing it back to its origin.

                                    The language is JavaScript. that of Mordor, which I will not utter here
                                    This is Javascript. If you put big wheels and a racing stripe on a golf cart, it's still a fucking golf cart.
                                    "I don't know, extraterrestrial?" "You mean like from space?" "No, from Canada." If software development were a circus, we would all be the clowns.

                                    B Offline
                                    B Offline
                                    Benito Aramando
                                    wrote on last edited by
                                    #36

                                    The thing is that that exception may not appear until your application is live. I don't think Javascript should throw application-crashing runtime errors in order to help you identify conditions in your code that may not even be bugs.

                                    1 Reply Last reply
                                    0
                                    • J Jeremy Falcon

                                      After years and years of using JavaScript, only now do I find that checking for zero on the denominator when dividing is a complete waste of time since JavaScript handles it gracefully without crashing.

                                      console.log(0 / 0); // returns NaN
                                      console.log(1 / 0); // returns Infinity

                                      // let's get fancy
                                      var divide = (...args) => args.reduce((a, b) => a / b);

                                      console.log(divide(0, 0)); // still returns NaN
                                      console.log(divide(1, 0)); // still returns Infinity

                                      // if I want to save the sucker in a database
                                      var result = divide(42, 0);
                                      saveMeSomehow.result = isNaN(result) || !isFinite(result) ? 0 : result;

                                      These young kids these days just don't know how spoiled they are. Note: I don't think the syntax highlighting supports ES6 yet, so yay I broke the Internet.

                                      Jeremy Falcon

                                      C Offline
                                      C Offline
                                      Cloud William
                                      wrote on last edited by
                                      #37

                                      Here's the thing. JavaScript's way of handling divide-by-zero is not better or worse than any other. It simply is. If you have code that should do something specific when a divide-by-zero happens, then you damn well better code it that way. If you are depending on the the language for that specific behavior, then for Pete's sake emulate that behavior when you move to a language that doesn't have the behavior. JavaScript does not matter. C++ does not matter. Algol does not matter. Only the behavior you want matters. So, know the language, and code accordingly.

                                      1 Reply Last reply
                                      0
                                      • J Jeremy Falcon

                                        After years and years of using JavaScript, only now do I find that checking for zero on the denominator when dividing is a complete waste of time since JavaScript handles it gracefully without crashing.

                                        console.log(0 / 0); // returns NaN
                                        console.log(1 / 0); // returns Infinity

                                        // let's get fancy
                                        var divide = (...args) => args.reduce((a, b) => a / b);

                                        console.log(divide(0, 0)); // still returns NaN
                                        console.log(divide(1, 0)); // still returns Infinity

                                        // if I want to save the sucker in a database
                                        var result = divide(42, 0);
                                        saveMeSomehow.result = isNaN(result) || !isFinite(result) ? 0 : result;

                                        These young kids these days just don't know how spoiled they are. Note: I don't think the syntax highlighting supports ES6 yet, so yay I broke the Internet.

                                        Jeremy Falcon

                                        B Offline
                                        B Offline
                                        BrainiacV
                                        wrote on last edited by
                                        #38

                                        The BASIC that came with the Heathkit computers was way ahead of the game. It's error message said, "I'm a computer, not God" when you would divide by zero.

                                        Psychosis at 10 Film at 11 Those who do not remember the past, are doomed to repeat it. Those who do not remember the past, cannot build upon it.

                                        1 Reply Last reply
                                        0
                                        • J Jeremy Falcon

                                          After years and years of using JavaScript, only now do I find that checking for zero on the denominator when dividing is a complete waste of time since JavaScript handles it gracefully without crashing.

                                          console.log(0 / 0); // returns NaN
                                          console.log(1 / 0); // returns Infinity

                                          // let's get fancy
                                          var divide = (...args) => args.reduce((a, b) => a / b);

                                          console.log(divide(0, 0)); // still returns NaN
                                          console.log(divide(1, 0)); // still returns Infinity

                                          // if I want to save the sucker in a database
                                          var result = divide(42, 0);
                                          saveMeSomehow.result = isNaN(result) || !isFinite(result) ? 0 : result;

                                          These young kids these days just don't know how spoiled they are. Note: I don't think the syntax highlighting supports ES6 yet, so yay I broke the Internet.

                                          Jeremy Falcon

                                          O Offline
                                          O Offline
                                          obermd
                                          wrote on last edited by
                                          #39

                                          Mathematically, these are the correct answers. 0/n = 0; Since 0 =/= Infinity, 0/0 is Not a Number (NaN) Lim(1/n)(as n=>0) => infinity; specifically aleph(null). There are multiple infinities and this is just the first of them. So, regardless of what you think about JavaScript, it handles division by zero properly.

                                          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