Problems with printing float variables

Asked

Viewed 48 times

0

Hello folks I’m solving the 1098 issue of the Judge URI, it is apparently quite simple just have to print the results of a sequence in which i goes from 0 to 2 increasing 0.2 and j that goes from 1 to 3 + i, the J repeats goes from 1 to 3 every time I increases 0.2, the way the system wants broken numbers to be written with only one house after the comma and when integer numbers cannot have comma.

Okay I did a cute procedure and created a conditional for cases where I have to write as an integer, but apparently it only enters it when the number is 0 and 1 (thus missing the case when the sequence number is 2). Right below I will put the code, the desired output and the output of my code:

#include <stdio.h>
#include <stdlib.h>

int main () {
   float i = 0;
   float j = 0;
   int cont = 1; 
   while (i <= 2.2){
      for (cont = 1; cont <= 3; cont++){
         if(i == 0 || i == 1 || i == 2){
             printf ("I=%.0f J=%.0f\n", i, j+i+cont);
         }else{
             printf ("I=%.1f J=%.1f\n", i, j+i+cont);
         }
      }
      i += 0.2;
      j = 0;
   }
   return 0;
}

Desired exit:

I=0 J=1
I=0 J=2
I=0 J=3
I=0.2 J=1.2
I=0.2 J=2.2
I=0.2 J=3.2
I=0.4 J=1.4
I=0.4 J=2.4
I=0.4 J=3.4
I=0.6 J=1.6
I=0.6 J=2.6
I=0.6 J=3.6
I=0.8 J=1.8
I=0.8 J=2.8
I=0.8 J=3.8
I=1 J=2
I=1 J=3
I=1 J=4
I=1.2 J=2.2
I=1.2 J=3.2
I=1.2 J=4.2
I=1.4 J=2.4
I=1.4 J=3.4
I=1.4 J=4.4
I=1.6 J=2.6
I=1.6 J=3.6
I=1.6 J=4.6
I=1.8 J=2.8
I=1.8 J=3.8
I=1.8 J=4.8
I=2 J=3
I=2 J=4
I=2 J=5

Out of my code:

I=0 J=1
I=0 J=2
I=0 J=3
I=0.2 J=1.2
I=0.2 J=2.2
I=0.2 J=3.2
I=0.4 J=1.4
I=0.4 J=2.4
I=0.4 J=3.4
I=0.6 J=1.6
I=0.6 J=2.6
I=0.6 J=3.6
I=0.8 J=1.8
I=0.8 J=2.8
I=0.8 J=3.8
I=1 J=2
I=1 J=3
I=1 J=4
I=1.2 J=2.2
I=1.2 J=3.2
I=1.2 J=4.2
I=1.4 J=2.4
I=1.4 J=3.4
I=1.4 J=4.4
I=1.6 J=2.6
I=1.6 J=3.6
I=1.6 J=4.6
I=1.8 J=2.8
I=1.8 J=3.8
I=1.8 J=4.8
I=2.0 J=3.0
I=2.0 J=4.0
I=2.0 J=5.0

As you can see the error is only in the last three values, in which when i is equal to 2 and I can’t figure out how to put it into integer form being that in my theory it should enter my parole.

  • 1

    Take great care when making accurate comparisons with float variables (for example equality), the representation of floating point numbers is inherently inaccurate (see ANSI / IEEE Std 754-1985 standard). Try rounding to 1 decimal place.

  • An option for your solution is to treat your numbers as integers multiplied by 10 and at the time of printing divided by 10. for (int i=0; i<=22; i+=2) { if (i==0 || i==10 || i ==20)...

1 answer

1


all right? So I won’t know how to explain to you why this occurs but apparent from the moment it assumes the 2.0 it does not assume an exact value but very close to 2.0 as for example 2,000...1 soon when making the comparison with the value 2 it will not enter your parole, one way to solve this is by changing the

  if(i == 0 || i == 1 || i == 2){}

for

  if(i == 0 || i == 1.0 || i > 1.9){}

this way it enters the last term of the conditional and will not actually change the execution since the code will terminate in the next iteration.

Browser other questions tagged

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