Q:

A Floating point expression whose value is 1 shows 0 when converted to int. How to get rid of this isuue in C++?

I need to change an expression's type from floating point to integer so as to add it to another integer variable.
The actual value of the expression is 1.
But the expression value ((d*s)/sqrt(1+pow(s, 2))) shows 0 when changed from floating point to int.
It is giving the wrong result due to this. Why does it show 0 and how do I avoid this?
#include <iostream>
#include <math.h>
using namespace std;

int main(){
    float d = sqrt(2);
    float s = sqrt(1);
    cout<<((d*s)/sqrt(1+pow(s, 2)))<<"\n";  // **prints 1**
    cout<<(int) ((d*s)/sqrt(1+pow(s, 2)));  // **prints 0**
}

A:

The problem is the float representation. If you print the following:
#include <iostream>
#include <math.h>
using namespace std;

int main(){
    float d = sqrt(2);
    float s = sqrt(1);
    cout.precision(15);
    cout<<((d*s)/sqrt(1+pow(s, 2)))<<"\n";  // **prints 1**
    cout<<(int) ((d*s)/sqrt(1+pow(s, 2)));  // **prints 0**
}

You will see that the first line prints 0.999999982885729. When casting to int, the number is truncated, so 0. You will have to round first. For that, change the last line:
cout<<(int) round(((d*s)/sqrt(1+pow(s, 2))));
And the int value will be 1.

