Floating point woes

topic posted Mon, September 4, 2006 - 9:57 AM by  Unsubscribed
Share/Save/Bookmark
Advertisement
I'm writing a few short programs based on calculating factors in electronics.

As such, I often have to multiply and divide some small numbers, and the round-off is of such a magnitude that it renders the calculations useless.

For instance, with this fragment of code:

#include <iostream>
using namespace std;
int main () {

double test;
test = ( 1 / 10000) + ( 1 / 200000);
cout >> test;
return 0;
}

The output will give me zero.
posted by:
Unsubscribed
Advertisement
Advertisement
  • Re: Floating point woes

    Mon, September 4, 2006 - 10:39 AM
    1, 10000, and 200000 are all integers, so the math is done using integers. Then the result (0) is converted to a double when assigned to 'test'

    It needs to look like this:
    test = (1.0 / 10000.0) + (1.0 / 200000.0);

    If you are using variables, make sure they are cast to doubles, for example:
    int i = 10000;

    test = 1.0 / (double)i;