Results 1 to 3 of 3

Thread: Generic Numeric Class, Avoid Narrowing and Widening with Division

Hybrid View

  1. #1

    Thread Starter
    Hyperactive Member cptHotkeys's Avatar
    Join Date
    Apr 2007
    Location
    New Zealand
    Posts
    294

    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...

    Signatures suck

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    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 (/).
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Hyperactive Member cptHotkeys's Avatar
    Join Date
    Apr 2007
    Location
    New Zealand
    Posts
    294

    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

    Signatures suck

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width