- 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
Doesn't double / int return a double and perform proper division and NOT integer division?
TRWTF is whoever wrote that.
Admin
It's also possible that the author wanted to annotate each part of the expression with a type, "for the benefit of the reader".
I guess we'll never know.
Admin
I briefly wondered if
Yeah, the code could have been written without any casts at all: And yet he didn’t completely parenthesize the expression, possibly leaving the reader wondering if C# evaluates multiplication before division or not.Convert.ToInt32
could actually take anInt32
as a parameter. As it turns out, yes. It even specifies that the function does nothing.Admin
Admin
Admin
And I do think that TJ should meet the cluebat, a bit. The description of the cast-to-double-then-divide is just so much gibberish. There's also my C/C++ head that suggests that cast-to-int might change the type of an Int32 value, platform permitting, but I'm also aware that C# doesn't follow those rules, so I avoid the cluebat, this time.
Then again, I once worked in a C/C++ (with a bit of x86 assembler in the mix) shop where the development manager, allegedly himself a C/C++ programmer, freely admitted that he didn't get pointers. (I found this out about a year after I joined them. For unrelated reasons I left about six months later.)
Admin
That's like a journalist saying they don't understand letters
Admin
Admin
Step 1: Ensure that the number of students is less than or equal to 2,147,483,647. Step 2: For 64 bit machines, promote to Int64 to allow for more students in a later calculation.
Admin
Yup, it does.
Reminds me: I once worked on a compiler for a Digital Alpha, and integer division (int/int) simply meant that the compiler transferred the two numbers into the math processor, did the division, and then transferred the result back into the main cpu as an integer. My task (which actually is a nice coding challenge) was to do it properly using only integers.
Admin
Huh? C#, like most C like languages (and Python, at least) give the same order of precedence to * and / and evaluate left-to-right, so “a / b * c” is always interpreted as “(a / b) * c”.
Admin
Woosh much?
Admin
As always, if you feel the need to have fine-grained control of the way in which sub-expressions are evaluated, use the tools provided by the language (round brackets, intermediate assignments, etc.).
Admin
Admin
(100 / 3) * 5 != 100 / (3 * 5)
Admin
Admin
Where the second one is integer operations. The inequality is because 100 does not evenly divide by 3, so there is a loss of precision that gets multiplied up. And, of course, the first one assumes an infinite number of decimal places!
Admin
Whoosh.
100 / 3 * 5 == 100 * 5 / 3
Admin
I wonder if anyone reading that was thrown by that. I mean, unless you know
int
is C# short-hand forSystem.Int32
, it'd look like you're introducing a new data type.Admin
Doesn't that depend on the compiler? You can probably bet on the fact that there is some compiler that it even more TRWTF than this code that or some speed-up-reason performs an integer-division in this case. I'd say normal casting to double is not completely terrible in this case.... it's the way it was done that is mindboggeling!
Filed Under: Compilers are weird sometimes!
Admin
Sounds like the kind of thing that would be defined in any sensible spec
Admin
I dunno, all I remember from my C++101 is that the GCC allows
int array[length];
even though thats against the standard... So pity me for not believing specs and standards anymore!Filed Under: sensible is a stretchy word!
Admin
Admin
Admin
IIRC GCC allows this:
Even in C, if you pass the right flags.
Admin
I was more whooshing over the fact that in precedence terms,
*
and/
will be outranked by the brackets, but if you remove the brackets they are arithmetically equivalent.I make no cases against whatever environment you have that makes them ints outside of the expression itself.
And then you have http://codepad.org/Yx1jPjlr which says everything you need to know about PHP/floating point accuracy.
Admin
Admin
Yes, I realise this. I was just finding it funny that to 11 reported decimal places they were the same :stuck_out_tongue:
Admin
Yes, I thought that was funny, as well. Of course an IEEE 8-byte double has more than 15 decimal digits of precision, so the loss of precision is hidden in the 15th and 16th sig figs, which aren't printed (166.66666666667 is only 14 sig figs).
Admin
And of course this is PHP so it's anyone's guess where it's actually broken.
Admin
it would be broken like that in any language that uses
double
... It isn't actually a PHP problem, although there might be some issues seeing it when the code is compiled to native x86 code that uses old-style pre-SSE floating point instructions with 80-bitlong double
types for non-stored intermediate values.Admin
One might be forgiven for pinning the blame for this stuff on situations that existed before the PHP creators were born.
Admin
My compsci fu is good but an in-depth working of floating point math is something I learned and forgot a long time ago. I mean, I work in PHP :stuck_out_tongue:
Admin
And, as everyone knows, 4,195,835 / 3,145,727 = 1.333739068902037589 no matter how you look at it.
Admin
Fine even without any flags here.
Admin
I've spent most of my career dealing with some sort of low-level shit, all the way down to disassembling the output of a C-to-8088 compiler and discovering that this code:
really caused its most aggressive optimisation mode to generate code to load 1 into a register, compare that register to 0, and jump back for another go if they weren't equal.
We replaced it, by the way, with another compiler that generated code that was about 25% smaller and whose 100% software floating point implementation ran at three times the speed of the old compiler's...
Admin
Yeah, see I never played with anything quite like that.
Admin
I am glad someone else is as paranoid as I am about bad input data. Some years ago I was working with historic data from printers, specifically counting up printed pages. There was a mystery overflow causing arithmetic exceptions, hard to find in the millions of records being processed. It turned out that one model of a particular manufacturer was failing to return counts, but storing the value as -1 and returning it as an unsigned int.
Admin
With stress on "sensible". (A medieval concept, btw.)
Not if a comma separates expressions. And/or a single equals character marks an assignment.
Plus, it's clear from the syntax what is a pointer and what is not. C# (and VB) does know pointers, but is ashamed to admit it. Refactoring a struct as a class (or - not as frequently - a class as a struct) can lead to a reasonable amount of pain in the back.
Admin
Once again, PHP is TRWTF:
Great, now I don't even know what I passed into the function. Splendid!
Admin
C pointers & PHP references are different things, but IMO the latter should never be used (unless you like magic unexpected shit happening just by calling functions/methods).
Admin
Yes, they're not deprecating passing references into functions, only the implicit situation of passing something in as a reference that wasn't supposed to be a reference.
Passing by reference in general is kind of voodoo and generally should be avoided. It's usually the hallmark of an older application or someone coming to PHP from another background (e.g. I know one library I tried to work with had all its methods as accepting references and acting on the references themselves so the methods would all return bools for success/failure)
Admin
I was commenting the clarity remark, not equating pointers and references.
And I did use references at times. Works fine if you're really careful about it.
Which I did. And would keep avoiding it, but the damned thing doesn't have method overriding!
Admin
Sure it does (assuming you're talking about child class methods)?
Admin
Honestly, I have to look into my code / docs. I had some reason for doing it.
Also, checked, you're right, overriding works, overloading doesn't.
Admin
This kind of overloading? It works a little differently than most languages but it's there.
Admin
Yes, you can do it using
__call
. Which can also be a pain, depending on what you're trying to do. Luckily, I didn't need it much / at all. Though I did abuse__call
for all kinds of other ugly shit I'd rather not think about (or admit to).Admin
I've just redefined my own personal hell to include eternity developing in PHP :smile:
Admin
That's cool, more money for me :monkey_face:
Admin
Don't forget func_get_args() is a thing for really shitty overloading.