-2

so I have an assignment and I'm a newbie to programming.

My prof wants to have an output, that when I enter a number, the program will get the square root of that number, then if the answer is in whole number, it will + 1, and if its in a decimal number, it will -1. But the problem is, the if in whole number is working to add 1, but in decimal, its still adding 1. (Sorry for bad english)

For example, I entered 25, the square root of 25 is 5, so in condition, I should add 1 to answer so its 6

If i entered 24, the square root of it is 4.898979486, so it should be 3.898979486, because of its a decimal, the program should -1.

#include <iostream>
#include <cmath>
using namespace std;

int main()
{
    double num, result;
    cout << "Enter a number: \n";
    cin >> num;
    result = sqrt(num);
    if (num == 0) {
        cout << "\nThe number is in decimal number: " << --result;
    }
    else {
        cout << "\nThe number is in whole number: " << ++result;
    }
    return 0;
}
  • 10
    How does `if (num==0)` check is `num` is a whole number? – NathanOliver Feb 02 '21 at 14:43
  • 2
    Also, it sounds like you need a different professor, who actually knows not just C++, but computer science, and understands why floating point math is broken. – Sam Varshavchik Feb 02 '21 at 14:43
  • 2
    Guess this might help: https://stackoverflow.com/questions/1521607/check-double-variable-if-it-contains-an-integer-and-not-floating-point/1521682 – D-RAJ Feb 02 '21 at 14:44
  • I don't know either, they said I should use If (num%==0) but % is not working on float or double, because its for int only. – Dannielle Marvic Banaban Feb 02 '21 at 14:44
  • @DannielleMarvicBanaban Is it conceivable your prof wants you to implement your own sqrt function for integer numbers? – bitmask Feb 02 '21 at 14:45
  • @DannielleMarvicBanaban In that case, you don't want to be mucking about with floats. Write your integer sqrt function and then check if the result squared is the original value. – bitmask Feb 02 '21 at 14:52
  • 1
    Hello and welcome. Aside from the above solution suggestions, could you please make use of indentation to make your code a bit more readable. Thanks a lot and good luck. – AntiqTech Feb 02 '21 at 14:57
  • 1
    Don't compare floating point numbers for equality! Floating point variables might not be able to be exactly what you expect it to be depending on architecture. – Detonar Feb 02 '21 at 14:58
  • @bitmask, yeah, I have that in my code, my only problem is whenever I got whole number or decimal number in results, it gets plus 1, but I need to minus the result if its in decimal number – Dannielle Marvic Banaban Feb 02 '21 at 14:58
  • That statement is self-contradicting. – bitmask Feb 02 '21 at 14:59
  • [How to debug small programs.](//ericlippert.com/2014/03/05/how-to-debug-small-programs/) | [What is a debugger and how can it help me diagnose problems?](//stackoverflow.com/q/25385173/843953) Step through your code and narrow down the problem into a [mre]. Then ask a specific question. _"Why is my code doing this"_ is [too broad for Stack Overflow.](//meta.stackoverflow.com/a/253788/843953) Please also take the [tour], read [ask] and [what's on-topic](/help/on-topic). Welcome to Stack Overflow! – Pranav Hosangadi Feb 02 '21 at 15:00
  • You probably just need to use the code from the link that @D-RAJ gave to test if the floating point number is close enough to be a whole number. – drescherjm Feb 02 '21 at 15:02
  • `if (num == 0) {` will only be true if the person entered 0 as the number so the current code will ++result for all numbers the user enters other than 0. – drescherjm Feb 02 '21 at 15:21
  • 1
    Your code is not testing the result. – donjuedo Feb 02 '21 at 16:31
  • BTW, the `sqrt()` function does not modify it's parameter. – Thomas Matthews Feb 02 '21 at 18:05

2 Answers2

1

if (num == 0) is clearly not the correct way to determine whether num is a whole number or not.

they said I should use if (num%==0) but % is not working on float or double, because its for int only.

You could use std::fmod, which "works" for such types, but as mentioned in the comments, floating-point math is kind of broken, or better, it has a limited amount of precision. So that, instead of checking if two values are equal, it's better to check if they are almost equal.

There are a bunch of other library functions you can play with, to solve this task, like std::modf, std::nearbyint, std::round and so on. See e.g. Check double variable if it contains an integer, and not floating point

The following snippet is intended to test a possible implementation, for exposition only.

#include <iostream>
#include <iomanip>
#include <cmath>

bool is_almost_whole(double x, double eps)
{
    return std::min( x - std::floor(x), std::ceil(x) - x ) < eps;
}

int main()
{
    std::cout << std::boolalpha;
    for (double i{-1.0}; i < 2.1; i += 0.1)
    {
        std::cout << std::setprecision(35) << std::setw(45)
            << std::scientific << std::right << i 
            << "  " << std::setw(6) << std::left 
            << is_almost_whole(i, 0.001) << '\n';
    }
}

Testable here.

Bob__
  • 12,361
  • 3
  • 28
  • 42
0

The way you are trying to check decimal number is incorrect. You might try something like this-

if(num - (floor(result) * floor(result)) !=0 ){
    // result is a decimal number
} 

But, sometime this approach will give the wrong answer. Because here we are working with floating numbers and precision error is always a fact for floating numbers. Rather you can write a function- isSquareNumber(int n), which will decide if the result is a decimal or not.

#include <iostream>
#include <cmath>
using namespace std;

bool isSquareNumber(int n){
    int l=1, h=n;
    while(l<=h){
        int m = (l+h) / 2;
        if(m*m == n){
            return true;
        }else if(m*m > n){
            h = m-1;
        }else{
            l = m+1;
        }
    }
    return false;
}
 
int main(){
    double num, result;
    cout<<"Enter a number: \n";
    cin>>num;
    result=sqrt(num);
    if ( isSquareNumber(num) == false ){
        cout<<"\nThe number is in decimal number: "<<--result;
    }else{
        cout<<"\nThe number is in whole number: "<<++result;
    }
return 0;
}
Mahedi Kamal
  • 179
  • 1
  • 9