Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get decimal part of a number [duplicate]

Tags:

c++

Trying to get whole and decimal part of a number into two variables. What I tried:

#include <iostream>
int main(){
    float n,m; int k; std::cin >> n;
    k = n;
    m = n - k;

Tried to convert the float number to int and received warning by the compiler that the number could be incorrect, tested, was incorrect indeed and couldn't get the expected results. Searched on this, couldn't find any other workaround than using floor().

My actual code:

int main() {
    float n; cin >> n;
    int nfloor = n;
    cout << nfloor << "\n";
    float nfloat = n - nfloor; int nfloatfloor;
    cout << nfloat << "\n";
    do {
        nfloat *= 10;
        nfloatfloor = nfloat;
        cout << nfloat << "\n" << nfloatfloor << "\n";
    } while (nfloat > nfloatfloor);
}

Results:

Input: 12.34
Output :
12
0.34
3.4
3
34
34
340
340
3400
3400
34000
34000
340000
340000
3.4e+06
3400001
3.4e+07
34000016

Subtracting two float numbers returns an incorrect value, Searched on this but the answers were on a high level that I couldn't understand.

My actual code:

int main() {
    float n; cin >> n;
    float nfloor = floor(n);
    cout << nfloor << "\n";
    float nfloat = n - nfloor; float nfloatfloor;
    cout << nfloat << "\n";
    do {
        nfloat *= 10;
        nfloatfloor = floor(nfloat);
        cout << nfloat << "\n" << nfloatfloor << "\n";
    } while (nfloat > nfloatfloor);
}

Results:

Input: 12.34
Output:
12
0.34
3.4
3
34
34 //Run should stop here because of the while loop bit it doesn't, funny thing is it gives me different results sometimes, last time it gave me 35 and 34
340
340
3400
3400
34000
34000
340000
340000
3.4e+06
3.4e+06
3.4e+07
3.4e+07

@Slava Take a look at the output right above this sentence, compiler printed 34 and 34, the duplicate answer shows that the couts are 34.0000000000000004 or something like that, and as I commented above, the code should have stopped, what I'm really trying to do is compare a float number and int number, if (float >int) the code should continue and if not it should stop so is there any solution? @hnefatl I tried your answer and the compiler just hangs:

int main() {
    float n2, whole, fractional, fractional2, whole2; cin >> n2;
    int denominator = 1;
    fractional = modf(n2, &whole);
    do {
        fractional *= 10;
        fractional2 = modf(fractional, &whole2);
        denominator *= 10;
    } while (fractional > fractional2);
    if (denominator > 1)
        denominator /= 10;
    cout << denominator;
}
like image 579
Shayan Avatar asked Dec 15 '17 18:12

Shayan


2 Answers

Why not use std::modf, which is designed for this purpose:

float n = 12.34;
float whole, fractional;

fractional = std::modf(n, &whole);

The non-fractional part of the value is in whole while the fractional part is in fractional.


If you then want to get an integer value for the whole part (bearing in mind that you can lose data this way as the range of a float can be larger than that of an int), you can just do:

int integralWhole = static_cast<int>(whole);
like image 138
hnefatl Avatar answered Sep 21 '22 12:09

hnefatl


Well this should do the trick.

int main(){
    float n; std::cin >> n;

    float whole = floor(n);
    float decimal = n - whole;
    std::cout << whole << "\n";
    std::cout << decimal << "\n";

    std::cin.get();
    return 0;
}
like image 22
CodeCollector Avatar answered Sep 19 '22 12:09

CodeCollector