How to prevent Matlab to round off numbers?

15 views (last 30 days)
kenneth
kenneth on 1 Mar 2013
I'm working on some funny problems through the projecteuler.net community, and as of now two of the problems have dealt with very large numbers, i.e. numbers with more than 40 digits in a row. I've linked the problems below. My question is how can I import the number(s) to Matlab?
I have tried to copy the number from projecteuler.net and save them in a text editor as a txt file. This file I have placed in the working directory of my Matlab project, and I can even open the file in Matlab to view the number, and so far all is good. The problem occurs when I try to load the number(s) to Matlab. I have tried the load and the importdata function. Both functions start rounding off the number after about the fifteenth digit.
This is the first 2 rows of the 13th problem from projecteuler.net:
37107287533902102798797998220837590246510135740250 46376937677490009712648124896970078050417018260538
After I use either load or importdata, the rows now look like this:
37107287533902104311025740304689820495323647180800 46376937677490011015859272153628787382155365842944
I have also tried to paste the number in excel, pasting them as a char since excel start rounding off numbers after about the fifteenth digit too - but unfortunately I have not found a way to import this to Matlab even thought I have been using xlsread.
Is there anyway of omitting this problem without manually dividing the number after - f.eks. - every 10th digit?
Any advice is welcome :)
Regards
Kenneth

Answers (2)

Martin
Martin on 1 Mar 2013
I'm not entirely sure how many digits it works for but putting the line: "format long g" at the start of your script might help I'm not sure if it will work for 40 digits though...

Daniel Shub
Daniel Shub on 1 Mar 2013
The problem is that your numbers are too big to be accurately represented as double precision (or even 64-bit integers). You can use FEX:vpi to do it.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!