• Post Reply Bookmark Topic Watch Topic
  • New Topic

Method to perform arithmetic on all kinds of numbers  RSS feed

 
Fred Victa
Ranch Hand
Posts: 206
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How would I write a method that would accept 2 numbers of any numeric data type and perform arithmetic on them? I would like to have my method to accept parameters such as int, double, float, and so on.
 
Guillermo Ishi
Ranch Hand
Posts: 789
C++ Linux Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem is the method would expect types to be passed to it. I think the best you could do would be to have overloaded functions for every combination of the two numeric data types. That would at least let you keep the same method name.
 
Ivan Jozsef Balazs
Rancher
Posts: 999
5
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Number and autoboxing?

 
Guillermo Ishi
Ranch Hand
Posts: 789
C++ Linux Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This would probably do it, but my first solution was the best P.S. You would probably want to add conditionals for b too.


 
Guillermo Ishi
Ranch Hand
Posts: 789
C++ Linux Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Fred Victa
Ranch Hand
Posts: 206
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ivan Jozsef Balazs wrote:Number and autoboxing?



Thank you. That is a good idea.
 
Guillermo Ishi
Ranch Hand
Posts: 789
C++ Linux Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a test from a prospective employer. If you get hired, bring us along ;)

 
Ivan Jozsef Balazs
Rancher
Posts: 999
5
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guillermo Ishi wrote:


I do not really like that. If you want to handle different types, then overloading the same method with different argument types is the good way. My suggestion was how to avoid this by creating a single entry point. Making a common entry point and then till perusing the type do not seem to make much sense for me.
I would also not put

instead of

 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree. With Guillermo's code I could write class com.coderanch.fake.Float which extends Number but does something completely differently when floatValue() is called (like call System.exit!).
 
Guillermo Ishi
Ranch Hand
Posts: 789
C++ Linux Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ivan Jozsef Balazs wrote:
I do not really like that. If you want to handle different types, then overloading the same method with different argument types is the good way. My suggestion was how to avoid this by creating a single entry point. Making a common entry point and then till perusing the type do not seem to make much sense for me.
I would also not put

instead of



Ivan, I don't like it either, but it does what he wants and yours doesn't. He might disagree, but he's really not sure what he wants.

The best solution is the overloading one I suggested first. I only continued with the silliness because you did Your code does not determine type, e.g. your example only returns a long. My code determines the type and returns the right type automatically.

Also, one significant thing is your code replacement above is not equivalent to what I wrote. instanceof will only work with objects, and he calls for numeric types. The purpose of my code you quoted is to determine the type of the primitive by finding what the appropriate wrapper for it is. ;) Pretty dang crafty. So my first solution is the best, my second is second best. Yours is half thought out, I think.



 
Guillermo Ishi
Ranch Hand
Posts: 789
C++ Linux Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


But I did just discover this is equivalent. Hooda thunk! I can't spend any more time on this.



 
Ivan Jozsef Balazs
Rancher
Posts: 999
5
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


It is a pity, because thus we never might learn why the unnecessary cast to Object.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!