help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: fprintf long integers


From: Przemek Klosowski
Subject: Re: fprintf long integers
Date: Mon, 15 Sep 2014 11:39:48 -0400
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.1.0

On 09/14/2014 03:59 PM, João Rodrigues wrote:
I want to use fprintf to generate mixed string and numeric data but it seems that fprint f does not handle well integers longer than 8 characters.

The code below generates number 1*1 where * goes from no zero to 20 zeros, in both float and integer format. My conclusion is that in Octave integers are stored with 8 digits and floats with 16.

Is there a way to force Octave to recognize a number as a long integer?

(For my problem 16 digits would be sufficient. The example goes up to 20 to show the limit for floats too.)

Thanks

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


reply via email to

[Prev in Thread] Current Thread [Next in Thread]