Generic Numeric Class, Avoid Narrowing and Widening with Division
I was just wondering how I should handle division in a generic math class, should I never use integer devision?
Or is there a way to determine if there will be a narrowing conversion when using integer division and avoid this with a desicion structure?
The purpose of this class is to provide some reusable statistics functions etc.
I am not a math guru, so any info or help would be appreciated (Im not exactly sure of the information i need to google for and everything I find is not really helpful to my situation).
Thanx in advance...
Re: Generic Numeric Class, Avoid Narrowing and Widening with Division
Your question doesn't make a lot of sense to me. An Integer divided by an Integer in VB.NET returns a Double every time. If you specifically want an Integer result then you must use the integer division operator (\), not the division operator (/).
Re: Generic Numeric Class, Avoid Narrowing and Widening with Division
Yeah sorry, I could have been a bit more descriptive.
The question I wanted to know the awnser for is irrelevant, I have a math problem not really a VB problem. Anyway I want a generic class so I dont have to worry about conversions or anything when I use the class, for example, if I had a whole lot of integers I would expect my class to use decimal for all calculation and internal/private variables and the generics would only be applied to the return value, so my class would convert all input numbers to decimal and (t) would define the return values.
E.g.
Code:
Class NumericClass(of t)
private decNumber as decimal
sub new(byval Number as t)
decNumber = directCast(Number, t)
end sub
readonly property Value
get
return directCast(decNumber, t)
end get
end property
end class