+ Reply to Thread
Results 1 to 24 of 24

From C to VBA - Error in Conversion

  1. #1
    Registered User
    Join Date
    11-19-2014
    Location
    Mumbai, Maharashtra, INDIA
    MS-Off Ver
    2003
    Posts
    52

    From C to VBA - Error in Conversion

    I need help to convert the c code to VBA. My VBA code and reference C code is enclosed.

    Please Login or Register  to view this content.
    Please Login or Register  to view this content.

  2. #2
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,532

    Re: From C to VBA - Error in Conversion

    You have the complete VBA code. What problem are you having?
    Jeff
    | | |·| |·| |·| |·| | |:| | |·| |·|
    Read the rules
    Use code tags to [code]enclose your code![/code]

  3. #3
    Registered User
    Join Date
    11-19-2014
    Location
    Mumbai, Maharashtra, INDIA
    MS-Off Ver
    2003
    Posts
    52

    Re: From C to VBA - Error in Conversion

    @6StringJazzer; the code is not executing.

  4. #4
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,532

    Re: From C to VBA - Error in Conversion

    I get it to run but I get a Division by Zero error here

    Formula: copy to clipboard
    Please Login or Register  to view this content.


    because e has not been assigned a value the first pass through this loop. The C code uses this expression as the iterator, not the exit condition.

    It would help to know what you want your code to do. There are no comments and you have not described the specification for this function.

  5. #5
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,532

    Re: From C to VBA - Error in Conversion

    I did a conversion from scratch, but it still does not seem to do much. It is returning 0 for 0-5. I am guessing this is some sort of algorithm for the nth digit of pi.

    Your function does have one definite bug. You are passing the argument d to your function, and then changing the value of d inside the function. The default mode for argument passing in VBA is by reference, so you are also changing the caller's version of d. If you are going to change d in your function you should call it by value.

    I do not have a C environment to test the C version of this function. Do you know how it works, or did you just copy from someplace?

    Please Login or Register  to view this content.

  6. #6
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,532

    Re: From C to VBA - Error in Conversion

    You seem to be asking the same question about a different C class in another place. Strictly speaking this is not exactly the same question, since the code is different, but you are trying to reach the same goal and both are C to VBA conversions. Please post a cross-link here.

    Administrative Note:

    Welcome to the forum.

    We would very much like to help you with your query, however it has been brought to our attention that the same query has been posted on one or more other forums and you have not provided the required cross-post link(s) here.

    Please see Forum Rule #3 about cross-posting and adjust accordingly. Read this to understand why we (and other sites like us) consider this to be important.

    (Note: this requirement is not optional. No help to be offered until the link is provided.)

  7. #7
    Registered User
    Join Date
    11-19-2014
    Location
    Mumbai, Maharashtra, INDIA
    MS-Off Ver
    2003
    Posts
    52

    Re: From C to VBA - Error in Conversion

    @6StringJazzer; The code calculates the n-th digit of pi. So, instead of computing the whole value to the n-th place, it just computes the nth place digit.

    A working demo of the code is at https://tio.run/##jVFNU4MwFLzzK/bSGR...ZmPVSHqK26aT4B

    The same query cross-posted with a different code at https://www.mrexcel.com/board/thread...of-pi.1189484/

  8. #8
    Registered User
    Join Date
    11-19-2014
    Location
    Mumbai, Maharashtra, INDIA
    MS-Off Ver
    2003
    Posts
    52

    Re: From C to VBA - Error in Conversion

    To add ... ' … the decimal value of pi with decimal places from 0,001 to 1,000…
    ' 1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679
    ' 8214808651328230664709384460955058223172535940812848111745028410270193852110555964462294895493038196
    ' 4428810975665933446128475648233786783165271201909145648566923460348610454326648213393607260249141273
    ' 7245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094
    ' 3305727036575959195309218611738193261179310511854807446237996274956735188575272489122793818301194912
    ' 9833673362440656643086021394946395224737190702179860943702770539217176293176752384674818467669405132
    ' 0005681271452635608277857713427577896091736371787214684409012249534301465495853710507922796892589235
    ' 4201995611212902196086403441815981362977477130996051870721134999999837297804995105973173281609631859
    ' 5024459455346908302642522308253344685035261931188171010003137838752886587533208381420617177669147303
    ' 5982534904287554687311595628638823537875937519577818577805321712268066130019278766111959092164201989…

  9. #9
    Registered User
    Join Date
    11-19-2014
    Location
    Mumbai, Maharashtra, INDIA
    MS-Off Ver
    2003
    Posts
    52

    Re: From C to VBA - Error in Conversion


  10. #10
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: From C to VBA - Error in Conversion

    Maybe :
    Please Login or Register  to view this content.
    1. I care dog
    2. I am a loop maniac
    3. Forum rules link : Click here
    3.33. Don't forget to mark the thread as solved, this is important

  11. #11
    Registered User
    Join Date
    11-19-2014
    Location
    Mumbai, Maharashtra, INDIA
    MS-Off Ver
    2003
    Posts
    52

    Re: From C to VBA - Error in Conversion

    Thanks, @karedog; ... this surely helps.

  12. #12
    Registered User
    Join Date
    11-19-2014
    Location
    Mumbai, Maharashtra, INDIA
    MS-Off Ver
    2003
    Posts
    52

    Re: From C to VBA - Error in Conversion

    @karedog; while testing code I figured that there is a small error in the original c code itself. There are a few digits that are being computed wrong. For instance:

    The correct digits of Pi:
    14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328230664709384460955058223172535940812848111745028410270193852110555964462294895493038196442881097566593344612847564823378678316527120190914

    The output digits: the ones coded with "x" are wrongly computed:
    141592653589793238462643383279x0288419716939937510582097494459230781640628620899862x034825342117067982148086513282306647093844609550582231725359408128481117450284102x01938521105559644622948954930381964428810975665933446128475648233786783165271x0190914

    The actual output without x:
    14159265358979323846264338327940288419716939937510582097494459230781640628620899862703482534211706798214808651328230664709384460955058223172535940812848111745028410260193852110555964462294895493038196442881097566593344612847564823378678316527110190914

  13. #13
    Registered User
    Join Date
    11-19-2014
    Location
    Mumbai, Maharashtra, INDIA
    MS-Off Ver
    2003
    Posts
    52

    Re: From C to VBA - Error in Conversion

    @karedog; To add further, each "x" is less by 1 than expected. The table below summarises the digits where the code is failing. (Output compared with first 1,000 digits of pi.)

    d Actual pi Output pi
    32 4 5
    85 7 8
    167 6 7
    245 1 2
    291 5 6
    307 6 7
    357 8 9
    360 5 6
    361 9 0
    408 6 7
    493 2 3
    523 5 6
    545 6 7
    601 1 2
    602 9 0
    659 8 9
    669 2 3
    703 1 2
    724 3 4
    802 4 5
    818 2 3
    853 0 1
    855 0 1
    856 9 0
    857 9 0
    973 2 3
    974 9 0
    996 1 2

  14. #14
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: From C to VBA - Error in Conversion

    Juggler_IN,

    After examining the expected result compared to output of C program, I conclude that the correction cannot be made by keep using original code.
    I propose a new macro (modified), but it will not produce individual digit of PI (looping from x to y), but the value of the biggest expected digit is set first (for example 50), then the macro will calculate all the digits between 0 to 50.
    Using this macro, not only the digit number is corrected, but also it is blazing fast.

    Shall we do it ?

  15. #15
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: From C to VBA - Error in Conversion

    Well, do you want my modified macro as proposed above, or will you wait someone fixing from the C code side and then re-translate it to VBA ?

  16. #16
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: From C to VBA - Error in Conversion

    Also, I must correct this statement :
    Quote Originally Posted by Juggler_IN View Post
    @6StringJazzer; The code calculates the n-th digit of pi. So, instead of computing the whole value to the n-th place, it just computes the nth place digit.
    The C code is compute not just the n-th place, but all from 0 to n-th place.
    For example, if you call NthPi(10), actually NthPi(0), NthPi(1), NthPi(2), NthPi(3), ... until NthPi(10) is calculated, so you are not just compute at exact n-th place.

    This means, if you run the loop like this :
    Please Login or Register  to view this content.
    when i = 0, nth(0) is calculated
    when i = 1, nth(0) is recalculated, nth(1) is calculated
    when i = 2, nth(0) is recalculated, nth(1) is recalculated, nth(2) is calculated
    when i = 3, nth(0) is recalculated, nth(1) is recalculated, nth(2) is recalculated, nth(3) is calculated
    when i = 4, nth(0) is recalculated, nth(1) is recalculated, nth(2) is recalculated, nth(3) is recalculated, nth(4) is calculated
    etc

    To show using the code :

    Here we "only want the 10th" place (notice no loop, just single item 10) :
    Please Login or Register  to view this content.
    We insert the Debug line to show what is going on behind the scene :
    Please Login or Register  to view this content.
    The result of macro is :
    The 0 digit = 3
    The 1 digit = 1
    The 2 digit = 4
    The 3 digit = 1
    The 4 digit = 5
    The 5 digit = 9
    The 6 digit = 2
    The 7 digit = 6
    The 8 digit = 5
    The 9 digit = 3
    The 10 digit = 5
    The 11 digit = 8

    Which means, even though we input the 10 as n-th place, but actually all the digit between 0 to 10 is calculated, it's only not shown up to user (hidden).

    Thus the statement "it just computes the nth place digit" is wrong.


    Now, if put it back again into loop, the result will be very clear:
    Please Login or Register  to view this content.
    The 0 digit = 3
    The 1 digit = 1
    =================
    The 0 digit = 3
    The 1 digit = 1
    The 2 digit = 4
    =================
    The 0 digit = 3
    The 1 digit = 1
    The 2 digit = 4
    The 3 digit = 1
    =================
    The 0 digit = 3
    The 1 digit = 1
    The 2 digit = 4
    The 3 digit = 1
    The 4 digit = 5
    =================
    The 0 digit = 3
    The 1 digit = 1
    The 2 digit = 4
    The 3 digit = 1
    The 4 digit = 5
    The 5 digit = 9
    =================
    The 0 digit = 3
    The 1 digit = 1
    The 2 digit = 4
    The 3 digit = 1
    The 4 digit = 5
    The 5 digit = 9
    The 6 digit = 2
    =================
    The 0 digit = 3
    The 1 digit = 1
    The 2 digit = 4
    The 3 digit = 1
    The 4 digit = 5
    The 5 digit = 9
    The 6 digit = 2
    The 7 digit = 6
    =================
    The 0 digit = 3
    The 1 digit = 1
    The 2 digit = 4
    The 3 digit = 1
    The 4 digit = 5
    The 5 digit = 9
    The 6 digit = 2
    The 7 digit = 6
    The 8 digit = 5
    =================
    The 0 digit = 3
    The 1 digit = 1
    The 2 digit = 4
    The 3 digit = 1
    The 4 digit = 5
    The 5 digit = 9
    The 6 digit = 2
    The 7 digit = 6
    The 8 digit = 5
    The 9 digit = 3
    =================
    The 0 digit = 3
    The 1 digit = 1
    The 2 digit = 4
    The 3 digit = 1
    The 4 digit = 5
    The 5 digit = 9
    The 6 digit = 2
    The 7 digit = 6
    The 8 digit = 5
    The 9 digit = 3
    The 10 digit = 5
    =================
    The 0 digit = 3
    The 1 digit = 1
    The 2 digit = 4
    The 3 digit = 1
    The 4 digit = 5
    The 5 digit = 9
    The 6 digit = 2
    The 7 digit = 6
    The 8 digit = 5
    The 9 digit = 3
    The 10 digit = 5
    The 11 digit = 8
    =================
    Last edited by karedog; 12-09-2021 at 12:53 PM.

  17. #17
    Registered User
    Join Date
    11-19-2014
    Location
    Mumbai, Maharashtra, INDIA
    MS-Off Ver
    2003
    Posts
    52

    Re: From C to VBA - Error in Conversion

    @karedog; I just saw your replies.

    This was base-10 equivalent to base-16 bailey-borwein-plouffe-formula. The beauty of this code is that in base-16 you can compute 10,000 digit of pi without computing earlier digits.
    https://observablehq.com/@rreusser/c...louffe-formula

    For base-10 while searching today, I found this link and pdf.
    https://www.semanticscholar.org/pape...8a32709b40f5d2

    I will connect with you tomorrow as it is late night at my end.

  18. #18
    Registered User
    Join Date
    11-19-2014
    Location
    Mumbai, Maharashtra, INDIA
    MS-Off Ver
    2003
    Posts
    52

    Re: From C to VBA - Error in Conversion

    My need is not of a 0-th to n-th digit of pi macro. I am looking for a code which can output n-th digit directly like the base-16 code.

  19. #19
    Valued Forum Contributor Hydraulics's Avatar
    Join Date
    07-15-2018
    Location
    Udine - Italy
    MS-Off Ver
    Office 365
    Posts
    369

    Re: From C to VBA - Error in Conversion

    Quote Originally Posted by Juggler_IN View Post
    This was base-10 equivalent to base-16 bailey-borwein-plouffe-formula.
    Well, not really. The C# you posted is (poorly) based on the spigot algorithm (warning, pdf) by Rabinowitz and Wagon.
    Unfortunately, it has some drawbacks, since we must track remainders.
    A faster algorithm can be used up to 50k digits, changing base. You can find it, for instance, in "Pi Unleashed" (yes, still coded in C).
    Aim high or don't even try.
    ---------------------------------
    If your question has been answered, don't forget to mark the thread as SOLVED.
    If you find an answer helpful, click on the star icon at the bottom of the post.

  20. #20
    Registered User
    Join Date
    11-19-2014
    Location
    Mumbai, Maharashtra, INDIA
    MS-Off Ver
    2003
    Posts
    52

    Re: From C to VBA - Error in Conversion

    On page 6 of the spigot algorithm (pdf shared by Hydraulics) by Rabinowitz and Wagon... "At the 32nd iteration, a 102 shows up, yielding a predigit of 10." I think this fact can be used to trap the error in C code. This occurrence of 102, is happening across all "x" marked values. If we add Debug p, x(l) after the line p = x(l) \ 10, we get to see the instances of 102 against x(l).
    Last edited by Juggler_IN; 12-10-2021 at 04:41 AM.

  21. #21
    Valued Forum Contributor Hydraulics's Avatar
    Join Date
    07-15-2018
    Location
    Udine - Italy
    MS-Off Ver
    Office 365
    Posts
    369

    Re: From C to VBA - Error in Conversion

    Here is a modified function that returns Pi correctly up to 1000 digits. Awfully slow if used in a for loop, it needs 78 seconds. For the single 10000th digit it requires 23 seconds.

    Please Login or Register  to view this content.
    HTH,

    Francesco

  22. #22
    Registered User
    Join Date
    11-19-2014
    Location
    Mumbai, Maharashtra, INDIA
    MS-Off Ver
    2003
    Posts
    52

    Re: From C to VBA - Error in Conversion

    @Francesco; Thanks ... This works!

  23. #23
    Valued Forum Contributor Hydraulics's Avatar
    Join Date
    07-15-2018
    Location
    Udine - Italy
    MS-Off Ver
    Office 365
    Posts
    369

    Re: From C to VBA - Error in Conversion

    This is one order of magnitude faster: 2.3s for the 10000th digit alone.

    Please Login or Register  to view this content.
    As karedog noted, these algorithms calculate Pi up to the desired digit.

    HTH,

    Francesco

  24. #24
    Registered User
    Join Date
    11-19-2014
    Location
    Mumbai, Maharashtra, INDIA
    MS-Off Ver
    2003
    Posts
    52

    Re: From C to VBA - Error in Conversion

    @Francesco; Great ... this helps surely!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Time Conversion Error
    By royalew/cheese in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 07-22-2021, 11:07 AM
  2. [SOLVED] EXCEL HEX2DEC conversion error
    By Jerry in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 09-06-2005, 03:05 AM
  3. ROUNDing on a MAC - conversion error
    By Lokai in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-30-2005, 05:34 PM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1