Gold segmentation is more than the decimal point (Big Data operation, Go+java language implementation)

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Gold segmentation is more than infinite after decimal point (Go+java language implementation)

The code given in theory can be infinitely accurate, but the fact that the data is too precise is ineffective for humans.
The following code is accurate to the decimal point after the 2000-bit precision is not tested, but more than 1000 accuracy is correct, only need to adjust the program constants can be accurate to the exact number of digits you want.

This is the 2000-bit evaluated by the runtime: (Csnd does not wrap)

0.61803398874989484820458683436563811772030917980576286213544862270526046281890244970720720418939113748475408807538689175 2126633862223536931793180060766726354433389086595939582905638322661319928290267880675208766892501711696207032221043216269 5486262963136144381497587012203408058879544547492461856953648644492410443207713449470495658467885098743394422125448770664 7809158846074998871240076521705751797883416625624940758906970400028121042762177111777805315317141011704666599146697987317 6135600670874807101317952368942752194843530567830022878569978297783478458782289110976250030269615617002504643382437764861 0283831268330372429267526311653392473167111211588186385133162038400522216579128667529465490681131715993432359734949850904 0947621322298101726107059611645629909816290555208524790352406020172799747175342777592778625619432082750513121815628551222 4809394712341451702237358057727861600868838295230459264787801788992199027077690389532196819861514378031499741106926088674 296226757560523172777520353613936210767389376455606060592165894667595519004005559089502295309423124823552122124154440064703405657347976639723949499465845 7887303962309037503399385621024236902513868041457799569812244574717803417312645322041639723213404444948730231541767689375 2103068737880344170093954409627955898678723209512426893557309704509595684401755519881921802064052905518934947592600734852 2821010881946445442223188913192946896220023014437702699230078030852611807545192887705021096842493627135925187607778846658 3615023891349333312231053392321362431926372891067050339928226526355620902979864247275977256550861548754357482647181414512 7000602389016207773224499435308899909501680328112194320481964387675863314798571911397815397807476150772211750826945863932 0456520989698555678141069683728840587461033781054443909436835835813811311689938555769754841491445341509129540700501947754 8616307542264172939468036731980586183391832859913039607201445595044977921207612478564591616083705949878600697018940988640 076443617093341727091914336501371 

code Author: Days, Reproduced in this article please specify the source and the original author

Package Mainimport ("FMT") type myInt int32const (SIZE = 120//array length data length less than: 120*9times = 5150//ab Interchange presize = 2000 Number of significant digits)/** code Author: Days of * Blog: http://blog.csdn.net/WAPWO?viewmode=contents */func main () {A, B, tmp, res: = make ([]myint, Size), make ([]myint, size), make ([]myint, size), make ([]byte, Presize) Initab (A, B) gab (A, B, TMP)//printbignum (a)// Printbignum (b) Bignumdiv (A, B, TMP, RES) FMT. Println (RES)}/* copying an integer array */func cpymyintarr (d, S []myint) {for i: = 0; i < SIZE; i++ {D[i] = s[i]}}/* The sum of large numbers, A=a+b*/func Bignu MSum (A, b []myint) {for i: = 0; i < SIZE; i++ {count: = A[i] + b[i]//because known a[i] and B[i] are nine-digit if Count > 1000000000 {a[i+ 1] + = 1a[i] = count-1000000000} else {A[i] = count}}//If there is no a[i+1], then overflow, in the input need to control}/* 10 times times the value, A=10*a*/func BIGNUM10 (A, TMP []m Yint) {Cpymyintarr (TMP, a) for I: = 0; i < 9; i++ {bignumsum (A, TMP)}}/* large number, A=a-b*/func bignumdif (A, B, TMP []myint) in t16 {Cpymyintarr (tmp, a) for I: = 0; i < SIZE; i++ {count: = a[i]-B[i]if count < 0 {if I < SIZE-1 {A[i+1]-= 1a[i] = 1000000000 + count} else {Cpymyintarr (A, TMP) return 0}} else {a[i] = count}}for I: = SIZE-1; I >= 0; i--{if a[i] < 0 {Cpymyintarr (A, TMP) return 0}}return 1}/* Analog Division operation res=b/a, enter Operation B<a*/func Bignumdiv (A, B, TMP []myint, res []byte) {var count bytefor I: = 0; i < presize; i++ {count = 0for bignumdif (b, A, tmp) = = 1 {count++}res[i] = count  BIGNUM10 (b, TMP)}}/* generated scale, b/a-A/(B+a) */func gab (A, B, TMP []myint) {for i: = 0; i < times; i++ {Cpymyintarr (TMP, A) bignumsum (A, b) Cpymyintarr (b, TMP)}}/* initialize AB array */func initab (A, b []myint) {for i: = 1; i < SIZE; i++ {a[i] = 0b[i] = 0}a[0] = 3b[0] = 2}/* print large data array */func printbignum (a []myint) {for i: = SIZE-1; I >= 0; i--{fmt. Printf ("%10d", A[i])}fmt. Println ()}

Import Java.math.bigdecimal;public class Demo {static int i=0;public static void main (String args[]) {BigDecimal x=new BIGD Ecimal (double.parsedouble ("0.5")); System.out.println (Fun (1,x));} Static BigDecimal Fun (int times,bigdecimal n) {//n ' =1/(1+n) BigDecimal a=new BigDecimal (double.parsedouble ("1")); if ( times>1000) {n=a.add (n); return a.divide (N,300,BIGDECIMAL.ROUND_HALF_UP);} Else{n=a.add (n); return Fun (Times+1,a.divide (N,300,BIGDECIMAL.ROUND_HALF_UP));}}


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.