dcsimg
Page 1 of 2 12 LastLast
Results 1 to 40 of 52
  1. #1

    Thread Starter
    Member
    Join Date
    Jun 2017
    Posts
    50

    Resolved [RESOLVED] How to fast split Number integet part and decimal part ?

    Code:
    Private Sub Form_Load()
    
    Const s = 12345.6789
    
    
    Dim w As Long
    Dim k As Long
    Dim p As Long
    
    Dim integet_part   As Long
    Dim decimal_part   As Double
    Dim decimal_length As Long
    
    
    'methode_01
    integet_part = CLng(Split(s, ".")(0))
    decimal_part = CLng(Split(s, ".")(1))
    '================
    
    'methode_02
    str_001$ = s
    p = InStr(1, str_001, ".")
    integet_part = CLng(Left$(str_001, p - 1))
    integet_part = CLng(Mid$(str_001, p + 1))
    
    
    
    ' Prohibit use string and string function to get integet part and decimal part
    ' because it is too slow use string and string function
    '
    '
    
    'methode_03
    Const test_speed_loops = 10 ^ 6
    
    TT = Timer
    
    For k = 1 To test_speed_loops  'still too slow, because CDec() too slow
    
    integet_part = Int(s)
    decimal_part = CDec(s) - Int(s)
    
    For w = 1 To 10 ^ 9
       decimal_part = decimal_part * 10
       If (decimal_part - Int(decimal_part)) = 0 Then
           'MsgBox w, , "decimal lenght"
           decimal_length = w
           Exit For
       End If
    Next
    
    Next
    
    MsgBox Timer - TT
    
    End
    
    End Sub
    Last edited by quickbbbb; Jul 5th, 2018 at 08:13 AM.

  2. #2
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    4,370

    Re: How to fast split Number integet part and decimal part ?

    My first thought would be to do something like this:

    Code:
    
        Dim integer_part   As Double
        Dim decimal_part   As Double
        '
        Dim d As Double
        d = 12345.6789
    
    
        integer_part = Fix(d)
        decimal_part = d - integer_part
    
        Debug.Print integer_part
        Debug.Print decimal_part
    
    And, a few comments. First, if we declare integer_part as a Double, we solve a few different problems. For one, it'll be faster because assigning to a Double won't require any typecasting. Second, if our original number is larger than a Long, we won't overflow it.

    And then also, if we stick with math (rather than using strings and the Instr function), things will clearly be much faster, as everything will be done in the IEEE floating-point-processor, which is much faster than dealing with Strings.

    Also, having the input argument as a regular variable, rather than a Const, will also probably be faster. When passing constants, VB6 creates a temporary variable to protect the constant, where that's not going to happen with a regular variable.

    However, I'll let you do your speed testing.

    Good Luck,
    Elroy
    Any software I post in these forums written by me is provided “AS IS” without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. Please understand that I’ve been programming since the mid-1970s and still have some of that code. My contemporary VB6 project is approaching 1,000 modules. In addition, I have a “VB6 random code folder” that is overflowing. I’ve been at this long enough to truly not know with absolute certainty from whence every single line of my code has come, with much of it coming from programmers under my employ who signed intellectual property transfers. I have not deliberately attempted to remove any licenses and/or attributions from any software. If someone finds that I have inadvertently done so, I sincerely apologize, and, upon notice and reasonable proof, will re-attach those licenses and/or attributions. To all, peace and happiness.

  3. #3
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    6,453

    Re: How to fast split Number integet part and decimal part ?

    @Elroy...that's interesting (Sorry to butt in).

    When I ran your snippet, my decimal_part shown in debug.print was: 0.678900000000795

    Why would that be? Where did those 8 extra zeroes followed by 795 come from?

  4. #4
    Frenzied Member ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    1,081

    Re: How to fast split Number integet part and decimal part ?

    Hi @all,

    my 2 cents..

    Code:
    Private Sub Command10_Click()
    Dim Trennzeichen As String
    Trennzeichen = "."
    
    Dim Parts() As String
    Dim Satz As String
    
    
    Satz = "12345.6789"
    
    Parts() = Split(Satz, Trennzeichen)
    
    MsgBox Parts(0)
    MsgBox Parts(1)
    
    End Sub
    regards
    Chris
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  5. #5

    Thread Starter
    Member
    Join Date
    Jun 2017
    Posts
    50

    Re: How to fast split Number integet part and decimal part ?

    Quote Originally Posted by SamOscarBrown View Post
    @Elroy...that's interesting (Sorry to butt in).

    When I ran your snippet, my decimal_part shown in debug.print was: 0.678900000000795

    Why would that be? Where did those 8 extra zeroes followed by 795 come from?

    so you need use

    decimal_part = Cdec(d) - integer_part

    but Cdec() is a slow function
    Last edited by quickbbbb; Jul 5th, 2018 at 08:00 AM.

  6. #6

    Thread Starter
    Member
    Join Date
    Jun 2017
    Posts
    50

    Re: How to fast split Number integet part and decimal part ?

    Quote Originally Posted by Elroy View Post
    My first thought would be to do something like this:

    For one, it'll be faster because assigning to a Double won't require any typecasting

    Elroy
    This is not helpful

    because really time consuming is Cdec() function and code For w = 1 To 10 ^ 9 .... Next

  7. #7
    Frenzied Member
    Join Date
    Jun 2015
    Posts
    1,995

    Re: How to fast split Number integet part and decimal part ?

    Why are you using CDec? That converts to Decimal type, which you don't want to use.
    Not only is the Decimal type very slow - you're not even storing it in a Decimal, you're doing a second conversion back to Double.
    Do as Elroy suggested and just keep them as Double.
    Imagine what it would be like to set breakpoints in, or step through subclassing code;
    and then being able to hit stop/end/debug or continue, without crashing the IDE.

    VB6.tlb | Bulletproof Subclassing in the IDE (no thunks/assembly/DEP issues)

  8. #8

    Thread Starter
    Member
    Join Date
    Jun 2017
    Posts
    50

    Re: How to fast split Number integet part and decimal part ?

    Quote Originally Posted by DEXWERX View Post
    Why are you using CDec? That converts to Decimal type, which you don't want to use.
    Not only is the Decimal type very slow - you're not even storing it in a Decimal, you're doing a second conversion back to Double.
    Do as Elroy suggested and just keep them as Double.
    Name:  Noname.jpg
Views: 134
Size:  26.6 KB

    =============================


    Name:  2222.jpg
Views: 130
Size:  23.3 KB

  9. #9
    Frenzied Member
    Join Date
    Jun 2015
    Posts
    1,995

    Re: How to fast split Number integet part and decimal part ?

    Quote Originally Posted by SamOscarBrown View Post
    @Elroy...that's interesting (Sorry to butt in).

    When I ran your snippet, my decimal_part shown in debug.print was: 0.678900000000795

    Why would that be? Where did those 8 extra zeroes followed by 795 come from?
    Such is the life of a Base 10 fraction being crammed into a base 2 format.
    They can only be so accurate.

    If you don't like it you can either use a more accurate format, (doesn't have to be base 2)
    or round it.
    Imagine what it would be like to set breakpoints in, or step through subclassing code;
    and then being able to hit stop/end/debug or continue, without crashing the IDE.

    VB6.tlb | Bulletproof Subclassing in the IDE (no thunks/assembly/DEP issues)

  10. #10
    PowerPoster
    Join Date
    Oct 2013
    Posts
    3,132

    Re: How to fast split Number integet part and decimal part ?

    Quote Originally Posted by SamOscarBrown View Post
    @Elroy...that's interesting (Sorry to butt in).

    When I ran your snippet, my decimal_part shown in debug.print was: 0.678900000000795

    Why would that be? Where did those 8 extra zeroes followed by 795 come from?
    The problem of storing fractions in a binary notation.
    Code:
    Debug.Print 12345.6789 - 12345
    -> 0.678900000000795

  11. #11
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    6,453

    Re: How to fast split Number integet part and decimal part ?

    Such is the life of a Base 10 fraction being crammed into a base 2 format.
    They can only be so accurate.
    Thanks D....'such IS the life'. Fortunately, I don't have to deal with this type of accuracy in any of my 'home' projects. Appreciate the explanation. (yeah, if I were to WANT just the first four digits in the decimal, I'd use rounding).

    Outa here!

  12. #12
    Frenzied Member
    Join Date
    Jun 2015
    Posts
    1,995

    Re: How to fast split Number integet part and decimal part ?

    @quickbbb

    I see, you're using the Decimal Format so you can get rid of the perceived inaccuracies of the Double format.
    Why those inaccuracies exist, and how to deal with them is typically taught in computer science, but If you google it there are lots of resources explanations.

    What's more important is deciding what kind of accuracy you need. How many digits?

    Code:
        Dim integer_part   As Double
        Dim decimal_part   As Double
        '
        Dim d As Double
        d = 12345.6789
    
    
        integer_part = Fix(d)
        decimal_part = d - integer_part
    
        Debug.Print integer_part
        Debug.Print Round(decimal_part, 4)
    Imagine what it would be like to set breakpoints in, or step through subclassing code;
    and then being able to hit stop/end/debug or continue, without crashing the IDE.

    VB6.tlb | Bulletproof Subclassing in the IDE (no thunks/assembly/DEP issues)

  13. #13

    Thread Starter
    Member
    Join Date
    Jun 2017
    Posts
    50

    Re: How to fast split Number integet part and decimal part ?

    Quote Originally Posted by DEXWERX View Post
    round it.
    const decimal_part_length = 3

    you mean round ( 123.456 , decimal_part_length ) ??

    but you can not know in advance the decimal part length


    d may be = 123.456789
    d may be = 123.456
    d may be = 123.4561111111
    Last edited by quickbbbb; Jul 5th, 2018 at 08:29 AM.

  14. #14
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    4,370

    Re: How to fast split Number integet part and decimal part ?

    Quote Originally Posted by SamOscarBrown View Post
    @Elroy...that's interesting (Sorry to butt in).

    When I ran your snippet, my decimal_part shown in debug.print was: 0.678900000000795

    Why would that be? Where did those 8 extra zeroes followed by 795 come from?
    Hi Sam,

    That ...795 is just an artifact of converting binary to base-10 (i.e., decimal) and vice-versa. There's not really a "quick" way around that. Also, please notice that 0.678900000000795 is extremely close to 0.6789. In fact, in base-2, it's going to be as close as you can get.

    And, for a general purpose algorithm (i.e., one that will work with any Double), that's going to be about as fast as you can get.

    Now, if quickbbbb would like to provide some more restriction criteria, such as possibly the integer portion never overflowing a Long, or the fraction portion never has more than four digits, then we can possibly be a bit more accurate (however, pretty much anything is going to sacrifice speed, which is what I thought the whole point was). Any kind of typecasting (which my approach doesn't do) is going to slow things down.

    Best To All,
    Elroy
    Any software I post in these forums written by me is provided “AS IS” without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. Please understand that I’ve been programming since the mid-1970s and still have some of that code. My contemporary VB6 project is approaching 1,000 modules. In addition, I have a “VB6 random code folder” that is overflowing. I’ve been at this long enough to truly not know with absolute certainty from whence every single line of my code has come, with much of it coming from programmers under my employ who signed intellectual property transfers. I have not deliberately attempted to remove any licenses and/or attributions from any software. If someone finds that I have inadvertently done so, I sincerely apologize, and, upon notice and reasonable proof, will re-attach those licenses and/or attributions. To all, peace and happiness.

  15. #15
    Frenzied Member
    Join Date
    Jun 2015
    Posts
    1,995

    Re: How to fast split Number integet part and decimal part ?

    Quote Originally Posted by quickbbbb View Post
    const decimal_part_length = 3

    you mean round ( 123.456 , decimal_part_length ) ??

    but you can not know in advance the decimal part length
    in engineering, you always do. In engineering only so many decimals matter...

    If you need accuracy beyond what a Double can do - you have to sacrifice speed. Decimal is also a binary format, and similarly has only so much accuracy. If you want unlimited accuracy - you have to use a String Math Library/Big Number Library, or similar arbitrary precision math. Again - using anything like these, will be at a severe performance penalty.
    https://en.wikipedia.org/wiki/Arbitr...ion_arithmetic

    If you use the built in help, it can explain each datatype, min/max/epsilon, how many digits they are accurate to.
    https://www.microsoft.com/en-us/micr...wrong-answers/

    Quote Originally Posted by MSDN
    Decimal variables are stored as 96-bit (12-byte) signed integers scaled by a variable power of 10. The power of 10 scaling factor specifies the number of digits to the right of the decimal point, and ranges from 0 to 28. With a scale of 0 (no decimal places), the largest possible value is +/-79,228,162,514,264,337,593,543,950,335. With a 28 decimal places, the largest value is +/-7.9228162514264337593543950335 and the smallest, non-zero value is +/-0.0000000000000000000000000001.

    How Many Decimals of PI do we really need?

    https://www.jpl.nasa.gov/edu/news/20...e-really-need/
    Last edited by DEXWERX; Jul 5th, 2018 at 08:48 AM.
    Imagine what it would be like to set breakpoints in, or step through subclassing code;
    and then being able to hit stop/end/debug or continue, without crashing the IDE.

    VB6.tlb | Bulletproof Subclassing in the IDE (no thunks/assembly/DEP issues)

  16. #16
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    4,370

    Re: How to fast split Number integet part and decimal part ?

    And quickbbbb,

    Just FYI, Decimal types are very strange animals. They're 14 bytes with 12 of those bytes being a mixed-endian unsigned mantissa, which means that nothing about them will be able to use the floating-point-processor (i.e., all math done in software). Also, because of what Microsoft tried to do with them (i.e., have a number that never loses any precision, even in the fractional part), a Double (or even a Single) can easily overflow them. Basically, they're a number from 0 to 79228162514264337593543950336 (i.e., 2^96) with a decimal point placed anywhere in that number (or on either end), and also a separate sign-bit.

    Take Care,
    Elroy
    Last edited by Elroy; Jul 5th, 2018 at 08:51 AM.
    Any software I post in these forums written by me is provided “AS IS” without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. Please understand that I’ve been programming since the mid-1970s and still have some of that code. My contemporary VB6 project is approaching 1,000 modules. In addition, I have a “VB6 random code folder” that is overflowing. I’ve been at this long enough to truly not know with absolute certainty from whence every single line of my code has come, with much of it coming from programmers under my employ who signed intellectual property transfers. I have not deliberately attempted to remove any licenses and/or attributions from any software. If someone finds that I have inadvertently done so, I sincerely apologize, and, upon notice and reasonable proof, will re-attach those licenses and/or attributions. To all, peace and happiness.

  17. #17

    Thread Starter
    Member
    Join Date
    Jun 2017
    Posts
    50

    Re: How to fast split Number integet part and decimal part ?

    Const s = 12345.6789

    Const test_speed_loops = 10 ^ 6


    TT = Timer

    For k = 1 To test_speed_loops

    aaaa$ = cstr(s)

    Next

    MsgBox Timer - TT '----> it cost 0.29 second


    my code split integet part and decimal part need 0.8 second


    when VBA.Cstr( ) convert floating point to String , it same need calculate integet part length and decimal part length

    but VBA.Cstr( ) can calculate it very fast

    it should mean really can be split integet part and decimal part very fast . just VBA not offered a function to calculate decimal part length
    Last edited by quickbbbb; Jul 5th, 2018 at 09:13 AM.

  18. #18
    Frenzied Member
    Join Date
    Jun 2015
    Posts
    1,995

    Re: How to fast split Number integet part and decimal part ?

    Elroy offered a solution to calculate decimal part, that's faster than Cstr()?

    You're making a mountain out of a mole hill.
    Imagine what it would be like to set breakpoints in, or step through subclassing code;
    and then being able to hit stop/end/debug or continue, without crashing the IDE.

    VB6.tlb | Bulletproof Subclassing in the IDE (no thunks/assembly/DEP issues)

  19. #19
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    17,326

    Re: How to fast split Number integet part and decimal part ?

    You can test this if you like, it should be faster than converting with CDec. However, conversion to Double is used to handle wide range of values and string manipulation used to return unaltered fractional part of the number, locale-aware.

    Code:
    Public Sub SplitNumber(theNumber As Double, rtnWholeNr As Double, rtnFraction As Double)
    
        rtnWholeNr = Int(theNumber)
        If rtnWholeNr = theNumber Then
            rtnFraction = 0
        Else
            rtnFraction = CDbl("0" & Mid$(CStr(theNumber), Len(CStr(rtnWholeNr)) + 1))
        End If
        
    End Sub
    Sample Call:
    Code:
    Dim dblWhole As Double, dblFraction as Double
    SplitNumber 12345.6789, dblWhole, dblFraction
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  20. #20

    Thread Starter
    Member
    Join Date
    Jun 2017
    Posts
    50

    Re: How to fast split Number integet part and decimal part ?

    Quote Originally Posted by DEXWERX View Post
    Elroy offered a solution to calculate decimal part, that's faster than Cstr()?

    You're making a mountain out of a mole hill.

    my data amount very huge , i need a function faster than cstr()

    vba.Cstr convert long type to String = 4 second
    My.Cstr convert long type to String = 2 second

    now i need let My.Cstr can convert float type to String faster than vba.Cstr
    so i need know decimal part length

    Quote Originally Posted by LaVolpe View Post
    You can test this if you like, it should be faster than converting with CDec. However, conversion to Double is used to handle wide range of values and string manipulation used to return unaltered fractional part of the number, locale-aware.

    Code:
    Public Sub SplitNumber(theNumber As Double, rtnWholeNr As Double, rtnFraction As Double)
    
    you code = 1.4 second
    my code  = 0.8 second
    
        rtnWholeNr = Int(theNumber)
        If rtnWholeNr = theNumber Then
            rtnFraction = 0
        Else
            rtnFraction = CDbl("0" & Mid$(CStr(theNumber), Len(CStr(rtnWholeNr)) + 1))
        End If
        
    End Sub
    Sample Call:
    Code:
    Dim dblWhole As Double, dblFraction as Double
    SplitNumber 12345.6789, dblWhole, dblFraction

    your code = 1.4 secomd
    my code = 0.8 secomd
    Last edited by quickbbbb; Jul 5th, 2018 at 09:50 AM.

  21. #21
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    4,370

    Re: How to fast split Number integet part and decimal part ?

    Say LaVolpe,

    Isn't that Int() going to do funky things to negative numbers? That's why I used Fix(). I'll admit though that your approach does a better job of not adding superfluous fractional numbers. All the typecasting is sure going to slow things down though. But, at this point, I'm very confused about what quickbbbb's ultimate objective is.

    Y'all Take Care,
    Elroy
    Any software I post in these forums written by me is provided “AS IS” without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. Please understand that I’ve been programming since the mid-1970s and still have some of that code. My contemporary VB6 project is approaching 1,000 modules. In addition, I have a “VB6 random code folder” that is overflowing. I’ve been at this long enough to truly not know with absolute certainty from whence every single line of my code has come, with much of it coming from programmers under my employ who signed intellectual property transfers. I have not deliberately attempted to remove any licenses and/or attributions from any software. If someone finds that I have inadvertently done so, I sincerely apologize, and, upon notice and reasonable proof, will re-attach those licenses and/or attributions. To all, peace and happiness.

  22. #22
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    4,370

    Re: How to fast split Number integet part and decimal part ?

    Quote Originally Posted by quickbbbb View Post
    my data amount very huge , i need a function faster than cstr()
    Then quickbbbb, I gave you the answer in post #2. I suppose you could declare the fractional part as a Single. That would clean up most of your "very small" artifacts. And typecasting from Double to Single will be much faster than doing anything that involves typecasting to a String.

    Good Luck,
    Elroy
    Any software I post in these forums written by me is provided “AS IS” without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. Please understand that I’ve been programming since the mid-1970s and still have some of that code. My contemporary VB6 project is approaching 1,000 modules. In addition, I have a “VB6 random code folder” that is overflowing. I’ve been at this long enough to truly not know with absolute certainty from whence every single line of my code has come, with much of it coming from programmers under my employ who signed intellectual property transfers. I have not deliberately attempted to remove any licenses and/or attributions from any software. If someone finds that I have inadvertently done so, I sincerely apologize, and, upon notice and reasonable proof, will re-attach those licenses and/or attributions. To all, peace and happiness.

  23. #23
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    17,326

    Re: How to fast split Number integet part and decimal part ?

    Quote Originally Posted by Elroy View Post
    Say LaVolpe,

    Isn't that Int() going to do funky things to negative numbers? That's why I used Fix(). I'll admit though that your approach does a better job of not adding superfluous fractional numbers. All the typecasting is sure going to slow things down though. But, at this point, I'm very confused about what quickbbbb's ultimate objective is.

    Y'all Take Care,
    Elroy
    Good point. Int() can round up negative numbers. And type-casting will slow things down, no doubt.

    And as you said, we don't really know what the point is. Why the need to separate a fractional part from the whole & keep the fraction separately? If the range of numbers were known (i.e., none exceed min/max of Single for example), more efficiencies might be made. Turning off overflow checks could be helpful too.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  24. #24

    Thread Starter
    Member
    Join Date
    Jun 2017
    Posts
    50

    Re: How to fast split Number integet part and decimal part ?

    use int() function is my Negligence


    my data amount very huge , i need a function faster than cstr()

    vba.Cstr convert long type to String = 4 second
    My.Cstr convert long type to String = 2 second

    now i need let My.Cstr can convert float type to String faster than vba.Cstr
    so i need know decimal part length
    Last edited by quickbbbb; Jul 5th, 2018 at 10:00 AM.

  25. #25

    Thread Starter
    Member
    Join Date
    Jun 2017
    Posts
    50

    Re: How to fast split Number integet part and decimal part ?

    VBA.Fix( ) has bug

    sometime I use Fix() to get value , it return a round value

  26. #26
    Frenzied Member
    Join Date
    Jun 2015
    Posts
    1,995

    Re: How to fast split Number integet part and decimal part ?

    If the whole point is to convert a number to a string faster than CStr() I believe Olaf posted a faster CStr()

    But honestly - none of what you're doing makes any sense. What's the purpose of converting a large amount of numbers to string?

    First you're issue was with the speed of CDec(),
    Then it was the accuracy of the Double type.
    Now its the speed of CStr().

    It would be good to know the goal - because it seems you don't know the best way to it. We could help if we knew what the goal was.
    Last edited by DEXWERX; Jul 5th, 2018 at 11:12 AM.
    Imagine what it would be like to set breakpoints in, or step through subclassing code;
    and then being able to hit stop/end/debug or continue, without crashing the IDE.

    VB6.tlb | Bulletproof Subclassing in the IDE (no thunks/assembly/DEP issues)

  27. #27
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    17,326

    Re: How to fast split Number integet part and decimal part ?

    Quote Originally Posted by DEXWERX View Post
    It would be good to know the goal - because it seems you don't know the best way to it. We could help if we knew what the goal was.
    Dex, sounds something like this:

    1. He has his own CStr() routine for Long to String that he says is 2x faster than CStr()

    2. Sounds like he is looking at a way to separate the numbers into whole/fraction parts so he can then use his own CStr() implementation? And the separation is what he is wanting as fast as possible.

    But I'm not sure how that 2nd part is going to work. Wouldn't the fractional part need to be stored in a Single or Double to preserve any leading zeroes? And if so, stripping a part of a double to be placed in another double, then putting that into a string, doesn't sound like it's going to be speedy regardless
    Last edited by LaVolpe; Jul 5th, 2018 at 11:20 AM.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  28. #28
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    4,370

    Re: How to fast split Number integet part and decimal part ?

    I'm just procrastinating my packing for a trip, thinking about this thing.

    quickbbbb, one thing you must understand ... if you're going to insist on converting to a string, that's going to be slow. All numbers (true numeric variables or constants) are some kind of binary in VB (VB6 and/or VBA). And floating-point (which would include Single, Double, & Decimal) can be even more complex.

    And Strings are basically arrays of characters, and, inherently, have nothing to do with numbers. And any algorithm to convert these binary floating-point numbers into strings is going to be slow. You may be able to "roll-your-own" and speed it up a bit, as I suspect the CStr() function does checking to see if it should go to scientific notation, and some other checking (possibly for NaN and Inf), where a roll-your-own wouldn't need to do that. But it's still not going to be super-speedy.

    And also, just thinking it through, if we insisted on this approach, and a Decimal type would cover our range of numbers, I suspect a CnvDecToStr() function could probably be optimized to be fastest (faster than CnvSngToStr() or CnvDblToStr()). I say this because these Decimal types are basically 96 bit integers with an inserted decimal point. I'd have to ponder the fastest way to get this 96 bit integer into a String. I might even consider going through a LongLong to get it done (which VB6 can be forced to handle and new VBA versions just have). I wonder how fast a CStr(LongLong) would be? If it's very fast, that might be an answer. Just do that, and then insert the decimal point and any minus sign.

    However, I'm just spit-balling. As Dex said a few posts up, it seems like you're making a mountain out of a mole-hill.

    Good Luck,
    Elroy

    EDIT1: Actually, if we want to be sure and handle the full range of a Decimal, we can't put the integer in a LongLong, as the integer portion of a decimal is 12 bytes, and a LongLong is only 8 bytes. But quickbbbb, in post #1, is putting the integer portion in a Long, which is only 4 bytes. So we really do need better criteria for what we're trying to do before an appropriate answer can be offered. quickbbbb, what's the range of numbers you need to deal with (both in terms of magnitude and in terms of fractional precision)?
    Last edited by Elroy; Jul 5th, 2018 at 12:17 PM.
    Any software I post in these forums written by me is provided “AS IS” without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. Please understand that I’ve been programming since the mid-1970s and still have some of that code. My contemporary VB6 project is approaching 1,000 modules. In addition, I have a “VB6 random code folder” that is overflowing. I’ve been at this long enough to truly not know with absolute certainty from whence every single line of my code has come, with much of it coming from programmers under my employ who signed intellectual property transfers. I have not deliberately attempted to remove any licenses and/or attributions from any software. If someone finds that I have inadvertently done so, I sincerely apologize, and, upon notice and reasonable proof, will re-attach those licenses and/or attributions. To all, peace and happiness.

  29. #29

    Thread Starter
    Member
    Join Date
    Jun 2017
    Posts
    50

    Re: How to fast split Number integet part and decimal part ?

    Quote Originally Posted by Elroy View Post
    I'm just procrastinating my packing for a trip, thinking about this thing.

    if you're going to insist on converting to a string, that's going to be slow
    Elroy
    after i get stock price ( float type ) , i need save stock price to file

    so I need convert number to string

    of course , i can save it to Binary file ( it will be no need to convert to string )

    but For some reason , I must save it to text file
    Last edited by quickbbbb; Jul 5th, 2018 at 12:25 PM.

  30. #30
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    4,370

    Re: How to fast split Number integet part and decimal part ?

    Excellent. That helps tremendously.

    quickbbbb, tell me if I'm wrong, but don't stock prices consider splitting once they get very much over $1000? And also, wouldn't four decimal places be the maximum you'd ever track regarding stock prices?

    If all that's true, then I'd consider using a Currency (especially if the max four decimal places can be assumed). You could trivially, do everything you want while keeping all types as Currency.

    Code:
    
        Const s As Currency = 12345.6789
    
    
        Dim integer_part   As Currency
        Dim decimal_part   As Currency
    
    
        integer_part = Fix(s)
        decimal_part = s - integer_part
    
    
        Debug.Print integer_part
        Debug.Print decimal_part
    
    
    Take Care,
    Elroy

    EDIT1: And Currency math is effectively a special form of integer math, which is lightening fast.
    Any software I post in these forums written by me is provided “AS IS” without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. Please understand that I’ve been programming since the mid-1970s and still have some of that code. My contemporary VB6 project is approaching 1,000 modules. In addition, I have a “VB6 random code folder” that is overflowing. I’ve been at this long enough to truly not know with absolute certainty from whence every single line of my code has come, with much of it coming from programmers under my employ who signed intellectual property transfers. I have not deliberately attempted to remove any licenses and/or attributions from any software. If someone finds that I have inadvertently done so, I sincerely apologize, and, upon notice and reasonable proof, will re-attach those licenses and/or attributions. To all, peace and happiness.

  31. #31

    Thread Starter
    Member
    Join Date
    Jun 2017
    Posts
    50

    Re: How to fast split Number integet part and decimal part ?

    Quote Originally Posted by DEXWERX View Post
    If the whole point is to convert a number to a string faster than CStr() I believe Olaf posted a faster CStr()
    Is it possible to provide a URL?

    thank you

  32. #32
    Hyperactive Member
    Join Date
    Nov 2017
    Posts
    361

    Re: How to fast split Number integet part and decimal part ?

    Quote Originally Posted by Elroy View Post
    Excellent. That helps tremendously.

    quickbbbb, tell me if I'm wrong, but don't stock prices consider splitting once they get very much over $1000? And also, wouldn't four decimal places be the maximum you'd ever track regarding stock prices?
    You shouldn't assume it will be stock prices from a USA Market. Also, see Berkshire Hathaway A stock.

  33. #33
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    4,370

    Re: How to fast split Number integet part and decimal part ?

    Quote Originally Posted by OptionBase1 View Post
    You shouldn't assume it will be stock prices from a USA Market. Also, see Berkshire Hathaway A stock.
    Hi Option,

    Well yeah, I knew they were sort of doing away with all the splitting. However, so long as we're good with four decimal places, Currency seems like the correct choice. That might get a bit tight form some currencies that use three decimal places and want to track stocks to 1/100th of a penny, but I don't think most (if any) of those places have stock exchanges. Does Chile have a stock exchange, or Bahrain? Again, I suppose we need more specifics from quickbbbb, but I'm thinking that Currency would get the job done. I mean, look at the name of the type, and then think about what we're trying to do.

    Take Care,
    Elroy
    Any software I post in these forums written by me is provided “AS IS” without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. Please understand that I’ve been programming since the mid-1970s and still have some of that code. My contemporary VB6 project is approaching 1,000 modules. In addition, I have a “VB6 random code folder” that is overflowing. I’ve been at this long enough to truly not know with absolute certainty from whence every single line of my code has come, with much of it coming from programmers under my employ who signed intellectual property transfers. I have not deliberately attempted to remove any licenses and/or attributions from any software. If someone finds that I have inadvertently done so, I sincerely apologize, and, upon notice and reasonable proof, will re-attach those licenses and/or attributions. To all, peace and happiness.

  34. #34
    Addicted Member
    Join Date
    Feb 2017
    Posts
    204

    Re: How to fast split Number integet part and decimal part ?

    Elroy:
    I knew they were sort of doing away with all the splitting.
    Not True.

    Most items that are traded can be grouped into:
    1) Indexes
    2) Commodities
    3) Stocks (large cap, medium cap, small cap)
    4) Options
    5) Bonds
    6) Real Estate Trusts.

    Within each on of these groups the number of significant digits can vary Not only based on their
    calculated value (such as indexes), but the price EACH EXCHANGE sets (number of significant digits)
    for the instrument. The same instrument may be traded on multiple exchanges.
    Some trade in 1/8th, quarters, decimal values, etc. Go to the NYSE or (www.cmegroup.com) and pull up a few.
    My recommendation is to set up a table by symbol and then associate the decimal value need for that instrument.
    For example if trades in 1/8th use .125
    Then based on the instrument of interest, you know what calculation is needed.
    Last edited by vb6forever; Jul 5th, 2018 at 04:22 PM.

  35. #35
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    17,326

    Re: How to fast split Number integet part and decimal part ?

    Quote Originally Posted by quickbbbb View Post
    ...but For some reason , I must save it to text file
    This seems to be the problem if this text file is for your use. Why should the format of the data in the file matter? Save it as binary or as a UDT record. If this text file is for use in some other application, then it is understandable that you don't have any say in the format. But if not, why force a file format that you say is slowing you down?
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  36. #36
    PowerPoster
    Join Date
    Oct 2013
    Posts
    3,132

    Re: How to fast split Number integet part and decimal part ?

    And the FileIO will be the bottleneck, not the speed of a typical conversion routine.

  37. #37
    PowerPoster
    Join Date
    Feb 2006
    Posts
    18,917

    Re: How to fast split Number integet part and decimal part ?

    None of this thread makes a lot of sense to me. Is this some attempt to do battle with regional and language settings, i.e. the character used to represent decimal points?

  38. #38
    Frenzied Member ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    1,081

    Re: How to fast split Number integet part and decimal part ?

    Hi quickbbbb,


    Quote Originally Posted by quickbbbb View Post
    my data amount very huge , i need a function faster than cstr()
    you say your Data is very huge, it would be helpful to know where the Data comes from and in what Format
    why do you want to split the number anyway ?

    regards
    Chris
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  39. #39
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    4,497

    Re: How to fast split Number integet part and decimal part ?

    Quote Originally Posted by ChrisE View Post
    ...
    why do you want to split the number anyway ?
    ...
    Chris
    LaVolpe already explained a probable reason in post #27, and also identified a possible situation quickbbbb didn't consider, i.e. leading 0's in the decimal portion.

  40. #40

    Thread Starter
    Member
    Join Date
    Jun 2017
    Posts
    50

    Re: How to fast split Number integet part and decimal part ?

    Now I decided to accelerate it with multi cpu core


    Quote Originally Posted by dilettante View Post
    None of this thread makes a lot of sense to me. Is this some attempt to do battle with regional and language settings, i.e. the character used to represent decimal points?
    OK , Stop the thread

    Thank you for your participation.
    Last edited by quickbbbb; Jul 6th, 2018 at 09:54 AM.

Page 1 of 2 12 LastLast

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width