C weird approximation on floating point

111 views Asked by At

I have the following code:

#include<stdio.h>
int main(int argc, char const *argv[])
{
    float min, max, step;
    min = -0.85, max = 0.85, step = 0.002;
    int rank = 3, total = 4;
    float step1 = min + (max - min) * rank / total; // should be 0.425
    printf("%f %.7g\n", step1, step1); // 0.425000 0.4250001
    float step2 = min + (max - min) * (rank + 1) / total - step; //should be 0.848
    printf("%f %.7g\n", step2, step2); // 0.848000 0.848
    float noc = (step2 - step1 + step) / step; //should be 212,5
    printf("%f %.7g\n", noc, noc); // 212.499985 212.5
    int nol = 1200;
    int result = (int)nol * noc; //should be 255000
    printf("%d\n", result); // 254999
    return 0;
}

This is a fragment of code isolated from a project I have to do. The final result should be 255000, but for some causes, it shows 254999. Can someone please explain me what happens in the process? I have read somewhere that multiplying a floating number with 10^k and then dividing back solves such problems, but in this case, due to the variable step varying from 0.000001 to 0.1, I can't actually use that (in the same way, I can't use a defined EPSILON). What else can I do?

Thanks in advance!

P.S.: I have used double and long double as well, but with same problems, only this time error propagates from a further decimal. I am using gcc 4.8.2, under Ubuntu 14.04.1.

2

There are 2 answers

2
chux - Reinstate Monica On BEST ANSWER

Truncation vs. rounding.

Due to subtle rounding effect of FP arithmetic, the product nol * noc may be slightly less than an integer value. Conversion to int results in fractional truncation. Suggest rounding before conversion to int.

#include <math.h>

int result = (int) roundf(nol * noc);
2
user3629249 On
the significant problem(s) are:
1) mixing floating point and double with integer math
   --so the compiler promotes all the math to float (or double)
2) not all numbers can be expressed exactly in float
3) --the initialization of min, max, step are taking double literals 
   and converting them to float  
   --even double cannot express all values exactly
   --some precision is lost when performing the conversion from double to float 
4) this code excerpt:  (rank + 1) / total is always = 1
   --(although the many conversions may result in being 'not exactly' 1)
5) argc and argv are not referenced in your code.
   --this, given that all warnings are enabled, will rise two warnings
     at compile time about unused parameters
6) this line in your code is not correct syntax 
   --(although the compiler might not complain) #include<stdio.h>  
   --it should be #include <stdio.h>
   --sometimes spaces count, sometimes they dont