fmod giving "bad" output

i am writing a constructor for my fraction class that will convert a decimal value into a fraction.

in order to do this i am using fmod to count how many digits there are after the decimal. however, it never reaches 0 even though it should.

this is my test code to demonstrate the problem:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

//#include "fraction.h"
#include <cmath>
#include <iostream>
using namespace std;

int main()
{
    double Value = 1.25;
    double i = 1.0;
    for (int j =0 ; j<5 ; j++)
    {
        //cout<<Value<<"/"<<i<<"="<<Value/i<<endl;
        cout<<fmod(Value,i)<<endl;
        i = i *0.1;
    }

}//end main 


my output is:


0.25
0.05
0.01
0.001
0.0001



could someone explain why i am getting a result of 0.01 instead of 0?
or even o.05 which should be 0.5
Last edited on
Topic archived. No new replies allowed.