Results 1 to 8 of 8

Thread: real division in an integer world

  1. #1
    Guest
    I declare i as integer (forgive me, old Fortran habits die hard) and perform the following calculations:

    i=(Last_Date - First_Date)/NumSets


    where xxx_date is a date (validated) and NumSets is integer.

    Apparently, i is being rounded to the nearest integer.


    ie: i=47/26 => 2
    i=33/26 => 1

    What gives?

    DerFarm

  2. #2
    Guest
    You are asking why an integer is only holding an integer value? When you declare a variable (i) as integer, can't it only hold integer values? That would mean to get the results you want, you are probably going to have to use a single, or double. At least that's what I think.

  3. #3
    Guest
    Not at all. Effectively, integer means with no trailing decimals.

    In other languages, integer = xnum/ynum with a real result would be interpreted as

    integer=Int(xnum/ynum)

    Here it is apparently integer=round(xnum/ynum,0)

    Different kettle of fish altogether.


    DerFarm

  4. #4
    Frenzied Member
    Join Date
    Jan 2000
    Location
    Bellevue, WA, USA
    Posts
    1,357

    DerFarm is right...an integer is an INTEGER!

    Try this:
    Code:
    Dim sngAvgSetsPerDay As Single ' Forgive me...VB habits die hard :)
    
    sngAvgSetsPerDay =(Last_Date - First_Date)/NumSets
    An Integer (or a Long) is a whole number, whereas a Single (or Double) can have a decimal number value.

    Hope that helps!
    ~seaweed

  5. #5
    Frenzied Member
    Join Date
    Jan 2000
    Location
    Bellevue, WA, USA
    Posts
    1,357

    I misunderstood your question

    I don't think your question was clear initially. I thought you wanted to know why the result was rounded to the nearest integer.

    True, this is the way that VB handles it - rounding instead of truncating. Don't know why.
    ~seaweed

  6. #6
    Lively Member
    Join Date
    Aug 2000
    Location
    quebec
    Posts
    81

    Cool

    In your code you use the / operator
    Code:
    i=(Last_Date - First_Date)/NumSets
    whereas you should use the \ operator instead. This op. divides

    ex.:
    Code:
    Dim intVal As Integer
    
    intVal = 11 \ 4   ' Returns 2.
    intVal = 9 \ 3   ' Returns 3. 
    intVal = 10 \ 6   ' Returns 2.
    intVal = 10 / 6 ' Returns 1.
    try it, you'll like it
    C/C++,Delphi,VB6,Java,PB (blech!),ASP,JSP,SQL...bla bla bla and bla
    I love deadlines. I like the whooshing sound they make as they fly by.
    —Douglas Adams

  7. #7
    Frenzied Member
    Join Date
    Jan 2000
    Location
    Bellevue, WA, USA
    Posts
    1,357

    Rounding is more accurate

    Actually, rounding is more accurate, isn't it?

    If you add the results of rounded numbers, you will be closer to the actual result than if you add the result of truncated numbers.

    When adding numbers less or equal to .5 away from the whole part, the methods work identically. The further away you move from .5 toward the next whole number, the less acurate truncating is.

    i.e:
    Code:
    ' Actual numbers:
    ' 1.1 + 1.3 + 1.5 + 1.7 + 1.9 = 7.5
    '
    ' Truncated numbers:
    ' 1 + 1 + 1 + 1 + 1 = 5  (difference = 2.5)
    ' 
    ' Rounded numbers:
    ' 1 + 1 + 1 + 2 + 2 = 7  (difference = .5)
    '
    ' *****   ANOTHER EXAMPLE   *****
    '
    ' Using the same number over and over:
    ' 1.6 * 10 = 16
    '
    ' Truncated version:
    ' 1 * 10 = 10  (difference = 6)
    ' 
    ' Rounded version:
    ' 2 * 10 = 20  (difference = 4)
    This leads me to believe that VB is a little more advanced than languages like C when it comes to converting decimals to whole numbers!

    [Edited by seaweed on 10-03-2000 at 05:41 PM]
    ~seaweed

  8. #8
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    On average, truncating will produce an error of 0.5 per number. Rounding gives an error of 0. This is probably why it was chosen for VB.

    But bear in mind that C is designed to be compact and excess-free, in which case it is best to discard the extra values. "If the programmer wants it rounded, then they can do it themselves" is basically its attitude!
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

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