Juan Andres
06/30/2023, 12:57 PMStefan Schippers
06/30/2023, 1:08 PMstop_l=1.5001
to get rid of numerical errros while incrementing l_act
Juan Andres
06/30/2023, 1:15 PMJuan Andres
06/30/2023, 1:25 PMStefan Schippers
06/30/2023, 1:31 PM#include <stdio.h>
int main(int argc, char *argv[])
{
double start_l = 1.0;
double stop_l = 1.5;
double delta_l = 0.05;
double act_l = start_l;
while( act_l <= stop_l) {
printf("act_l = %g\n", act_l);
act_l += delta_l;
}
return 0;
}
the output is:
schippes@asus:~$ ./test
act_l = 1
act_l = 1.05
act_l = 1.1
act_l = 1.15
act_l = 1.2
act_l = 1.25
act_l = 1.3
act_l = 1.35
act_l = 1.4
act_l = 1.45
You see the last iteration is not performed because the last w_act
is not exactly 1.5 since adding 0,05 many times produces rounding errors. There is no exact representation of 0.05 in binary:
0,05 (dec) = 0.000011001100110011001100110011001100110011....(bin)
Juan Andres
06/30/2023, 1:33 PMStefan Schippers
06/30/2023, 1:33 PMstop_l
and you will be ok.Stefan Schippers
06/30/2023, 1:35 PMstop_l = 1.5 +delta_l / 2.0
Juan Andres
06/30/2023, 1:47 PM