C
C#2y ago
populus

double array yields only zeroes

static void RunExerciseSixteen()
{
int[] ints = new int[10];
Random rnd = new Random();
double[] dbls = new double[ints.Length];

for (int i = 0; i < ints.Length; i++)
{
ints[i] = rnd.Next(1, 500);
}

for (int i = 0; i < dbls.Length; i++)
{
Convert.ToDouble(dbls[i] = 1 / ints[i]);
}

foreach (int i in ints)
{
Console.WriteLine("Intsarray: " + i);
}
foreach (double i in dbls)
{
Console.WriteLine("Dblsarray: " + i); // Yields '0' repeatedly, why?
}
}
static void RunExerciseSixteen()
{
int[] ints = new int[10];
Random rnd = new Random();
double[] dbls = new double[ints.Length];

for (int i = 0; i < ints.Length; i++)
{
ints[i] = rnd.Next(1, 500);
}

for (int i = 0; i < dbls.Length; i++)
{
Convert.ToDouble(dbls[i] = 1 / ints[i]);
}

foreach (int i in ints)
{
Console.WriteLine("Intsarray: " + i);
}
foreach (double i in dbls)
{
Console.WriteLine("Dblsarray: " + i); // Yields '0' repeatedly, why?
}
}
What am I getting wrong here?
12 Replies
mtreit
mtreit2y ago
integer math Dividing two integers truncates the fractional part Cast the integers to double
populus
populus2y ago
Doesn't "Convert.ToDouble()" do that?
Auger
Auger2y ago
That parses a double from a string casting is very different
mtreit
mtreit2y ago
Um...then it wouldn't compile
Auger
Auger2y ago
Oh, good point I don't use that Convert class too often. I suppose it's an overload then
mtreit
mtreit2y ago
But the convert is on the result of the integer division where truncation has already happened
populus
populus2y ago
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?
mtreit
mtreit2y ago
You need to do floating point division Like 1 / (double)ints[i]
populus
populus2y ago
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?
mtreit
mtreit2y ago
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
populus
populus2y ago
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.
mtreit
mtreit2y ago
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
Want results from more Discord servers?
Add your server
More Posts
What does 'Intellisense' refer to?Is intellisense solely responsible for all feedback that you receive in the IDE? Or do other parts oCloud Blazor web app - Mitigate malicious crawlers from wasting container resourcesI have a cloud-related question. If I'm hosting a Blazor server web app and I want to prevent maliciSystem.AccessViolationExceptionI'm trying to create a simple multiplayer game using SignalR and Raylib, I wrote a really simple demHow can i achieve something like this```cs public class Test { public string Name{get;set;} } //And i want to do Test.Add(new string SShould I use && and || operators at the end of a line or beginning? [Answered]if (a || b || c) { } vs. if (a || b || c) { } Which is the more accepted standard? I jusConfused about interface default implementations [Answered]Trying out default implementations for methods in the interfaces for the first time. In the interfacAnyone have experience or know of an open source barcode reading library for images?There's of course a bunch of paid options, but I'm looking for an open source alternative if one exiJquery.load() with MVC how to load view when Exception appearJquery.load() with MVC how to load view when Exception appearTested it a lot of times but still giving unexpected errorusing System; class Program { public static void Main (string[] args) { Random numGen = nDockerfile cannot find dependent project in the parent directory``` . +---src | +---MarketData.Api | +---MarketData.Messages | +---MarketData.Subscriber