On 15-09-2014 16:39, Przemek Klosowski
wrote:
On 09/14/2014 03:59 PM, João
Rodrigues wrote:
Is
there a way to force Octave to recognize a number as a long
integer?
octave:1> a = [0:20]; b = 10.^a+1;
octave:2> fprintf(stdout,"%3d %24.22g %24d \n",[a;b;b]);
0 2 2
1 11 11
2 101 101
..
9 1000000001 1000000001
10 10000000001 1410065409
11 100000000001 1215752193
12 1000000000001 -727379967
The default format of Octave data is double precision floating
point, which has about 16 digits of precision.
If you really want long integers, you have to do your calculations
with integer variables. It's enough to just start with the type
you want, because Octave will apply it to further calculations on
that variable:
a = int64([0:20]); b = 10.^a+1;
You also have to print them as long integers, using 'ld' instead
of 'd' in the format specifier
fprintf(stdout,"%3d %24.22g %24ld \n",[a;b;b]);
0 2 2
1 11 11
[..correct, boring results elided..]
15 1000000000000001 1000000000000001 <-
last correct result
16 10000000000000000 10000000000000000 <-
bug in printf?
17 100000000000000000 100000000000000000 <- bug
in printf?
18 1000000000000000000 1000000000000000000 <- bug
in printf?
19 9223372036854775808 -9223372036854775808 <- int
64 overflow
20 9223372036854775808 -9223372036854775808
So, where did the final '1' go for 10^16+1? Is there a bug in the
integer format specifier for large values or am I missing
something about printf formats?
BTW, you could squeeze a doubling of range by using unsigned
int64:
a = uint64([0:20]); b = 10.^a+1:
fprintf(stdout,"%3d %24.22g %24lu \n",[a;b;b]);
...
15 1000000000000001 1000000000000001
16 10000000000000000 10000000000000000
17 100000000000000000 100000000000000000
18 1000000000000000000 1000000000000000000
19 10000000000000000000 10000000000000000000
20 18446744073709551616 0
Thanks! uint64() and lu/ld are what I was looking for.
Still, I find it strange that integers are handled as single (8
digit precision) while floats are double by default (16 digit
precision).
I also think that it would be nice to allow for more than 16 digit
precision if desired. (I don't need it but I guess someone may).
|