• (nodebb)

What's the differenz between a BMW and a Benz?

• Burnus (unregistered)

But the for loop compares the values rounded to the (i+1)th decimal. So wouldn‘t that erroneously stop whenever dValue contains a 0 inside the range to round?

For example if dValue = 1.20345 and iDecimals = 3, the expected output is „1.203“. However, at i=1 it would see that dummy = 1.2 and dValue, rounded to 2 decimals is 1.20, which is equal to 1.2. So it should break right there and output „1.2“.

• Pag (unregistered)

Bonus points for using homebrew sprintf in one line then string concatenation in the next

• (nodebb)

They're also using Doubles for rounding to DECIMAL values. That... does not work the way you think it works. A Double cannot store things like, say 0.1 precisely, so I wouldn't really be sure that RoundTo(0.1, 1) and RoundTo(0.1, 2) would produce the exact same result...

• Heinebold (unregistered)

I wish TRWTF were the Hungarian notation and the mixed German and English variable names, but the code is so weird that even those don't manage to outWTF it

• RLB (unregistered)

This is one of those where I'm not sure which factor is greater, the WhatTF, the WhyTF or even the HowTF.

• (nodebb)

I don't even want to know the horrors of how that Sprintf method is used. Probably by a stubborn "string.Format is not good enough"/"I know my way around how we did string formatting in the 1970s!" fella.

I'll do you one better: WhoTF?

• (nodebb)
1. Burnus has it right...

2. Both my boat and (one of my) cars have tillers, not wheels.....

• Brian (unregistered)

So they know about string interpolation, yet still use a homebrew sprintf. That's just... wow.

• Tinkle (unregistered)

If the Sprintf will leave trailing zeros then the for loop does have a use - removing them.

Still a huge WTF

• (nodebb)

Nods to RLB and Network Noadle:

This is one of those where I'm not sure which factor is greater, the WhatTF, the WhyTF or even the HowTF.

I'll do you one better: WhoTF?

We almost have the makings of a WTF interface here. We need is WhereTF -- as in, "WhereTF did you learn how to code?" We'll also need a WhenTF to support multiple threads, but only if they're unsafe ;)

• tbo (unregistered) in reply to Burnus

Yeah, but if the sting/number contains a 0, that's a better place to round to, because it's really "close" to what you round it to.

_>

• aalien (unregistered)

Knowing a programming language is one thing, knowing how to program is another. If one knows how to program, changing to a different language is like changing a car: you need to study where all the buttons are, but the traffic rules don't change. The writer of that piece of WTF clearly didn't know either.

• Argle (unregistered) in reply to aalien

I'm with you on this, aalien. If you know fundamental programming ideas, the language shouldn't matter. I transitioned easily from C to C++ to C#. Not too long ago, I rewrote a 30 year old basic program in C#. It was a textbook example of what happens when a typical electrical engineer is essentially forced to write a program. Like today's WTF, it did strange and unnecessary loops (in one case, to make sure one value was a multiple of another).

• akozakie (unregistered)

That... is beautiful. It wasn't easy, but I followed the logic. The moment you realize the train of thought that must have led to some decisions... it's thrilling. Nevermind the bug (already pointed out above) - the way this takes various ideas sort-of-related to the problem, puts them all in and fixes until it works... That's a truly beautiful mind at work. It's not programming, it's art. Incomprehensible, illogical, beautiful art - that still produces correct output most of the time.

The thing I like the most is the pre-return processing. Imagine the thinking: "1. Ok, I'll build a format string (todo for later), then return dValue formatted like that. Cool, let's make that format. 2. Oh, but I used dDummyRound inside and now it contains a rounded value - maybe it's safer to use that for return? No problem, I'll just... (drumroll) set dValue to dDummyRound! 3. Wait... I still have doubts... Are those two values different? Sometimes... If they aren't, then this doesn't matter... Ah, of course! (drumroll) I'll just check if they differ, and skip the assignment if not! There, all done!"

See? Not once do they go back on a decision. They just patch around it. And whenever they get an idea, they implement it, and if it doesn't break the function - it must have been good.

I'm getting chills. This is awesome and terrifying. Uncharted territory of a very different kind of brain.

• (nodebb)

Does dDifferenz == 0.0 even work or does this have to account for machine epsilon?

• (nodebb) in reply to TheGreatLobachevsky

Does dDifferenz == 0.0 even work or does this have to account for machine epsilon?

By blind coincidence (or deliberate design???), no.

`RoundTo(dDummy - dDummyRound, i + 1)` will make sure (if `RoundTo` does its job correctly) that if it should be zero, `dDifferenz` will be a very small amount rounded to only one more decimal place than the initial value of `dDummyRound` was rounded to, so it will be zero.

It's a weird function - what it does is find the smallest number of decimal places that would, if we rounded to one more place, produce a trailing zero, and rounds to that smallest number of places, up to a maximum specified number of places.

Example: if the input is 1.23456789 and the requested max places is three, it produces "1.235".

If the input is 1.20456 and the requested max places is three, it produces "1.2" and not "1.205".

Not sure why you'd want that, but that's what it does.

Addendum 2021-12-08 13:34: EDIT: the weirdness of the description is a clear hint that something is not quite right in the original developer's brain.

• Loren Pechtel (unregistered)

The intent is obviously to not have a number with trailing zeroes after rounding. That doesn't mean that's what you actually get, though.

As for the pseudo-sprintf and string interpolation--the original programmer might not have known, or it might be from before it was in the language. It's not all written at the same time.

• xtal256 (unregistered) in reply to akozakie

People are worried about the kind of code that GPT-3 / CoPilot will stitch together, but when humans are already writing code as messy as then then I'd say we should have been worried all along.

• jochem (unregistered)

Ahhh, the pain! And I can even imagine that it's not allowed to fix this, or worse, you have to write your own code like this. Because the original writer has since become lead senior architect, and this is now the enforced coding style...

• RLB (unregistered) in reply to Bananafish

Implemented, of course, in Node.js

• (nodebb) in reply to RLB

Implemented, of course, in Node.js

That goes without saying, no?

• Gnasher729 (unregistered)

It looks like any value for example from 4.95000001 to 5.049999999 would be rounded to 5 which is nonsense when rounding to 2 or more decimals.

• Chris (unregistered)

I want to see the RoundTo function. I have \$50 saying there's casting to and from strings involved.

• Joe M (unregistered) in reply to akozakie

Actually, I think the goal of the for loop was to drop extraneous zeros.

so if they wanted to round 1.13000001 to 3 decimals, they didn't want the output to be 1.130, they wanted it to be 1.13.

Still incredibly bad way of doing it.

• Viz Deals (unregistered)
Comment held for moderation.
• Mr. Smith (unregistered)
Comment held for moderation.