Skip to main content
replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

If you increase the precision of your printf format strings from 2 decimal places to 20 decimal places, you will see that using floating point arithmetic, 0.35 - 0.25 is very slightly less than 0.10. Here's a runnable demonstration on ideone.

This is what the problem set 1 specification is warning about when it says:

Incidentally, do beware the inherent imprecision of floating-point values. For instance, 0.01 cannot be represented exactly as a float. Try printing its value to, say, 50 decimal places, with code like the below:

float f = 0.01;
printf("%.50f\n", f);

Before doing any math, then, you’ll probably want to convert the user’s input entirely to cents (i.e., from a float to an int) to avoid tiny errors that might otherwise add up! Of course, don’t just cast the user’s input from a float to an int! After all, how many cents does one dollar equal? And be careful to round and not truncate your pennies!

For more details about floating point arithmetic, here are some related questions:

If you increase the precision of your printf format strings from 2 decimal places to 20 decimal places, you will see that using floating point arithmetic, 0.35 - 0.25 is very slightly less than 0.10. Here's a runnable demonstration on ideone.

This is what the problem set 1 specification is warning about when it says:

Incidentally, do beware the inherent imprecision of floating-point values. For instance, 0.01 cannot be represented exactly as a float. Try printing its value to, say, 50 decimal places, with code like the below:

float f = 0.01;
printf("%.50f\n", f);

Before doing any math, then, you’ll probably want to convert the user’s input entirely to cents (i.e., from a float to an int) to avoid tiny errors that might otherwise add up! Of course, don’t just cast the user’s input from a float to an int! After all, how many cents does one dollar equal? And be careful to round and not truncate your pennies!

For more details about floating point arithmetic, here are some related questions:

If you increase the precision of your printf format strings from 2 decimal places to 20 decimal places, you will see that using floating point arithmetic, 0.35 - 0.25 is very slightly less than 0.10. Here's a runnable demonstration on ideone.

This is what the problem set 1 specification is warning about when it says:

Incidentally, do beware the inherent imprecision of floating-point values. For instance, 0.01 cannot be represented exactly as a float. Try printing its value to, say, 50 decimal places, with code like the below:

float f = 0.01;
printf("%.50f\n", f);

Before doing any math, then, you’ll probably want to convert the user’s input entirely to cents (i.e., from a float to an int) to avoid tiny errors that might otherwise add up! Of course, don’t just cast the user’s input from a float to an int! After all, how many cents does one dollar equal? And be careful to round and not truncate your pennies!

For more details about floating point arithmetic, here are some related questions:

replaced http://cs50.stackexchange.com/ with https://cs50.stackexchange.com/
Source Link

If you increase the precision of your printf format strings from 2 decimal places to 20 decimal places, you will see that using floating point arithmetic, 0.35 - 0.25 is very slightly less than 0.10. Here's a runnable demonstration on ideone.

This is what the problem set 1 specification is warning about when it says:

Incidentally, do beware the inherent imprecision of floating-point values. For instance, 0.01 cannot be represented exactly as a float. Try printing its value to, say, 50 decimal places, with code like the below:

float f = 0.01;
printf("%.50f\n", f);

Before doing any math, then, you’ll probably want to convert the user’s input entirely to cents (i.e., from a float to an int) to avoid tiny errors that might otherwise add up! Of course, don’t just cast the user’s input from a float to an int! After all, how many cents does one dollar equal? And be careful to round and not truncate your pennies!

For more details about floating point arithmetic, here are some related questions:

If you increase the precision of your printf format strings from 2 decimal places to 20 decimal places, you will see that using floating point arithmetic, 0.35 - 0.25 is very slightly less than 0.10. Here's a runnable demonstration on ideone.

This is what the problem set 1 specification is warning about when it says:

Incidentally, do beware the inherent imprecision of floating-point values. For instance, 0.01 cannot be represented exactly as a float. Try printing its value to, say, 50 decimal places, with code like the below:

float f = 0.01;
printf("%.50f\n", f);

Before doing any math, then, you’ll probably want to convert the user’s input entirely to cents (i.e., from a float to an int) to avoid tiny errors that might otherwise add up! Of course, don’t just cast the user’s input from a float to an int! After all, how many cents does one dollar equal? And be careful to round and not truncate your pennies!

For more details about floating point arithmetic, here are some related questions:

If you increase the precision of your printf format strings from 2 decimal places to 20 decimal places, you will see that using floating point arithmetic, 0.35 - 0.25 is very slightly less than 0.10. Here's a runnable demonstration on ideone.

This is what the problem set 1 specification is warning about when it says:

Incidentally, do beware the inherent imprecision of floating-point values. For instance, 0.01 cannot be represented exactly as a float. Try printing its value to, say, 50 decimal places, with code like the below:

float f = 0.01;
printf("%.50f\n", f);

Before doing any math, then, you’ll probably want to convert the user’s input entirely to cents (i.e., from a float to an int) to avoid tiny errors that might otherwise add up! Of course, don’t just cast the user’s input from a float to an int! After all, how many cents does one dollar equal? And be careful to round and not truncate your pennies!

For more details about floating point arithmetic, here are some related questions:

Source Link
Air
  • 2.1k
  • 18
  • 29

If you increase the precision of your printf format strings from 2 decimal places to 20 decimal places, you will see that using floating point arithmetic, 0.35 - 0.25 is very slightly less than 0.10. Here's a runnable demonstration on ideone.

This is what the problem set 1 specification is warning about when it says:

Incidentally, do beware the inherent imprecision of floating-point values. For instance, 0.01 cannot be represented exactly as a float. Try printing its value to, say, 50 decimal places, with code like the below:

float f = 0.01;
printf("%.50f\n", f);

Before doing any math, then, you’ll probably want to convert the user’s input entirely to cents (i.e., from a float to an int) to avoid tiny errors that might otherwise add up! Of course, don’t just cast the user’s input from a float to an int! After all, how many cents does one dollar equal? And be careful to round and not truncate your pennies!

For more details about floating point arithmetic, here are some related questions: