C
C#ā€¢14mo ago
Dinny

Overloading "+" operator

I am adding fractions together and need to overload the plus operator, but I keep running into an error while I am building the code. Before even adding them together, he wants us to find the GCD (greatest common denom) to simplfy the fractions down before adding them together, which is where my error arises.
private void Simplify()
{
if (numerator == 0)
{

denominator = 1;
return;

}

//Finding GCD for num and denom.
uint GCD = GetGCD((uint)Math.Abs(numerator), denominator);
numerator /= (int)GCD;
denominator /= GCD;
}
private void Simplify()
{
if (numerator == 0)
{

denominator = 1;
return;

}

//Finding GCD for num and denom.
uint GCD = GetGCD((uint)Math.Abs(numerator), denominator);
numerator /= (int)GCD;
denominator /= GCD;
}
I get an error under "denominator" after the "Math.Abs(numerator)" and when dividing the denominator by the GCD
18 Replies
Ahmed
Ahmedā€¢14mo ago
make a loop and go through numbers from 1 up until the denominator in the loop, add all the divisible ones into a list and then compare via the list @Dinny wanna know what's cool i asked chat gpt It seems like you're trying to simplify a fraction by finding the greatest common divisor (GCD) of the numerator and denominator. The issue you're encountering could be related to the data types you are using. Ensure that numerator and denominator are of appropriate types, and the GCD function returns the expected result. Here's a potential solution:
private void Simplify()
{
if (numerator == 0)
{
denominator = 1;
return;
}

// Finding GCD for num and denom.
uint GCD = GetGCD((uint)Math.Abs(numerator), (uint)denominator); // Ensure both values are uint.

numerator /= (int)GCD;
denominator /= (int)GCD;
}

// Define a method to find the GCD (you might have this elsewhere in your code).
private uint GetGCD(uint a, uint b)
{
while (b != 0)
{
uint temp = b;
b = a % b;
a = temp;
}
return a;
}
private void Simplify()
{
if (numerator == 0)
{
denominator = 1;
return;
}

// Finding GCD for num and denom.
uint GCD = GetGCD((uint)Math.Abs(numerator), (uint)denominator); // Ensure both values are uint.

numerator /= (int)GCD;
denominator /= (int)GCD;
}

// Define a method to find the GCD (you might have this elsewhere in your code).
private uint GetGCD(uint a, uint b)
{
while (b != 0)
{
uint temp = b;
b = a % b;
a = temp;
}
return a;
}
In this code, I explicitly cast denominator to uint when calling GetGCD to ensure that both numerator and denominator have the same data type for finding the GCD. Make sure you have a GCD calculation method, as shown in the code. If you don't have one, you can use the System.Numerics.BigInteger.GreatestCommonDivisor method to find the GCD. If you continue to encounter errors, please provide more information about the error message you're seeing or the data types of numerator and denominator so that I can provide more specific assistance.
Dinny
DinnyOPā€¢14mo ago
oh wow, thank you so much
Ahmed
Ahmedā€¢14mo ago
hope chatgpt helps šŸ’€
Dinny
DinnyOPā€¢14mo ago
and i have heard of usinf chat gpt nut i dont want things necessarily done for me i wanna understand why and how ok lemme look at what u put
Ahmed
Ahmedā€¢14mo ago
it explained
Dinny
DinnyOPā€¢14mo ago
yeah i see ur explanation i just hadn't read it yet i kinda thanke du first lol
Ahmed
Ahmedā€¢14mo ago
Hmmmmmmmmmmmmm
Dinny
DinnyOPā€¢14mo ago
okay real quick, the area under the "finding gcd for num and denom," my prof didn't put the (uint)denominator with his code and everything works properly. Ill send a picture
Dinny
DinnyOPā€¢14mo ago
No description
Ahmed
Ahmedā€¢14mo ago
so did it work out?
Dinny
DinnyOPā€¢14mo ago
whenever i try his code, like exactly, i get an error, but when he does it, it works fine lemme see what the squiggal says hold on it says i can't convert uint to int
Sweerpotato
Sweerpotatoā€¢14mo ago
Try unchecked((int)yourNumber) but be careful it will convert yourNumber if it overflows Int32 to negative
Dinny
DinnyOPā€¢14mo ago
wait hang on i have an idea for this, i have a method for this in my code. it is this
private static uint GetGCD(uint x, uint y)
{
if (x < y) { return GetGCD(y, x); }
if (x % y == 0) { return y; }
return GetGCD(x, x % y);
}
private static uint GetGCD(uint x, uint y)
{
if (x < y) { return GetGCD(y, x); }
if (x % y == 0) { return y; }
return GetGCD(x, x % y);
}
he hasn't taught us recursion so, so we had to write this specifically from him
Sweerpotato
Sweerpotatoā€¢14mo ago
What was the problem again? Type conversion?
Dinny
DinnyOPā€¢14mo ago
yes it keep ssaying i can't convert uint to int implicitly
Sweerpotato
Sweerpotatoā€¢14mo ago
Did you try what I said earlier This one unchecked((int)yourNumber) will be negative if yourNumber is greater than Int32.MaxValue
Dinny
DinnyOPā€¢14mo ago
oh yeah i caqn try that now but where would i put that ? i've never heard of that until now so i am unsure
Sweerpotato
Sweerpotatoā€¢14mo ago
Where it says you can't convert uint is much larger than int, and uint promises that it will be a positive integer In beginner's terms int can go both ways It can be positive or negative
Want results from more Discord servers?
Add your server