12 Replies
integer math
Dividing two integers truncates the fractional part
Cast the integers to double
Doesn't "Convert.ToDouble()" do that?
That parses a double from a string
casting is very different
Um...then it wouldn't compile
Oh, good point
I don't use that Convert class too often. I suppose it's an overload then
But the convert is on the result of the integer division where truncation has already happened
I haven't been taught casting yet. But from what I've read online it's putting the desired type in parenthesis before the variable I want casted, correct? Is there anything else I ought to know about casting?
You need to do floating point division
Like 1 / (double)ints[i]
Ok, so what I've gathered from this is that the Convert class only deals in converting string to ints/doubles etc while (casting) converts types into other types?
No
But you don't need to use Convert here
You need to convert int to double before you do the division, not after
You could use Convert to do that technically but casting is more straightforward
The casting works fine. I don't understand why however. Seems like I was doing the same thing with the Convert class. I'll be googling on the differences in a second.
It seems to be a quite complex topic, one I won't be able to wrap my head around in this short moment. But I'd like to thank you for your help.
Convert takes as input the result of the division (in your example) AFTER the integer division happened and already threw away the decimal part
When you divide two integers the result is an integer, and integers have no way to represent fractions