Problem with floating point comparison

Asked

Viewed 49 times

0

Hello! I’m doing a job for college and one of the parts I have to do is take the value of h and see if it’s positive and between 0.8 and 1.2. It’s very simple, but when I type 0.8, it accepts!! Could someone help me? please. Another thing, when they say 'between', the numbers that were used to measure from which to which number to pick, also enter (0.8, 1.2)?

float h;
do{
    printf("Por favor, digite o tamanho do nivel em cm (entre 0.8 e 1.2): \n");
    scanf("%f",&h);
    if(h<0){
        printf("Favor, insira um valor positivo!\n");
    }
}while(!(h>0&&(h>0.8&&h<1.2)));
  • 1

    Wouldn’t it be simpler to just do it while (h<0.8 || h>1.2))) since, from what I understand you want a number in the interval [0.8, 1.2]?

  • Unfortunately it did not work :(. For some reason when I implement what you mentioned, the code does not accept 1.2 and accepts 0.8 (the strange thing here would be that it accepts 0.8). I have tried to put up to >= and <= for 0.8 and 1.2, but for some reason 1.2 will not :(

  • This is the problem of the inherent inaccuracy of the representation of floating point numbers. Look at this test: https://ideone.com/G7e4Xo you will find that 1.2 is not accurately represented.

  • @anonimo, I took the liberty of making a Fork of your code. Using literals floating point error the same accuracy on both sides and the comparison works as expected (https://ideone.com/zMadkX). The problem in the case is the comparison of the type float on the one hand, with literals of the kind double of the other.

  • @Anthony Accioly: perfect. This is an aspect totally neglected by beginners (especially if it involves arithmetic operations). If defining h as double will also obtain the expected result.

2 answers

1


Your question is not so clear, but if what you need is to get the numbers between 0.8 and 1.2 including the 0.8 and the 1.2 you just use the operator OR ( || ), follows the code:

float h;

do{
    printf("Por favor, digite o tamanho do nivel em cm (entre 0.8 e 1.2): \n");
    scanf("%f",&h);
    if(h<0){
        printf("Favor, insira um valor positivo!\n");
    }
}while(h < 0.8f || h > 1.2f);

How have you handled the case of h < 0 in the loop body, I see no need to treat again in the loop condition.

  • I did exactly as you did, but for some reason, when I went to test with the numbers 0.8 and 1.2, 0.8 was accepted, and there is no sign of equality in < and >. The strange thing is that 1.2 was not accepted (which would be right, because there is no >= or <= in the condition). That is, for some reason 0.8 goes in and 1.2 does not.

  • 1

    Victor, the condition no while is the failure, ie all the numbers you want to reject. If 0.8 should be rejected then you really need a <=. Another thing, you’re comparing a float (h) with two literals of the kind double (0.8 and 1.2). I would recommend or use float for everything (while(h <= 0.8f || h >= 1.2f)) or use double for everything (ie, use double h; and "%lf" in the scanf).

  • @Anthonyaccioly you’re right, thank you for the warning, the reply has been corrected. I just fixed the code of Victorhugo22 and didn’t see this detail of the float.

  • @Victorhugo22 the problem with '1.2' is the difference in accuracy between 'float' and 'double', according to Anthony Accioly’s response and the correction you must achieve.

  • 1

    I got it!!! Thank you very much Lucas Oliveira and Anthony Accioly. The problem was with the comparison of float type with double. I used 0.8f and 1.2f and it worked! I’ve never seen it before! Good that now I’ve learned something new. Again... thank you!

1

Well the code that the colleague gave as answer is correct, say you want an interval between 1 - 3, if you put a condition (input < 1 || input > 3) it will receive values less than 1, ie the 1 is included in the condition, because only smaller numbers that it does not enter, if vc wants to exclude 1 from the use condition (input < 2 || input > 3), then numbers 2 and 3 will be included in the condition.

In your case put the condition (h < 0.8f || h > 1.2f) even, as the friend said, I did here without using the "do", confer ai:

   #include <stdio.h>

   int main(void)
   {
     float h = 0;

     while (h < 0.8f || h > 1.2f){
        printf("Por favor, digite o tamanho do nivel em cm (entre 0.8 e 1.2): \n");
        scanf("%f", &h);
        if (h < 0){
           printf("Favor, insira um valor positivo!\n");
        }
   }

   return 0;
   }
  • Thanks for having answered me Gustavo Macedo Rodrigues! my problem was having put && instead of ||. I also put 'f' after 0.8 and 1.2, just like you did! Now I can deliver my work! Thanks again.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.