Ad
  • Custom User Avatar

    It's funny because this was actually pretty much my original answer, except that I didn't account for numbers less than zero.
    The error message wasn't very helpful, so I found myself jumping through hoops trying to figure out how to handle floating point inaccuracies.

  • Custom User Avatar

    Seemed like a simple task, but I broke my head solving it, 2 out of 3 conditions in the task were met and the last one was not accepted by me. Of course, I had to resort to help....... It turned out to be the same thing I wrote, just with some minor changes, which also turned out to be tricky, but now I see that it's even easier. I'm going to be a wreck today, work in 4 hours and I haven't slept.....

  • Default User Avatar
  • Default User Avatar

    I have checked the following source: https://cplusplus.com/reference/cmath/sqrt/?kw=sqrt, and it did not say that std :: sqrt returns NaN when the argument is negative.
    Could you please point out the source of your information? I will check that source if it is more reliable/updated.

  • Custom User Avatar
  • Custom User Avatar

    Why is this answer getting so many best practice votes? Surely since sqrt returns a floating point value, with which arithmatic is limited in precision, which could cause false answers. It would also risk undefined behaviour due to integer overflow with larger n values as squaresquare (square_rootsquare_root) reaches the max value for an int.
    Calculating square roots is also much slower than other integer operations so its not really an efficient approach. Am i mistaken here or missing something?

  • Custom User Avatar

    No need to check for negatives sqrt returns -NAN for negatives square it and it gives you NAN.

  • Custom User Avatar

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

  • Default User Avatar

    I just got it now!

    If we store the result of std::sqrt(n) in an int it will disregard the potential decimal numbers (which means the input parameter is not a perfect square to begin with).

    However, storing the result of std::sqrt(n) in an int works for this problem because if you multiply the values stored in the int variable by itself it will NOT equal to the input parameter because the decimal numbers have been taken off. Hence why the function will return false if the input parameter is NOT a perfect square.

  • Default User Avatar

    I don't quite understand why this is accepted? If the square root of n results in a decimal number, then n is not considered a perfect square correct? (and so the function should return false)
    But if we simply store the result of std::sqrt(n) in an int type variable then that disregards the potential decimal digits returned by std::sqrt(n).

  • Default User Avatar

    Very intelligent!

  • Custom User Avatar

    Hey, did you find out what the weird bit of code does? Anyone?

  • Default User Avatar

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

  • Custom User Avatar

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

  • Default User Avatar
  • Loading more items...