Ad
  • Custom User Avatar
  • Custom User Avatar

    Surely, a controversial solution due to its comments!
    but, I like it!

  • Default User Avatar

    "Ok, lesson is over. Let us do a task with while."
    Not clear?

  • Default User Avatar

    Hey, thank you for letting me know. My first comment on here and still getting used to etiquette.

  • Custom User Avatar

    Please use spoiler flag next time.

  • Default User Avatar

    This comment is hidden because it contains spoiler information about the solution

  • Custom User Avatar

    Pretty simple solution, I like it!

  • Custom User Avatar

    For what it's worth, if you managed to complete the question you're still doing something right! :) happy coding!

  • Custom User Avatar

    What requirements? "Zero (0) is not checked for any specific sign." You can read English right?

  • Default User Avatar

    @Unihedron
    You still do not get the point. You would ruin finance application if you were to allow negative zero. So stop your nonsense with copying stuff of internet on the theory of -0.
    My advice to you is to stick to requirements while designing solutions.

  • Custom User Avatar

    Ah yes, older comments on non issues being discussed by seemingly inexperienced people who self-proclaim to be potentially "missing something" making the exact same, invarying point reiterating on an (minor, if I may add, though that's my own opinion on the pile) opinion makes a bible, and directing people towards such a totem is no more than mere preaching. You sir, however, had made some variation to that opinion and added the very strong, inarguable "If your attitude is to ignore the requirements perhaps you should reconsider career" valuable advice which noone else did, so I will give you a serious response for once out of this entire thread to finally put the discussion (preaching) to rest.

    Chapter 1: What is negative zero?

    Negative zero, in the common (not-specific-to-javascript) context, is the signed version of 0 representing zero on either the extended real number line, so that you can do 1/±0 for either of ±Infinity; or as an allowed value that is functionally identical to any not-negative-zero enabled by the side effect of number encoding having a bit to denote its sign, as a negative number representation exists for every possible value, including 0.

    Negative zeros in IEEE754, which will from now on be the negative zero to be exclusively referred to throughout this text, describes a value that is only generated by the literal (i.e. as-programmed "-0") or as the result of a floating point arithmetic (i.e. division) evaluating to 0 (i.e. too small to be rounded to -1). The simplest way to put it: If there were no -0 or /, there will be no -0.

    Now, that said, -0 isn't a value that acts entirely functional to +0. For starters, there's ±Infinity, and the sign on 0 persists sometimes when you perform arithmetic on it:

    (+0)-(-0) is 0, and (-0)-(+0) is -0. You also have (-0)+(-0) giving -0 and (-0)-(-0) giving 0. However, all of these results share the same "numerical value", and its behaviour does not vary except for the very few defined triangulation functions. The only one I can remember right now is atan2: Math.atan2(0,0) and Math.atan2(0,-0) giving 0 and pi respectively.

    Chapter 2: Why (When) is negative zero used?

    The importance of negative zero is not self-justifying, otherwise it would have been used far more frequently and we wouldn't need to have any conversation over it, just like we don't argue over whether "1" and "1/9 * 9" (0.999...) is the same number. However, I've just given one such function, atan2, so I might as well research why such a difference happened.

    As it turns out, atan2 takes the point A in coordinates (x, y) in our typical grid system, draws a line between it to the origin O, then takes that angle. It's in radians so you don't get the familiar 0~360, but Math.atan2(5, 5) gave the same value as Math.atan2(50, 50) (and it should draw out to be 45deg), so that feels right.

    To be more specific, this seems to be a function where we are drawing a circle starting from somewhere above the origin, going right, down around then left and then back up around the origin, only to finish where we started, going from 0 ~ pi/2 ~ pi then jumping to -pi ~ -pi/2 ~0? The disconnected nature explains the result through intuition, that +0 is pointing to where the circle starts and where -0 points towards where the circle went halfway.

    However, in this context -0 already makes no sense as an integer that represents a value of where a point is on a grid, and the result here, in this new context, actually means the difference doesn't exist: Math.atan2(0,+0)==Math.atan2(0,+1) already holds and so does Math.atan2(0,-0)==Math.atan2(0,-1).

    Chapter 3: Should I care whether it's a negative zero here?

    No.

    In case you don't get it, here's a generalization: If you were using values on an analytical or statistical use case where it matters whether a zero is signed, for example what angle the points are relative to the origin (of which case you wouldn't have an integer to begin with) or when you needed the -Infinity, then you need the negative zeroes to remain negative. One can make the argument that applying the negative operations in this context means to reflect the point towards its reflection along the x-axis, and thus keeping a negative zero negative here would mean we may be doing the wrong thing; if and only if the function was called "flip bit" and not "make negative".

    We also have not been given instructions on how the result of our function will be fed to any zero-signedness-significant function. This means that as a programmer, if you read too much into the client telling you to "not change the value if it's already zero", you will be wasting a lot of debug time down the run if everyone assumes the function always spews out a negative number so when someone did a tan2 and suddenly gets 0 instead of the expected pi, it becomes the symptom of a function not doing what it's named.

    If all of that makes not much sense, then let's go back to the premise: If there were no -0 or /, there will be no -0. We are making numbers negative, so it is entirely sensible to get back zeroes in the negative.

    Now, this is not to say that you are not allowed to care about the signedness of the zero being returned; keen eyes who notice how this function transforms 0 => -0 and -0 => -0 shows that the speaker is paying attention to the disrepancy on how makeNegative(0); // return 0 is written and not -0, but it ignores that even if it wrote // return -0, every other solution that returned +0 instead are not invalid and are in fact doing the exact same thing. This is where the context matters: When working with continuous numerical values on the number line, +0 and -0 are equal and represent the same value, and (+0)-(-0), (-0)-(+0), (-0)+(-0) and (-0)-(-0) are all just the numerical value 0 if we don't care which side of the axis it lands on.

    And if I have someone on my team preaching about how some code that passes all the unit tests and is functionally correct are wrong because it doesn't satisfy his misguided perception based on some nebulous idealogy on the passing mention of how the value doesn't need to be changed, I would really want to fire them and they should know full well that they should reconsider their career, if they don't take their time to research the simple factoid on the significance of negative zeroes.

  • Default User Avatar

    @Unihedron
    Review older comments. -0 issue had been pointed out by others too.

  • Custom User Avatar

    The requirements are defined by the test cases, I'm not sure what you're arguing against here. Are you advocating for the tests to check for your imaginary -0 so it punishes every other sane programmer who is doing their job properly?

  • Default User Avatar

    @Unihedron
    It's not about what's possible but the requirements of the exercise itself. If your attitude is to ignore the requirements perhaps you should reconsider career. Programming is not the path for people who argue but solve problems in the way that satisfies the REQUIREMENTS.

  • Custom User Avatar

    Signed zero is zero with an associated sign. In ordinary arithmetic, the number 0 does not have a sign, so that −0, +0 and 0 are identical. However, in computing, some number representations allow for the existence of two zeros, often denoted by −0 (negative zero) and +0 (positive zero), regarded as equal by the numerical comparison operations but with possible different behaviors in particular operations. This occurs in the sign and magnitude and ones' complement signed number representations for integers, and in most floating-point number representations. The number 0 is usually encoded as +0, but can be represented by either +0 or −0.

    The IEEE 754 standard for floating-point arithmetic (presently used by most computers and programming languages that support floating-point numbers) requires both +0 and −0. Real arithmetic with signed zeros can be considered a variant of the extended real number line such that 1/−0 = −∞ and 1/+0 = +∞; division is only undefined for ±0/±0 and ±∞/±∞.

  • Loading more items...