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.
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.
You have the complete VBA code. What problem are you having?
Jeff
| | |·| |·| |·| |·| | |:| | |·| |·|
Read the rules
Use code tags to [code]enclose your code![/code]
@6StringJazzer; the code is not executing.
I get it to run but I get a Division by Zero error here
Formula: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.
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.
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.)
@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/
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…
Cross-posted today at https://www.mrexcel.com/board/thread...of-pi.1189780/
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
Thanks, @karedog; ... this surely helps.
@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
@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
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 ?
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 ?
Also, I must correct this statement :
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 :
when i = 0, nth(0) is calculatedPlease Login or Register to view this content.
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) :
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 :Please Login or Register to view this content.
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:
The 0 digit = 3Please Login or Register to view this content.
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.
@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.
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.
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.
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.
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.
HTH,Please Login or Register to view this content.
Francesco
@Francesco; Thanks ... This works!
This is one order of magnitude faster: 2.3s for the 10000th digit alone.
As karedog noted, these algorithms calculate Pi up to the desired digit.Please Login or Register to view this content.
HTH,
Francesco
@Francesco; Great ... this helps surely!
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks