Consulting

Results 1 to 17 of 17

Thread: Extracting the Fractional Digits of a Number

  1. #1

    Extracting the Fractional Digits of a Number

    It must be the heat (it?s currently 105 degrees here today), but I just discovered that I don?t know how to extract the fractional digits from a number like 100.544. Consider the following code:
    [vba]Sub DigitsTest
    Dim X As Double, Y As Double, Z As Double
    X = 100.544 ?This is a constant and not the result of another calculation
    Y = Application.RoundDown(X, 0) ?This puts 100 into ?Y?
    Z = X ? Y ?This is 100.544 ? 100
    Msgbox Z ?This displays 0.543999999999997
    End Sub[/vba] Why doesn?t ?Z? contain 0.544 ??
    Instead of Y = Application.RoundDown(X, 0) I used Y = Cint(X) , but that rounds up so I get 101 instead of 100.

  2. #2
    VBAX Guru
    Joined
    Mar 2005
    Posts
    3,296
    Location
    Wierd isn't it. It just goes to show that VBA Int, CInt and Roundown are not accurate, this is though.
    [vba]Dim X As Single, Y As Single, Z As Double
    X = 100.544
    Y = Int(X) * 10000
    X = X * 10000
    MsgBox (X - Y) / 10000[/vba]
    So is this -

    [VBA] Dim X As Double, Y As Double, Z As Double
    X = 100.544
    Y = Int(X) * 10000000
    X = X * 10000000
    MsgBox (X - Y) / 10000000[/VBA]

  3. #3

    here's a thought too

    I was checking out the web, cause I was intrigued by the question. I found a web site at http://www.cpearson.com/excel/rounding.htm that explained the limitations of the floating point decimal system. I mentions that fractional numbers are not stored exactly accurate, but whole numbers are! So when we multiply fractions to make them whole numbers, excel can accuratly store them using the floating point decimal system. Hopefully this helps and I didn't miss the point or anything. Air out any thoughts, curious about them.

  4. #4
    VBAX Expert
    Joined
    Jul 2004
    Location
    Wilmington, DE
    Posts
    600
    Location
    It's all just "Fun with FLoating Point Arithmetic"

    BTW, this worked just fine in the Immediate window:

    [VBA]
    ?Val("." & Split(100.544 - 100, ".")(1))
    [/VBA]
    Regards,

    Patrick

    I wept for myself because I had no PivotTable.

    Then I met a man who had no AutoFilter.

    Microsoft MVP for Excel, 2007 & 2008

  5. #5
    Quote Originally Posted by matthewspatrick
    ?Val("." & Split(100.544 - 100, ".")(1))
    OK, I'll bite ... what's "?Val("??

    Hey, bigdoggit, that's the first time I've heard that. But one wonders, if 0.544 is stored as 0.543999 (etc), then why doesn't mulitplying it by a thousand give you 543.999 (etc)?? Strange ... very strange.

    Thanx for the replies guys!
    Sid

  6. #6
    Administrator
    2nd VP-Knowledge Base
    VBAX Master malik641's Avatar
    Joined
    Jul 2005
    Location
    Florida baby!
    Posts
    1,533
    Location
    Quote Originally Posted by matthewspatrick
    BTW, this worked just fine in the Immediate window:

    [vba]
    ?Val("." & Split(100.544 - 100, ".")(1))
    [/vba]
    not for me
    Quote Originally Posted by Immediate Window
    ?Val("." & Split(100.544 - 100, ".")(1))
    0.543999999999997
    I've been doing some small procedures in AutoCAD and I have a feeling I'm going to run into a problem if some numbers are not going to be accurate like this...so I'll have to code carefully.

    This worked for me from the Immediate window
    [vba]?Right("100.544",Len("100.544") - Application.WorksheetFunction.Find(".","100.544") + 1)[/vba]

    Quote Originally Posted by Immediate Window
    ?Right("100.544",Len("100.544") - Application.WorksheetFunction.Find(".","100.544") + 1)
    .544




    New to the forum? Check out our Introductions section to get to know some of the members here. Feel free to tell us a little about yourself as well.

  7. #7
    VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    In Cad--From experience and conversations with Tommy I have found you need to calculate pi to at least 14 digits to maintain accuracy over many calculations Joseph.
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  8. #8
    VBAX Expert
    Joined
    Jul 2004
    Location
    Wilmington, DE
    Posts
    600
    Location
    Sid,

    Quote Originally Posted by Cyberdude
    OK, I'll bite ... what's "?Val("??
    Taking the expression inside out...
    1. Split() returns a zero-based array, breaking the first argument by the delimiter specified in the second argument. Using Split()(1) is shorthand for grabbing just the 2nd (remember, zero-based!) element of that array. In this case, the return value is the string "544"
    2. Val() takes its argument and tries to convert it into a number. Effectively, my expression becomes Val(".544"), which should be 0.544.
    3. ? is just the old BASIC shorthand for Print



    Quote Originally Posted by Cyberdude
    Hey, bigdoggit, that's the first time I've heard that. But one wonders, if 0.544 is stored as 0.543999 (etc), then why doesn't mulitplying it by a thousand give you 543.999 (etc)?? Strange ... very strange.
    Remember, it is not necessarily the case that 0.544 was stored as 0.543999.... Rather, the result of subtracting 100 from 100.544 was represented as....
    Regards,

    Patrick

    I wept for myself because I had no PivotTable.

    Then I met a man who had no AutoFilter.

    Microsoft MVP for Excel, 2007 & 2008

  9. #9
    VBAX Expert
    Joined
    Jul 2004
    Location
    Wilmington, DE
    Posts
    600
    Location
    Joseph,

    How odd that our Immediate Windows disagree! Shouldn't make a difference, but I ran my test from Excel VBA. How about you?

    Quote Originally Posted by malik641
    not for me
    I've been doing some small procedures in AutoCAD and I have a feeling I'm going to run into a problem if some numbers are not going to be accurate like this...so I'll have to code carefully.
    Indeed. For some floating point comparisons, you should not use the = operator. Instead, you should test to see whether the absolute difference between two numbers is vanishingly small.
    Regards,

    Patrick

    I wept for myself because I had no PivotTable.

    Then I met a man who had no AutoFilter.

    Microsoft MVP for Excel, 2007 & 2008

  10. #10
    Administrator
    2nd VP-Knowledge Base VBAX Master malik641's Avatar
    Joined
    Jul 2005
    Location
    Florida baby!
    Posts
    1,533
    Location
    Quote Originally Posted by lucas
    In Cad--From experience and conversations with Tommy I have found you need to calculate pi to at least 14 digits to maintain accuracy over many calculations Joseph.
    14 huh? Wow. Is there a constant built-in to VBA for that? I know that Excel VBA does (Application.WorksheetFunction.Pi ....which is stored at 14 digits after the decimal point)

    Quote Originally Posted by mathewspatrick
    How odd that our Immediate Windows disagree! Shouldn't make a difference, but I ran my test from Excel VBA. How about you?
    I know! It's very strange. I'm running Excel 2003, how about you?

    Quote Originally Posted by mathewspatrick
    Indeed. For some floating point comparisons, you should not use the = operator. Instead, you should test to see whether the absolute difference between two numbers is vanishingly small.
    Nice idea, I'll look into that. Thankfully my line of work with CAD isn't an exact science....but if I get a job with cad in the field of Mechanical Engineering where it WILL matter, I'd like to know.




    New to the forum? Check out our Introductions section to get to know some of the members here. Feel free to tell us a little about yourself as well.

  11. #11
    VBAX Expert
    Joined
    Jul 2004
    Location
    Wilmington, DE
    Posts
    600
    Location
    Excel XP
    Regards,

    Patrick

    I wept for myself because I had no PivotTable.

    Then I met a man who had no AutoFilter.

    Microsoft MVP for Excel, 2007 & 2008

  12. #12
    VBAX Expert
    Joined
    Jul 2004
    Location
    Wilmington, DE
    Posts
    600
    Location
    Joseph,

    Quote Originally Posted by malik641
    14 huh? Wow. Is there a constant built-in to VBA for that? I know that Excel VBA does (Application.WorksheetFunction.Pi ....which is stored at 14 digits after the decimal point)
    A Double has maximum precision of 15 significant digits, so what the PI() function returns is the maximum precision possible in Excel or VBA.

    You could always just define a global constant in your project for pi...
    Regards,

    Patrick

    I wept for myself because I had no PivotTable.

    Then I met a man who had no AutoFilter.

    Microsoft MVP for Excel, 2007 & 2008

  13. #13
    VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    Const pi = 3.14159265358979
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  14. #14
    Administrator
    2nd VP-Knowledge Base VBAX Master malik641's Avatar
    Joined
    Jul 2005
    Location
    Florida baby!
    Posts
    1,533
    Location
    Excel XP....same as 2003 right?

    A Double has maximum precision of 15 significant digits, so what the PI() function returns is the maximum precision possible in Excel or VBA.

    You could always just define a global constant in your project for pi...
    I see and good idea... It's amazing how PI has yet to be found an end.




    New to the forum? Check out our Introductions section to get to know some of the members here. Feel free to tell us a little about yourself as well.

  15. #15
    VBAX Expert
    Joined
    Jul 2004
    Location
    Wilmington, DE
    Posts
    600
    Location
    No, Office XP = Office 2002.

    Regards,

    Patrick

    I wept for myself because I had no PivotTable.

    Then I met a man who had no AutoFilter.

    Microsoft MVP for Excel, 2007 & 2008

  16. #16
    Administrator
    2nd VP-Knowledge Base VBAX Master malik641's Avatar
    Joined
    Jul 2005
    Location
    Florida baby!
    Posts
    1,533
    Location
    Quote Originally Posted by matthewspatrick
    No, Office XP = Office 2002.

    Thanks.....still wondering why you had a different value than me eh...oh well. I'll worry about it later, it's getting late over here.




    New to the forum? Check out our Introductions section to get to know some of the members here. Feel free to tell us a little about yourself as well.

  17. #17
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by matthewspatrick
    Excel XP
    I have XP and I get 0.543999999999997, but I agree with you on testing for > than a small number, which the different results we all get is amply validated.

Posting Permissions

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