1 year ago

#320499

test-img

atr

C++ Division of double

I'realy don't understand how divide doubles in c++ and have the correct precision.

I read many posts and undertood the logic (What Every Computer Scientist Should Know About Floating-Point Arithmetic) but I dont see how I can do. Could you help me :-)

#include <stdio.h>
#include <iostream>
#include <math.h>

int main(int argc, char const *argv[])
{
    //Convert 123 minutes in hh:mm

    double a(123); //Minutes in decimal format
    
    double resultat = a/60.0;

    //resultat = 2.0499999999999998 and not 2.05
    double hours;
    double decimalMinute;
    
    decimalMinute = modf(resultat, &hours);
    
    int minutes = decimalMinute*60.0;
    //Give 2 and not 3 !!!

    double minutesDouble = decimalMinute*60.0;
    //Give 2,999999 and not 3 !!

    return 0;
}

How can I have the "correct" value ?

c++

double

division

0 Answers

Your Answer

Accepted video resources