βœ… Addition with integers and floating point numbers

namespace Calculator.Model
{
// The class for Addition "+".
public class Addition
{
public static int Add(int number1, int number2)
{

}
}
}
namespace Calculator.Model
{
// The class for Addition "+".
public class Addition
{
public static int Add(int number1, int number2)
{

}
}
}
If i were to create this method, and someone would use a floating point number instead of an int. Would this method break? And if so, is there a way around this?
31 Replies
Salman
Salmanβ€’3mo ago
I think you could use float in that case , it would be able to accept both int and float inputs right now it'd be small -> large type conversion which will cause error but if you change tha params type to float and you get an int as a parameter then it'll be large -> small type convesion which is ffine
Merineth πŸ‡ΈπŸ‡ͺ
Neat So i just have the two inputs be float and it will implicity convert from an integer if it's input?
Salman
Salmanβ€’3mo ago
yes also change the return type to be float
MODiX
MODiXβ€’3mo ago
Salman
REPL Result: Success
public static float Add(float number1, float number2)
{
return number1 + number2;
}
Console.Write(Add(1,2));
public static float Add(float number1, float number2)
{
return number1 + number2;
}
Console.Write(Add(1,2));
Console Output
3
3
Compile: 443.149ms | Execution: 25.086ms | React with ❌ to remove this embed.
Salman
Salmanβ€’3mo ago
based on your usecase you might also want to use double or decimal etc
Merineth πŸ‡ΈπŸ‡ͺ
yeah i looked into that but float will be fine wont expect anything longer than 3 decimals long Also do you know how i can use the bot to try out some code? For example.
namespace Calculator.Model
{
internal class Modulo
{
public static float Mod(float number1, float number2)
{
return number1 % number2;
}
}
}
namespace Calculator.Model
{
internal class Modulo
{
public static float Mod(float number1, float number2)
{
return number1 % number2;
}
}
}
Does this mod work ?
Salman
Salmanβ€’3mo ago
to run the code using bot you need to run the bot command in this way :
!eval //your c# code here
!eval //your c# code here
just remove the namespaces and classes and paste the function and it's call as I did above
Merineth πŸ‡ΈπŸ‡ͺ
Ah Yay seem to work
Salman
Salmanβ€’3mo ago
you can spam the bot in #bot-spam though
Merineth πŸ‡ΈπŸ‡ͺ
Yeah i'll keep that in mind c: Just making sure it works
Salman
Salmanβ€’3mo ago
we've a dedicated channel for the bot xD
Merineth πŸ‡ΈπŸ‡ͺ
:catderp: But thanks! everything seems to work fine now c: happy friday
FusedQyou
FusedQyouβ€’3mo ago
A floating point is casted to an integer, removing the decimals that exist If you want this to work for floats, either make one for float with voerloading (like you did), or use INumber INumber is a rather new interface that combines common operations, including additions So instead of making multiple methods that practically do the same, you can put them under a single INumber So this is wrong. It doesn't accept floats, it just implicitly casts a float to an integer which gets rid of the decimals So if you were to add 1.5 to 1.5, it returns 2 instead of 3 Kind of hard to explain INumber, but here is a method that uses it (INumber implements IAdditionOperators)
public static T AddAll<T, TOther>(T t, List<TOther> c) where T : IAdditionOperators<T, TOther, T>
{
foreach (var item in c)
{
t = t + item;
}

return t;
}
public static T AddAll<T, TOther>(T t, List<TOther> c) where T : IAdditionOperators<T, TOther, T>
{
foreach (var item in c)
{
t = t + item;
}

return t;
}
But if you are a beginner, forget this. Just write overloads If you make a method of the same name, but different parameters, we call those overloads
Salman
Salmanβ€’3mo ago
I think it works just fine, as a beginner they don't need to implement those complex interfaces
FusedQyou
FusedQyouβ€’3mo ago
Wasn't my point And it doesn't work at all
Salman
Salmanβ€’3mo ago
how ?
FusedQyou
FusedQyouβ€’3mo ago
You don't need to use INumber here, you can use overload methods fine I already explained this
MODiX
MODiXβ€’3mo ago
FusedQyou
So if you were to add 1.5 to 1.5, it returns 2 instead of 3
React with ❌ to remove this embed.
MODiX
MODiXβ€’3mo ago
FusedQyou
REPL Result: Success
(int)1.5 + (int)1.5
(int)1.5 + (int)1.5
Result: int
2
2
Compile: 198.583ms | Execution: 15.108ms | React with ❌ to remove this embed.
MODiX
MODiXβ€’3mo ago
Salman
REPL Result: Success
public static float Add(float number1, float number2)
{
return number1 + number2;
}
Console.Write(Add(1.5f,1.5f));
public static float Add(float number1, float number2)
{
return number1 + number2;
}
Console.Write(Add(1.5f,1.5f));
Console Output
3
3
Compile: 460.299ms | Execution: 25.074ms | React with ❌ to remove this embed.
Salman
Salmanβ€’3mo ago
Works just fine it's returning 3 as it should, the function would work fine with both normal integers and floats without making any overloads or such
FusedQyou
FusedQyouβ€’3mo ago
Oh, the point is using float as a general method? That's not any better to be honest πŸ˜„
Salman
Salmanβ€’3mo ago
their problem was that they want to accept both normal ints and floating points so the above function solves that
FusedQyou
FusedQyouβ€’3mo ago
This is bad advice, don't do this to beginners This works fine with small numbers, but the whole point of the float type is to store a base and an exponent, and this will cause inaccuracies as with any type that contains decimals Your integer type is precise, and will never have this issue And this is why you don't give advice like this, especially since there is a better solution right in front of you with overloading
Salman
Salmanβ€’3mo ago
then can't double or decimal solve this problem with bigger numbers ?
FusedQyou
FusedQyouβ€’3mo ago
@Merineth Please use overloading instead of one method.
namespace Calculator.Model
{
// The class for Addition "+".
public class Addition
{
public static int Add(int number1, int number2)
{
return number1 + number2;
}

public static float Add(float number1, float number2)
{
return number1 + number2;
}
}
}
namespace Calculator.Model
{
// The class for Addition "+".
public class Addition
{
public static int Add(int number1, int number2)
{
return number1 + number2;
}

public static float Add(float number1, float number2)
{
return number1 + number2;
}
}
}
Salman
Salmanβ€’3mo ago
can you provide an example of how the above function with even decimal , double or float will not be able to handle the bigger numbers and will break the application ?
FusedQyou
FusedQyouβ€’3mo ago
Look up floating point arithmetic imprecision
Salman
Salmanβ€’3mo ago
I think that breaking the application is an exaggeration also you've missed the context as well : https://discord.com/channels/143867839282020352/1286653567150587916/1286656203211935766
MODiX
MODiXβ€’3mo ago
Merineth
wont expect anything longer than 3 decimals long
React with ❌ to remove this embed.
FusedQyou
FusedQyouβ€’3mo ago
I don't see a point in discussing this if you refuse to take the suggestions from the other party My point is clear, and I ask you don't give bad advice to beginners like this The correct way is either overloading or the usage of INumber(base) Even if this user were to expect a maximum size in their numbers, you still don't advice this They don't know about explicit/implicit conversions that happen here, and if they take advices like this and make an application with it they end up with a poorly build application that does not work. And if this worked for you, great, but let's not give this bad advice to beginners and give them the correct way as soon as possible
Want results from more Discord servers?
Add your server