float vs. doubles

I wrote this code for a c++ class. I need to have the variables set to float and get a calculation, but it does not output anything. When I use doubles, though, it does.

while (d_3 > 1.0) {
--d_3;
a_3 += (1/d_3);
}
cout << "this is the float value of 1/100000000 + 1/99999999 + 1/99999998... + 1/3 + 1/2 + 1/1: " << a_3 << endl;

Is there anything I can do to get a floating point calculation with this loop?

Comments


  • I do hope thats not your cout statement, but just an example.

    Anywhoo, the loop you have posted is still working with doubles. [b]0.1 is a double, 0.1f is a float[/b] (Notice the "f"). From the code that has been posted, that is the only error I see at the moment. For most of the cases, this does not really matter though. ...With very large numbers, however, it can as the numbers may not be representable in the size of floats.

    For floats, this is the corrected code:
    [code]
    while (d_3 > 1.0f) {
    --d_3;
    a_3 += (1/d_3);
    }
    [/code]

    [hr][size=1][leftbr].:EvolutionEngine[rightbr][leftbr].:MicroOS Operating System[rightbr][leftbr][link=http://www.brokenthorn.com]Website :: OS Development Series[rightbr][/link][/size]
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion