Help in C++!

It might be very basic for many but I don’t know what’s happening here. Any help from the community of genius is much appreciated :slightly_smiling_face: !
C++ Code :

    double a = (log(243))/(log(3));
    float b = ((log(243))/(log(3)));
    cout<<a<<"\n";
    cout<<b<<"\n";
    cout<<floor(a)<<"\n";
    cout<<floor(b)<<"\n";

Output :
5
5
4
5

Why the float is given as integer there in the output.

It got floored in division.

Just the standard lack of floating-point precision :slight_smile:

Try the following small alteration:

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

using namespace std;

int main()
{
    double a = (log(243))/(log(3));
    float b = ((log(243))/(log(3)));
    cout << std::setprecision(std::numeric_limits<long double>::digits10 + 1) << a <<"\n";
    cout << std::setprecision(std::numeric_limits<long double>::digits10 + 1) << b << "\n";
    cout << floor(a) << "\n";
    cout << floor(b) << "\n";
}
[simon@simon-laptop][19:56:41]
[~/devel/hackerrank/otherpeoples]>./a.out 
4.999999999999999112
5
4
5
5 Likes

Thanks

Wooooh… :joy: