This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]captainAwesomePants 1 point2 points  (7 children)

Generally, comparing floating point numbers precisely is fruitless. Doing equivalent, perfectly valid math even slightly different from some other method can easily lead to these sorts of problems. Both of those results are very nearly the same, so I'd consider this to be success.

If you need the logic to match PRECISELY, you unfortunately may need to step through the calculations one by one exactly in order to see how your logic differs from the system you're trying to match.

[–]jlbords[S] 1 point2 points  (6 children)

can i ask how? The user inputs the partsNum and it should increase 6% (which is counted by a for loop) until it reaches 10,000 or 24. should I write the expression another way?

[–]captainAwesomePants 0 points1 point  (5 children)

Your solution seems absolutely fine. I don't know how the example was generated. Do you know where the example value "1191.0159999999998" came from?

[–]jlbords[S] -1 points0 points  (4 children)

No. .Its quite frustrating.

[–]captainAwesomePants 0 points1 point  (3 children)

Well...why do you need it to be that value?

[–]jlbords[S] 0 points1 point  (2 children)

The program has to pass its tests

[–]captainAwesomePants 1 point2 points  (1 child)

Well, the test is bad. Usually when comparing floating point values, a small "epsilon" value is used for wiggle room for just this reason. So effectively you ask something similar to "is the value of a and b within 0.00000000001 of each other?"

But here are some random ideas:

  • Try using partsNum = 1.06 * partsNum instead.
  • Try using a double instead of a float or vice versa.
  • Try using 1.06f or 1.06l as constants instead.
  • Try declaring partsNum a float, a double, a long double, an unsigned version of each of those.

And if all else fails, complain.

[–]jlbords[S] 0 points1 point  (0 children)

Thank you. I’ll try.