Skip to main content
Code Review

Return to Answer

Commonmark migration
Source Link

###You still have a floating point problem###

You still have a floating point problem

If you change your line to this input:

auto change = getChange(0.04, 0.03);

you will get the following output:

2, 0, 0, 0, 0, 0,

As you can see, it answers with 2 pennies instead of one. The problem is with the line that you "fixed":

// I had a floating-point problem here by computing cents = (M-P)*100;
// it took me a few minutes to figure it out
int cents = (int)(M*100) - (int)(P*100);

You did notice a problem with your floating point conversion but you came to the conclusion that it was the subtraction that was the problem. The actual problem is that a floating point value of something like 0.03 can sometimes be slightly above or slightly below the actual value of 0.03. In this case, it is slightly below. When you convert this value to cents by using (int)(P*100), you end up converting something like 2.999999 to 2 instead of 3.

One way to avoid this problem is to round instead of truncate. So you can change your line to this to fix the problem:

int cents = (int) (100*(M-P) + 0.5);

###You still have a floating point problem###

If you change your line to this input:

auto change = getChange(0.04, 0.03);

you will get the following output:

2, 0, 0, 0, 0, 0,

As you can see, it answers with 2 pennies instead of one. The problem is with the line that you "fixed":

// I had a floating-point problem here by computing cents = (M-P)*100;
// it took me a few minutes to figure it out
int cents = (int)(M*100) - (int)(P*100);

You did notice a problem with your floating point conversion but you came to the conclusion that it was the subtraction that was the problem. The actual problem is that a floating point value of something like 0.03 can sometimes be slightly above or slightly below the actual value of 0.03. In this case, it is slightly below. When you convert this value to cents by using (int)(P*100), you end up converting something like 2.999999 to 2 instead of 3.

One way to avoid this problem is to round instead of truncate. So you can change your line to this to fix the problem:

int cents = (int) (100*(M-P) + 0.5);

You still have a floating point problem

If you change your line to this input:

auto change = getChange(0.04, 0.03);

you will get the following output:

2, 0, 0, 0, 0, 0,

As you can see, it answers with 2 pennies instead of one. The problem is with the line that you "fixed":

// I had a floating-point problem here by computing cents = (M-P)*100;
// it took me a few minutes to figure it out
int cents = (int)(M*100) - (int)(P*100);

You did notice a problem with your floating point conversion but you came to the conclusion that it was the subtraction that was the problem. The actual problem is that a floating point value of something like 0.03 can sometimes be slightly above or slightly below the actual value of 0.03. In this case, it is slightly below. When you convert this value to cents by using (int)(P*100), you end up converting something like 2.999999 to 2 instead of 3.

One way to avoid this problem is to round instead of truncate. So you can change your line to this to fix the problem:

int cents = (int) (100*(M-P) + 0.5);
Source Link
JS1
  • 28.8k
  • 3
  • 41
  • 83

###You still have a floating point problem###

If you change your line to this input:

auto change = getChange(0.04, 0.03);

you will get the following output:

2, 0, 0, 0, 0, 0,

As you can see, it answers with 2 pennies instead of one. The problem is with the line that you "fixed":

// I had a floating-point problem here by computing cents = (M-P)*100;
// it took me a few minutes to figure it out
int cents = (int)(M*100) - (int)(P*100);

You did notice a problem with your floating point conversion but you came to the conclusion that it was the subtraction that was the problem. The actual problem is that a floating point value of something like 0.03 can sometimes be slightly above or slightly below the actual value of 0.03. In this case, it is slightly below. When you convert this value to cents by using (int)(P*100), you end up converting something like 2.999999 to 2 instead of 3.

One way to avoid this problem is to round instead of truncate. So you can change your line to this to fix the problem:

int cents = (int) (100*(M-P) + 0.5);
lang-cpp

AltStyle によって変換されたページ (->オリジナル) /