|
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... 9 1000000001 1000000001The 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 |
[Prev in Thread] | Current Thread | [Next in Thread] |