Hello,
please advise on the following issue. I need to check that difference
between two double values is not higher than defined limit.
int main()
{
double limit = 0.3;
double val1 = 0.5, val2 = 0.2;
if ( (val1 - val2) limit )
puts("Differenc e higher than limit!");
return 0;
}
This program displays message due to undefined binary presentation of
double, so result is unexpected. I see that it is behavior by
standard. But what can I do to make this code work as expected, i.e.
treat 0.3 as 0.3, not as (for example) 0.3000000000000 000012? Are
there any standard ways (macros, functions) to fix the precision for
double integers? Everything I found is just formatting for input-
output functions, but it doesn't affect the binary presentation, just
visualisation.
Thank you in advance.
please advise on the following issue. I need to check that difference
between two double values is not higher than defined limit.
int main()
{
double limit = 0.3;
double val1 = 0.5, val2 = 0.2;
if ( (val1 - val2) limit )
puts("Differenc e higher than limit!");
return 0;
}
This program displays message due to undefined binary presentation of
double, so result is unexpected. I see that it is behavior by
standard. But what can I do to make this code work as expected, i.e.
treat 0.3 as 0.3, not as (for example) 0.3000000000000 000012? Are
there any standard ways (macros, functions) to fix the precision for
double integers? Everything I found is just formatting for input-
output functions, but it doesn't affect the binary presentation, just
visualisation.
Thank you in advance.
Comment