# read a large prime number(which is in hexadecimal form) from a file and store that in a global variable(integer form)

2 vues (au cours des 30 derniers jours)
Anil le 13 Août 2014
I was trying to read a large prime number(which is in hexadecimal form) from a file and store that in a global variable(integer form) in Matlab.
I tried using vpi() function but that gave me the followings
CODE:
hex2dec(tline)
num2str(hex2dec(tline))
prime=vpi(num2str(hex2dec(tline)));
disp('prime = ');
OUTPUT:
FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E088A67CC74020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245E485B576625E7EC6F44C42E9A63A3620FFFFFFFFFFFFFFFF
ans = //hex2dec
1.5525e+231
ans = //num2str ALL BITS AFTER 9 ROUNDED OFF TO ZERO
1552518092300708900000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
CAN ANYBODY TELL ME HOW CAN I GET THE EXACT NUMBER AND USE THAT AS A GLOBAL VARIABLE.
##### 0 commentairesAfficher -2 commentaires plus anciensMasquer -2 commentaires plus anciens

Connectez-vous pour commenter.

### Réponses (1)

Christopher Berry le 13 Août 2014
Modifié(e) : Christopher Berry le 13 Août 2014
Anil,
Your prime number is a 192 character hexadecimal number, so that is roughly 2^768 in size. This is much bigger than the maximum integer MATLAB can store 18446744073709551615. You can check this by typing:
intmax('uint64')
Also, it is bigger than the integer precision of IEEE 754 double variables, which is 2^53. This explains why you are seeing rounding errors when converting it to a double.
To work with numbers on this scale in MATLAB, you can use the Symbolic Math toolbox. Specifically, MuPad has a function text2int which will do exactly what you want, convert from hexadecimal to decimal, but keep the results in symbolic form rather than numeric: The documentation for this function is here:
To get this result into the base MATLAB workspace, you can use evalin with the symengine argument. To avoid quoting issues, use sprintf to create the expression to be evaluated as follows (notice the double quotes around %s):
>> str = sprintf('text2int("%s",16)','FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E088A67CC74020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245E485B576625E7EC6F44C42E9A63A3620FFFFFFFFFFFFFFFF')
s = evalin(symengine,str)
which return s in decimal as a sym
s =
1552518092300708935130918131258481755631334049434514313202351194902966239949102107258669453876591642442910007680288864229150803718918046342632727613031282983744380820890196288509170691316593175367469551763119843371637221007210577919
##### 0 commentairesAfficher -2 commentaires plus anciensMasquer -2 commentaires plus anciens

Connectez-vous pour commenter.

### Catégories

En savoir plus sur String Parsing dans Help Center et File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by