Deletion by moderator [closed]

Flagged wrong answer - FAQ too unspecific?

and I cited an example with IMHO wrong answers.

Now Robert Harvey deleted my answer on the very same question:

Is this a valid float comparison that accounts for a set number of decimal places?

If the user wants to "compare two floats using a set number of decimal points (significant figures)" there is a simple but unfortunate answer:

It is NOT possible. All other answers which say it is or provide algorithms are WRONG (naturally with the assumption that operations like + - * and / should provide correct decimal answers.

Reason: The decision to round either upward or downward depends if the next digit is either 0.49999999999999 or 0.5000000001. As floats are always incorrect approximations to both values, you simply cant, either we would not have the need to use decimal or money classes.

The C# documentation even cites an example: http://msdn.microsoft.com/en-us/library/75ks3aby.aspx

``````Notes to Callers

Because of the loss of precision that can result from representing
decimal values as floating-point numbers or performing arithmetic
operations on floating-point values, in some cases the Round(Double,
Int32) method may not appear to round midpoint values to the nearest
even value in the digits decimal position. This is illustrated in the
following example, where 2.135 is rounded to 2.13 instead of 2.14.
This occurs because internally the method multiplies value by
10digits, and the multiplication operation in this case suffers from a
loss of precision.
``````

And as float has only 24 bits, it means that 16 777 216+1 == 16777216. You are screwing up if you use floats for decimal points.

EDIT: Because it seems that noone wants to see the problem, here the proof:

The very first problem is that the algorithms here are broken: AlmostEquals(0.06f, 0.14f, 1) = true.

The reason is that the original poster asked explicitly for equal decimal places. But even if we use a special round function, there is no way to escape the problem.

I used VC 2008 to print out the correct values of the Math.pow function. The first is the precision parameter, the second the hex value of the resulting float and the third is the exact decimal value.

1 3dcccccd 0.100000001490116119384765625

2 3c23d70a 0.00999999977648258209228515625

3 3a83126f 0.001000000047497451305389404296875

4 38d1b717 0.0000999999974737875163555145263671875

5 3727c5ac 0.00000999999974737875163555145263671875

6 358637bd 9.999999974752427078783512115478515625E-7

So you can try

AlmostEquals(0.0f, 0.100000001490116119384765625f, 1) or

AlmostEquals(0.0f, 0.0999999940395355224609375, 1)

and show me a function which is able to get correct answers for both values and is generalizable.

I see here a little problem.

Even if you assume that my answer is NOT correct....

• There is an answer: It is NO. Due to floating-point restrictions it is not possible to write a function which is able to work exactly as the author intended. You may get a function which works sometimes

• I added the Microsoft documentation of the round function which explicitly warns that the result will sometimes not work as expected.

• I provided a counterexample of both suggested implementations AlmostEquals(0.06f, 0.14f, 1) = true.

• I also printed out the results of the VC2008 implementation to show that the stepsizes do not fulfill the necessary conditions and show that it is in fact not possible to generate a function which satify both tests and can be generalized.

EDIT: It was mentioned in the comments that the question is vague. If this is the case, then surely it is not the problem of my answer, it applies to the question and the answers given.

But I cite now the OP:

@MrLister My original problem was that I wanted (0.1F + 0.2F) == 0.3F to be true (which returns false in C#). But the implementation can be used to say that 123.123 almost equals 123.124 with 2 decimal places. I'm not concerned that 12,300 would equal 12,400 if you took 2 significant figures into account. That's what I meant about decimal places

@sixlettervariables: Actually upon further reading, it's decimal-places I'm trying to get at here. Not significant figures.

If you find the question vague, tell me an alternative implementation for which "123.123 almost equals 123.124 with 2 decimal places." AND which wants decimal places, not significant figures.

-

closed as too localized by Robert HarveyMar 18 '12 at 21:47

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

While I'm not sure if your answer is right(the question is pretty vague, and can be interpreted in several ways), I certainly don't see why it should be deleted. – CodesInChaos Mar 18 '12 at 20:08
Look up the comments under @infact and under the question itself. The author himself states what exactly he wants. – Thorsten S. Mar 18 '12 at 20:10
Your answer was flagged as "Not an answer". I pondered whether I should agree or disagree with that flag, since your answer didn't directly answer the question, but indirectly dealt with it. It's a judgment call and apparently the moderator agreed that it wasn't an answer to the question. – Daniel Fischer Mar 18 '12 at 20:18
"Look up the comments under @infact and under the question itself. The author himself states what exactly he wants." I went over the page several times, I dare not guess what the author of the question really wants, it's extremely vague. – Daniel Fischer Mar 18 '12 at 20:20
"Since your answer didn't directly answer the question"...Lets say, one is asking "How do I drive a nail into the wall with my forehead ?" and while people are giving him some positions where the skull is thickest I tell him: "No, dont do that. While you may be occasionally successfull, it hurts and you may be harmed". Is that not an answer because I do not give the best advice to nail himself ? – Thorsten S. Mar 18 '12 at 20:27
I've read everything several times, and I still don't know what result the OP expects for `AlmostEquals(0.149f, 0.151f, 1)`. – CodesInChaos Mar 18 '12 at 20:32
That's a bad example, Thorsten, since there it's clear cut. If you say it's not possible to drive a nail into a wall with one's forehead, you're plain wrong, and if you just say "Don't", you're manifestly not answering the question. Now for the question in question, "Have I oversimplified or is this valid?" would give you the opener "It's not valid, you have oversimplified. But that's because it's impossible, since ..." and that would clearly be an answer to the question. Perhaps wrong, perhaps correct, depends on what the OP really wants. – Daniel Fischer Mar 18 '12 at 20:38
Oh, and by the way, edit to make it more directly an answer to the question, and flag for moderator attention, ask for undeletion. Wouldn't be a lost cause. – Daniel Fischer Mar 18 '12 at 20:41
@Arjan His answer is that it's impossible to fulfill the requirements of the question. Which of course implies that he can't give correct code. So he instead tried to prove that impossibility. – CodesInChaos Mar 18 '12 at 20:53
If formulated like that, then that doesn't read like a rant to me, @CodeInChaos. – Arjan Mar 18 '12 at 20:54
@Daniel: "edit to make it more directly an answer to the question". May I ask that you give an example how exactly can my answer be formed more directly ? "and flag for moderator attention, ask for undeletion". "Undelete" shows "vote to restore this post" which is unlikely to succeed and after my experience with flagging I am extremely reluctant to do that again – Thorsten S. Mar 18 '12 at 21:05
I gave you a start for more directly tying your post to the question, build on that. For more tips, ask somebody more eloquent. As for the reluctance to flag, what can happen? The flag can be declined, that's all. With flag weight gone, that doesn't even set you back much if you're trying to get a Marshal badge. Just be polite, say that you think that (especially after the edit) the deletion should be reversed. Or take the risk and post a new answer. If that gets deleted too as "not an answer", then, however, you may be in a bit of trouble. – Daniel Fischer Mar 18 '12 at 21:24

Is the answer of sufficient value to anyone that it is worth saving?

It does, after all, have two downvotes. Generally, when one of my answers attracts two or more downvotes and one moderator flag, I would conclude that it is not of much value to anyone else. At that point, I generally delete it myself, and get on with life.