It is necessary to assign the number X of a real type to the variable X, for example 1.1. Immediately after typing, the keyboard is rounded off and therefore the wrong answer in the expression and else if does not work. What is the error?

#include <stdio.h> #include <conio.h> #include <math.h> #include <locale.h> float x, w, b = -0.3, c = 4, a = 2.8; int main(void) { setlocale(LC_ALL, "rus"); printf("Для продолжения нажмите любую клавишу..."); getch(); printf("\nВведите переменную x ->"); scanf("%f", &x); if (x < 1.2) { w = a * pow(x, 2) + b*x + c; } else if (x == 1.2) { w = a / x + sqrt(pow(x, 2) + 1); } else { w = (a + b*x) / sqrt(pow(x, 2) + 1); } printf("\aa=%2.1fb=%2.1fc=%1.0fx=%2.1fw=%3.1f", a, b, c, x, w); getch(); printf("\nДля завершения нажмите любую клавишу..."); getch(); return 0; } 
  • For some numbers, the format of representing them with a floating point in the computer does not allow storing their exact values. - Vlad from Moscow

3 answers 3

The representation of a floating-point number in memory is approximate, as a sum of powers of two. Therefore, as in the decimal notation you cannot accurately write down, say, 1/3 - you need an infinite number of digits - the same happens with numbers in the binary number system. Therefore, there is a trimming of numbers to some accuracy, and it is impossible to compare numbers for simple equality in the general case.

Your code would still work, no matter how funny it is - because you enter, say, 1.2 and compare it with 1.2 — that is, their representations are the same , but the fact is that for some reason you are using float , while the literal 1.1 takes the compiler as double , and the representations of the same number are different.

So you need to compare on the basis of |ab| < ε |ab| < ε , and how to choose ε depends on the problem and the values ​​of a and b .

  • Add: in most cases, as ε you can take max(a, b) * numeric_limits<type_of_a>::epsilon() . - ߊߚߤߘ
  • one
    @Arhad is no better than comparing with == . epsilon() gives only one unit of the lower level of the mantissa, but the last few signs should really be thrown away. - Pavel Mayorov

To compare real numbers for equality, you need to use something like the following:

 if( fabs(ab) < 0.000001 ) printf("Числа равны"); else printf("Числа не равны"); 

Print х immediately after scanf("%f", &x);

    Since you changed the locale to Russian ( setlocale(LC_ALL, "rus"); ), be careful that real numbers in the Russian locale are written with a comma.

     #include <stdio.h> #include <conio.h> #include <math.h> #include <locale.h> float x, w, b = -0.3, c = 4, a = 2.8; int main(void) { setlocale(LC_ALL, "rus"); // после этой команды, числа с плавающей запятой надо вводить через запятую! printf("Для продолжения нажмите любую клавишу..."); getch(); printf("\nВведите переменную x ->"); scanf("%f", &x); if (x < 1.2) { w = a * pow(x, 2) + b*x + c; } else if (x == 1.2) { w = a / x + sqrt(pow(x, 2) + 1); } else { w = (a + b*x) / sqrt(pow(x, 2) + 1); } printf("\aa=%2.1fb=%2.1fc=%1.0fx=%2.1fw=%3.1f", a, b, c, x, w); getch(); printf("\nДля завершения нажмите любую клавишу..."); getch(); return 0; } 

    To continue, press any key ...
    Enter the variable x -> 1.9
    a = 2.8 b = -0.3 c = 4 x = 1.9 w = 1.0

    • I wanted to leave it as a comment, but my reputation is not enough) - Kto To