- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
"Why fabs(number1) > 0, and not, I don't know, number1 != 0?"
Habit. When comparing floats to 0, it's prudent to use > rather than != because "0" can be a bit of a moveable feast.
Whether you need to do that here or not is up for discussion, but NAWTF.
Admin
unless we're counting molecules, I don't think we can call a default value of 10000000000000000000 "small"
Admin
Maybe because "number1 !=0" won't catch negative values would be my guess
Addendum 2022-10-18 07:10: Doh! Totally missed the "fabs" part of the.
Seriously though Bruce Dawson had an excellent blog article (randomascii at wordpress) about comparison of floating point numbers and what can go wrong.
Admin
Great link. Very informative.
Admin
Sure, but in this case, we are only checking against divide by zero errors.
Admin
The
fabs(number1)
call might have been intended to protect against negative 0. Might have been. Except, negative 0 compares as equal to positive 0, so a test for== 0
would still have correctly handled negative 0 as well.Admin
Well there are multiple representations of zero in IEEE 754, but I would hope the compiler or fp hardware would understand that.
I think this is cargo cult programming. Somebody has heard the "do not compare floating point values for equality" rule and compared for equality without using an equals sign. They have adapted the
fabs(a - b) > some_small_value
pattern and broken it by using 0 instead of some small non zero value.Addendum 2022-10-18 09:20: Technically, they are comparing for inequality.
Admin
Back in the mid to late 90's I encountered a situation where 0 != -0. This would have been on a Pentium Pro with some version of MS Visual C. I had to go with the fuzzy equal route to get that code to work.
Admin
On the scale of Good / Bad / File Not Found, I fear this WTF comes down at File Not Found. What's this file being validated? And what does the mythical file have to do with percentages?
Admin
Half-way to a decent solution in the engineering domain.
That small number should be a tolerance, not a pointless initialisation. And (assuming no operator overload, which would require a class constant for the tolerance at the least), you need to test for "zero" by taking the absolute value of one double minus another double, and comparing that result to the tolerance.
At which point you have a "zero" case. Whether that is meaningful in this instance depends upon whether the values really need to be doubles or floats in the first place. But it's basically your special case: do whatever you want with it, but calculating a percentage off some tiny little denominator (or numerator) is a mug's game and will rapidly decay using basic numerical analysis.
I had to do three code reviews on a mathematician's version of something very like this. Apparently those versed in pure mathematics do not care to consider real world consequences.
Admin
So, delta (0.25, 1) = 0.75 , not 3.00? Interesting definition of "percentage"
Admin
This is a pretty common pattern. When receiving regular updates to a file, you compare the old size with the new size, to make sure it hasn't changed too much. A big change, especially large shrinkage, can indicate that something went wrong when generating or downloading the update.
But when I've done this, it's the old size that I divide by. Except for the first time, we shouldn't have to worry about the old file being zero-length, and I deal with that by handling a nonexistent old file separately. If the new file is zero-length, the percentage difference will be 100%, which should fail validation.
Admin
The funny thing is that, in the context of the code, this works. The developper calling delta() was aware of its peculiarities. They just didn’t document the dratted thing (that’s the usual boring WTF).
Admin
I guess "ratio" would be the proper word. Personnally I have always considered that % meant /100, so 0.75, 75/100 and 75% are all the same (as a kid that would lead to some heated discussions with math teachers who expected things like 3/4*100=75%)
Admin
I don't know if Remy is trolling us, but 1e19 is not a small number, it's enormous.
Admin
Oh, Industrial Automation Engineer got there first...
Admin
You may think 1E19 is enormous, but it's peanuts compared to how large the galaxy is...
Admin
Size of the galaxy in micrometers or in milions of light years? Again, numbers without units...
Admin
Even if counting molecules, 1e19 would still be almost 0,02 mmol, so eg. over 1/3 mL of gas - small, but still a non-negligible, macroscopic amount. Not so for water, a fraction of a microliter is far too small to even call it a droplet.
Admin
I can see a possible reason for that useless initialization. I've done a few of them over the years because the system wasn't bright enough to realize that the value was assigned in all possible paths and was complaining about using an uninitialized value.
Admin
It helps to remember that % is literally "per cent", which means per 100.
Admin
If one value is 0, and the other is non-zero, I would call that 100% difference for practical reasons (I think it's undefined in theory). But then again, when I use code like this, I'm usually looping until the difference is less than some very small value (e.g. 1E-12).
Admin
"23 comments - Last comment @ 02:03" - but when?!
Admin
Today. If the last comment is on a previous day, it's always shown as just a date on these pages.
Admin
If they only replaced that "100" with "100%" then everything would be fine.
Admin
There is no today, as long as it isn't in UTC :-)