β Addition with integers and floating point numbers
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
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 ffineNeat
So i just have the two inputs be
float
and it will implicity convert from an integer if it's input?yes
also change the return type to be float
Salman
REPL Result: Success
Console Output
Compile: 443.149ms | Execution: 25.086ms | React with β to remove this embed.
based on your usecase you might also want to use
double
or decimal
etcyeah 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.
Does this mod work ?
to run the code using bot you need to run the bot command in this way :
just remove the namespaces and classes and paste the function and it's call as I did above
Ah
Yay seem to work
you can spam the bot in #bot-spam though
Yeah i'll keep that in mind c:
Just making sure it works
we've a dedicated channel for the bot xD
:catderp:
But thanks! everything seems to work fine now c:
happy friday
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)
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 overloadsI think it works just fine, as a beginner they don't need to implement those complex interfaces
Wasn't my point
And it doesn't work at all
how ?
You don't need to use
INumber
here, you can use overload methods fine
I already explained thisFusedQyou
So if you were to add
1.5
to 1.5
, it returns 2
instead of 3
Quoted by
<@191282187016863744> from #β
Addition with integers and floating point numbers (click here)
React with β to remove this embed.
FusedQyou
REPL Result: Success
Result: int
Compile: 198.583ms | Execution: 15.108ms | React with β to remove this embed.
Salman
REPL Result: Success
Console Output
Compile: 460.299ms | Execution: 25.074ms | React with β to remove this embed.
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
Oh, the point is using
float
as a general method? That's not any better to be honest πtheir problem was that they want to accept both normal ints and floating points so the above function solves that
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 overloadingthen can't
double
or decimal
solve this problem with bigger numbers ?@Merineth Please use overloading instead of one method.
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 ?
Look up floating point arithmetic imprecision
I think that breaking the application is an exaggeration
also you've missed the context as well :
https://discord.com/channels/143867839282020352/1286653567150587916/1286656203211935766
Merineth
wont expect anything longer than 3 decimals long
Quoted by
<@923267140960796713> from #β
Addition with integers and floating point numbers (click here)
React with β to remove this embed.
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