Skip to main content
added 98 characters in body
Source Link
kzidane
  • 17.7k
  • 3
  • 28
  • 100

Well, if a compilation error happened, you won't be able to run your program at all because it won't compile (the executable ./remainder in your case won't exist)!

So this has nothing to do with the compiler.

Let's walk through your code to see what's really happening typically

  1. You get a float value from the user and store it in f (e.g., 9.73).

  2. You multiply this value by 100 and store the result in n. Notice that because n is an int, all the digits after the decimal points are truncated. So

    n = 9.73 * 100 = 973
    
  3. You then you divide n by 25 and storing the result in i. So

    i = 973 / 25 = 38
    
  4. Finally, you're taking the remainder of dividing n by 25 and storing the result in n. So

    973 / 25 = 38
    38 * 25 = 950
    973 - 950 = 23
    

and you're printing the value of i and n respectively which should be 38 and 23.

But what really happened? 

The problem is that floats are NOT precise. This means thatMeanwhile, when you enter thea value like 9.73, it isn't necessarily stored as 9.73. It can

In fact, it may be stored as 9.72999954(depends on many things). And since you're storing it in an int, you get 972 stored at the end (as the digits after the decimal points are truncated). And you

You then play with thisthe 972 all the way down through your code! And that explains why you get 22 instead of 23.

Well, if a compilation error happened, you won't be able to run your program at all because it won't compile (the executable ./remainder in your case won't exist)!

So this has nothing to do with the compiler.

Let's walk through your code to see what's really happening typically

  1. You get a float value from the user and store it in f (e.g., 9.73).

  2. You multiply this value by 100 and store the result in n. Notice that because n is an int, all the digits after the decimal points are truncated. So

    n = 9.73 * 100 = 973
    
  3. You then you divide n by 25 and storing the result in i. So

    i = 973 / 25 = 38
    
  4. Finally, you're taking the remainder of dividing n by 25 and storing the result in n. So

    973 / 25 = 38
    38 * 25 = 950
    973 - 950 = 23
    

and you're printing the value of i and n respectively which should be 38 and 23.

But what really happened? The problem is that floats are NOT precise. This means that when you enter the value 9.73, it isn't necessarily stored as 9.73. It can be stored as 9.72999954. And since you're storing it in an int, you get 972 stored at the end. And you play with this 972 all the way down through your code! And that explains why you get 22 instead of 23.

Well, if a compilation error happened, you won't be able to run your program at all because it won't compile (the executable ./remainder in your case won't exist)!

So this has nothing to do with the compiler.

Let's walk through your code to see what's really happening typically

  1. You get a float value from the user and store it in f (e.g., 9.73).

  2. You multiply this value by 100 and store the result in n. Notice that because n is an int, all the digits after the decimal points are truncated. So

    n = 9.73 * 100 = 973
    
  3. You then you divide n by 25 and storing the result in i. So

    i = 973 / 25 = 38
    
  4. Finally, you're taking the remainder of dividing n by 25 and storing the result in n. So

    973 / 25 = 38
    38 * 25 = 950
    973 - 950 = 23
    

and you're printing the value of i and n respectively which should be 38 and 23.

But what really happened? 

The problem is that floats are NOT precise. Meanwhile, when you enter a value like 9.73, it isn't necessarily stored as 9.73.

In fact, it may be stored as 9.72999954(depends on many things). And since you're storing it in an int, you get 972 stored at the end (as the digits after the decimal points are truncated).

You then play with the 972 all the way down through your code! And that explains why you get 22 instead of 23.

Source Link
kzidane
  • 17.7k
  • 3
  • 28
  • 100

Well, if a compilation error happened, you won't be able to run your program at all because it won't compile (the executable ./remainder in your case won't exist)!

So this has nothing to do with the compiler.

Let's walk through your code to see what's really happening typically

  1. You get a float value from the user and store it in f (e.g., 9.73).

  2. You multiply this value by 100 and store the result in n. Notice that because n is an int, all the digits after the decimal points are truncated. So

    n = 9.73 * 100 = 973
    
  3. You then you divide n by 25 and storing the result in i. So

    i = 973 / 25 = 38
    
  4. Finally, you're taking the remainder of dividing n by 25 and storing the result in n. So

    973 / 25 = 38
    38 * 25 = 950
    973 - 950 = 23
    

and you're printing the value of i and n respectively which should be 38 and 23.

But what really happened? The problem is that floats are NOT precise. This means that when you enter the value 9.73, it isn't necessarily stored as 9.73. It can be stored as 9.72999954. And since you're storing it in an int, you get 972 stored at the end. And you play with this 972 all the way down through your code! And that explains why you get 22 instead of 23.