arbitrary-precision

How can I use arbitrary length integers in Perl?

旧时模样 提交于 2019-12-04 05:09:14
Is there any standard way to use integers of arbitrary length in Perl? I am working on code that generates x64 assembly for tests, and I'm tired of manipulating 32 bits at a time. I'm using Perl 5.10.0, for what it's worth. If you only want to use big integers, you can use the bigint , which you can scope to a file: use bigint; or just a limited scope: { use bigint; ...; } If you need big floating point numbers as well as big integers, you can use the bignum pragma in the same way. Either way, these will slow down your program slightly (or significantly if you are doing a lot of math), so you

Speed up x64 assembler ADD loop

独自空忆成欢 提交于 2019-12-04 02:30:04
I'm working on arithmetic for multiplication of very long integers (some 100,000 decimal digits). As part of my library I to add two long numbers. Profiling shows that my code runs up to 25% of it's time in the add() and sub() routines, so it's important they are as fast as possible. But I don't see much potential, yet. Maybe you can give me some help, advice, insight or ideas. I'll test them and get back to you. So far my add routine does some setup and then uses a 8-times unrolled loop: mov rax, QWORD PTR [rdx+r11*8-64] mov r10, QWORD PTR [r8+r11*8-64] adc rax, r10 mov QWORD PTR [rcx+r11*8

How to work on big integers that don't fit into any of language's data structures

这一生的挚爱 提交于 2019-12-04 01:33:56
I'm trying to solve a programming contest's preliminary problems and for 2 of the problems I have to calculate and print some very big integers(like 100!, 2^100). I also need a fast way to calculate powers of this big integers. Can you advice me some algorithms or data structures for this?(btw, I read C Interfaces and Implementations 'arbitrary precision arithmetic' section but it doesn't help for pow()) EDIT: I think exponentiation by squaring method and bit-shifting will work for power but I also need a fast way to calculate factorials for this ints. Thanks. EDIT2: For those who are

Convert large hex string to decimal string

大城市里の小女人 提交于 2019-12-03 13:46:04
I need to convert a large (too large for the built-in data types) hex string to a string with it's decimal representation. For example: std::string sHex = "07AA17C660F3DD1D2A1B48F1B746C148"; std::string sDec; // should end up with: "10187768649047767717933300899576725832" I'm currently using the c++ BigInt Class which offers a very easy way to achieve this (but is GPL only): BigInt::Vin vbiTemp(sHex, 16); sDec = vbiTemp.toStrDec(); Is there simple way to do this conversion without a 3rd party arithmetic library? Or can you recommend a free (non-GPL) alternative with similar simplicity

Get GCC To Use Carry Logic For Arbitrary Precision Arithmetic Without Inline Assembly?

喜夏-厌秋 提交于 2019-12-03 06:49:08
When working with arbitrary precision arithmetic (e.g. 512-bit integers), is there any way to get GCC to use ADC and similar instructions without using inline assembly? A first glance at GMP's sourcecode shows that they simply have assembly implementations for every supported platform. Here is the test code I wrote, which adds two 128-bit numbers from the command line and prints the result. (Inspired by mini-gmp's add_n): #include <stdio.h> #include <stdint.h> #include <stdlib.h> int main (int argc, char **argv) { uint32_t a[4]; uint32_t b[4]; uint32_t c[4]; uint32_t carry = 0; for (int i = 0;

Arbitrary precision arithmetic with GMP

帅比萌擦擦* 提交于 2019-12-02 08:02:10
I'm using the GMP library to make a Pi program, that will calculate about 7 trillion digits of Pi. Problem is, I can't figure out how many bits are needed to hold that many decimal places. 7 trillion digits can represent any of 10^(7 trillion) distinct numbers. x bits can represent 2^x distinct numbers. So you want to solve: 2^x = 10^7000000000000 Take the log-base-2 of both sides: x = log2(10^7000000000000) Recall that log(a^b) = b * log(a) : x = 7000000000000 * log2(10) I get 23253496664212 bits. I would add one or two more just to be safe. Good luck finding the petabytes to hold them,

Strange precision issues in R when computing cumulative binomial probability

我怕爱的太早我们不能终老 提交于 2019-12-01 22:08:23
问题 I've been running into some weird problems when using this code: positions<-c(58256) occurrencies<-c(30) frequency<-c(11/5531777) length<-c(4) prob<-c(0) for(i in 0:(occurrencies-1)) { pow<-frequency^i pow1<-(1-frequency)^(positions-i) bin<-choose(positions, i) prob<<-prob+(bin*pow*pow1) } Each iteration of this for loop should calculate the binomial probability that, i number of occurrences of the event occur given the frequency. Each iteration also sums up the result. This should result in

Strange precision issues in R when computing cumulative binomial probability

蓝咒 提交于 2019-12-01 20:22:01
I've been running into some weird problems when using this code: positions<-c(58256) occurrencies<-c(30) frequency<-c(11/5531777) length<-c(4) prob<-c(0) for(i in 0:(occurrencies-1)) { pow<-frequency^i pow1<-(1-frequency)^(positions-i) bin<-choose(positions, i) prob<<-prob+(bin*pow*pow1) } Each iteration of this for loop should calculate the binomial probability that, i number of occurrences of the event occur given the frequency. Each iteration also sums up the result. This should result in the prob variable never exceeding 1, but after 7 or so for loop iterations, everything goes to hell and

JavaScript pack integers and calculate arbitrary precision float:

三世轮回 提交于 2019-12-01 10:37:02
I need to do the following in JavaScript and so far been unable to find solutions to do it seamlessly: Grab two integers in a specific order and pack them like Python's struct module. This packed value, (bonus for supporting different endianness than host) will be turned into a 64 bit float (double). They must be arbitrary thus I might get an exponent representation of the integer (say, they could be 0xdeadbeef and 500): In exp form: 1.0883076389305e-311 1.0883076389305000 * 10 ^ - 311 I need to convert it to the arbitrary precision, non-exponent form, so: 0

double-double precision floating point as sum of two doubles

折月煮酒 提交于 2019-12-01 09:56:43
问题 Following papers and source code for double-double arithmetic for some time, I still can't find out how exactly a dd_real ( defined as struct dd_real { double x[2];...} ) number is split into two doubles. Say if I initialize it with a string, dd_real pi = "3.14159265358979323846264338327950"; what will be pi.x[0] and pi.xi[1] ? I need to understand it and then write a hopefully small Python function that does it. The reason I don't just want to call into the QD library is that I'd prefer to