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.

• 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. Commented Mar 18, 2012 at 20:08
• Look up the comments under @infact and under the question itself. The author himself states what exactly he wants. Commented Mar 18, 2012 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. Commented Mar 18, 2012 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. Commented Mar 18, 2012 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 ? Commented Mar 18, 2012 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)`. Commented Mar 18, 2012 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. Commented Mar 18, 2012 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. Commented Mar 18, 2012 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. Commented Mar 18, 2012 at 20:53
• If formulated like that, then that doesn't read like a rant to me, @CodeInChaos. Commented Mar 18, 2012 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 Commented Mar 18, 2012 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. Commented Mar 18, 2012 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.

• In other news, these new real-time updates are awesome. The upvotes on this answer caused the count to automagically change to 3, and I didn't even have to refresh the page. :D
– user102937
Commented Mar 18, 2012 at 21:28
• You have an easy way out, moderators are not expected to judge the technical accuracy of an answer. You and casper don't like the janitorial aspect of the job much? I imagine, but I too would prefer if y'all tread lightlier. Commented Mar 18, 2012 at 21:41
• @UphillLuge: I don't need technical expertise to see that an answer isn't being well-received by the community.
– user102937
Commented Mar 18, 2012 at 21:42
• "Is the question of sufficient value to anyone that it is worth saving?" Hm, did you mean my answer or the question of the OP ? If you mean my answer, let's imagine this: My answer MIGHT be actually true independently of the question if people like it. In this case more competent people of the question might look at this thread and see how my complaint was received. My decision: I want the answer back even if I receive further downvotes. Commented Mar 18, 2012 at 21:46
• @ThorstenS: Undeleted. Next time, why don't you just cast a moderator flag on the answer? These lengthy discussions on Meta are not really necessary for a single answer.
– user102937
Commented Mar 18, 2012 at 21:57
• Hey, you think it is fun for us to invoke the drama to override a moderator decision? Oodles of rep and I can't do anything reasonable to open a closed question that I posted a good answer to. I have no clue how this happened, I can only guess because it's because of that doggone fire hose that's the flag queue. Backing up bad again lately. You can, the guy isn't here anymore to tell you that you can't reject a flag. Please do! Commented Mar 18, 2012 at 22:02
• (And as for your other news, @Robert: new comments are shown in realtime too. I like that a lot!) Commented Mar 18, 2012 at 22:03